Пример #1
0
        public int IndexOf(string value)
        {
            SinglyLinkedListNode nextNode;

            if (firstNode == null)
            {
                return(-1);
            }
            if (value == firstNode.ToString())
            {
                return(0);
            }
            else
            {
                int index = 1;
                nextNode = firstNode.Next;
                while (nextNode != null)
                {
                    if (nextNode.ToString() == value)
                    {
                        return(index);
                    }
                    nextNode = nextNode.Next;
                    index++;
                }
            }
            return(-1);
        }
Пример #2
0
 public void AddAfter(string existingValue, string value)
 {
     if (existingValue == last.ToString())
     {
         AddLast(value);
     }
     else
     {
         SinglyLinkedListNode node = first;
         while (node != null)
         {
             if (existingValue == node.Value)
             {
                 SinglyLinkedListNode newNode = new SinglyLinkedListNode(value);
                 newNode.Next = node.Next;
                 node.Next    = newNode;
                 count++;
                 break;
             }
             else if (node.Next == null)
             {
                 throw new ArgumentException();
             }
             else
             {
                 node = node.Next;
             }
         }
     }
 }
        public string[] ToArray()
        {
            LinkedList <string>  sentence    = new LinkedList <string>();
            SinglyLinkedListNode currentNode = firstNode;

            if (this.First() == null)
            {
                return(sentence.ToArray());
            }
            else if (firstNode.IsLast())
            {
                sentence.AddFirst(firstNode.ToString());
                return(sentence.ToArray());
            }
            else
            {
                while (currentNode != null)
                {
                    sentence.AddLast(currentNode.ToString());
                    if (currentNode.IsLast())
                    {
                        break;
                    }
                    currentNode = currentNode.Next;
                }
                return(sentence.ToArray());
            }
        }
Пример #4
0
        public override string ToString()
        {
            StringBuilder builtString = new StringBuilder("{ ");

            if (firstNode != null)
            {
                SinglyLinkedListNode testNode = firstNode;
                while (!testNode.IsLast())
                {
                    builtString.Append("\"").Append(testNode.ToString()).Append("\", ");
                    testNode = testNode.Next;
                }
                builtString.Append("\"").Append(testNode.ToString()).Append("\" ");
            }
            builtString.Append("}");
            return(builtString.ToString());
        }
Пример #5
0
 public string ElementAt(int index)
 {
     if (index == 0)
     {
         if (count == -1)
         {
             throw new ArgumentOutOfRangeException();
         }
         else
         {
             return(first.ToString());
         }
     }
     else if (index == count)
     {
         if (last == null)
         {
             return(null);
         }
         else
         {
             return(last.ToString());
         }
     }
     else if (index < count)
     {
         //throw new ArgumentException("else if (index < count)");
         if (GetIndex(index) != null)
         {
             return(GetIndex(index).ToString());
         }
         else
         {
             throw new IndexOutOfRangeException();
         }
     }
     else
     {
         throw new ArgumentOutOfRangeException();
     }
 }
        public override string ToString()
        {
            if (this.firstNode == null)
            {
                return("{ }");
            }
            SinglyLinkedListNode node = firstNode;
            StringBuilder        sb   = new StringBuilder("{ \"");

            while (true)
            {
                sb.Append(node.ToString());
                if (node.Next == null)
                {
                    break;
                }
                sb.Append("\", \"");
                node = node.Next;
            }
            sb.Append("\" }");
            return(sb.ToString());
        }
 public void NodeToString()
 {
     SinglyLinkedListNode node = new SinglyLinkedListNode("foo");
     Assert.AreEqual("foo", node.ToString());
 }