Skip to main content
Reply to comment from @Gabe which says, "Also, Id like to see what your ToString() would look like."
Source Link
ChrisW
  • 13.1k
  • 1
  • 35
  • 76

I suspect that the question is badly worded, and/or that your answer is too clever and simple for their question, and that instead they wanted a class which implements a list.

Something like:

public class ListOfStrings
{
    class Node
    {
        internal Node(string stringValue) { this.stringValue = stringValue; }
        internal readonly string stringValue;
        internal Node nextNode;
    }

    Node firstNode;

    public string Add(string input)
    {
        Node newNode = new Node(stringValue)
        if (firstNode == null)
        {
            firstNode = newNode;
            return;
        }
        Node lastNode = firstNode;
        while (lastNode.nextNode != null)
            lastNode = lastNode.nextNode;
        lastNode.nextNode = newNode;
    }

In order to "Ideally, this code should be able to handle large numbers of items" you would want to store lastNode as well as firstNode as members (so that you don't need to recalculate lastNode every time you add a new string).

There's some ambiguity when they talk about a 'set' of strings; perhaps they want to keep the strings unique.


Also, I'd like to see what your ToString() would look like.

public override string ToString()
{
    StringBuilder sb = new StringBuilder();
    if (firstNode != null)
    {
        sb.Append(firstNode.stringValue);
        // next strings if any are comma-delimited
        for (Node nextNode = firstNode.nextNode; nextNode != null; nextNode = nextNode.nextNode)
        {
            sb.Append(",");
            sb.Append(nextNode.stringValue);
        }
    }
    return sb.ToString();
}

The above returns an empty string if there are no nodes/strings in the list. Alternatively you might want to return null so that the user can distinguish between an empty list, and list which contains one empty string.

OTOH this storage/display format is already sightly ambiguous, e.g. if a string contains a comma when it's stored.

I suspect that the question is badly worded, and/or that your answer is too clever and simple for their question, and that instead they wanted a class which implements a list.

Something like:

public class ListOfStrings
{
    class Node
    {
        internal Node(string stringValue) { this.stringValue = stringValue; }
        internal readonly string stringValue;
        internal Node nextNode;
    }

    Node firstNode;

    public string Add(string input)
    {
        Node newNode = new Node(stringValue)
        if (firstNode == null)
        {
            firstNode = newNode;
            return;
        }
        Node lastNode = firstNode;
        while (lastNode.nextNode != null)
            lastNode = lastNode.nextNode;
        lastNode.nextNode = newNode;
    }

In order to "Ideally, this code should be able to handle large numbers of items" you would want to store lastNode as well as firstNode as members (so that you don't need to recalculate lastNode every time you add a new string).

There's some ambiguity when they talk about a 'set' of strings; perhaps they want to keep the strings unique.

I suspect that the question is badly worded, and/or that your answer is too clever and simple for their question, and that instead they wanted a class which implements a list.

Something like:

public class ListOfStrings
{
    class Node
    {
        internal Node(string stringValue) { this.stringValue = stringValue; }
        internal readonly string stringValue;
        internal Node nextNode;
    }

    Node firstNode;

    public string Add(string input)
    {
        Node newNode = new Node(stringValue)
        if (firstNode == null)
        {
            firstNode = newNode;
            return;
        }
        Node lastNode = firstNode;
        while (lastNode.nextNode != null)
            lastNode = lastNode.nextNode;
        lastNode.nextNode = newNode;
    }

In order to "Ideally, this code should be able to handle large numbers of items" you would want to store lastNode as well as firstNode as members (so that you don't need to recalculate lastNode every time you add a new string).

There's some ambiguity when they talk about a 'set' of strings; perhaps they want to keep the strings unique.


Also, I'd like to see what your ToString() would look like.

public override string ToString()
{
    StringBuilder sb = new StringBuilder();
    if (firstNode != null)
    {
        sb.Append(firstNode.stringValue);
        // next strings if any are comma-delimited
        for (Node nextNode = firstNode.nextNode; nextNode != null; nextNode = nextNode.nextNode)
        {
            sb.Append(",");
            sb.Append(nextNode.stringValue);
        }
    }
    return sb.ToString();
}

The above returns an empty string if there are no nodes/strings in the list. Alternatively you might want to return null so that the user can distinguish between an empty list, and list which contains one empty string.

OTOH this storage/display format is already sightly ambiguous, e.g. if a string contains a comma when it's stored.

Source Link
ChrisW
  • 13.1k
  • 1
  • 35
  • 76

I suspect that the question is badly worded, and/or that your answer is too clever and simple for their question, and that instead they wanted a class which implements a list.

Something like:

public class ListOfStrings
{
    class Node
    {
        internal Node(string stringValue) { this.stringValue = stringValue; }
        internal readonly string stringValue;
        internal Node nextNode;
    }

    Node firstNode;

    public string Add(string input)
    {
        Node newNode = new Node(stringValue)
        if (firstNode == null)
        {
            firstNode = newNode;
            return;
        }
        Node lastNode = firstNode;
        while (lastNode.nextNode != null)
            lastNode = lastNode.nextNode;
        lastNode.nextNode = newNode;
    }

In order to "Ideally, this code should be able to handle large numbers of items" you would want to store lastNode as well as firstNode as members (so that you don't need to recalculate lastNode every time you add a new string).

There's some ambiguity when they talk about a 'set' of strings; perhaps they want to keep the strings unique.