Ejemplo n.º 1
0
        public INode <T> AddBefore(INode <T> node, T value)
        {
            if (FirstNode == null)
            {
                throw new InvalidOperationException("List is empty");
            }
            else if (FirstNode == node)
            {
                // There's no previous node since it's the first one,
                // possibly because this.Count == 1.
                return(AddFirst(value));
            }
            else
            {
                INode <T> prevNode = FindPrevious(node);
                if (prevNode == null)
                {
                    throw new InvalidOperationException(
                              "node " + node.Value + "not in list");
                }

                INode <T> newNode = new SinglyLinkedNode <T>(value);
                newNode.Next  = node;
                prevNode.Next = newNode;
                Count++;
                return(newNode);
            }
        }
Ejemplo n.º 2
0
        public INode <T> AddLast(T value)
        {
            var newLastNode = new SinglyLinkedNode <T>(value);

            if (FirstNode == null)
            {
                // list is empty
                FirstNode = LastNode = newLastNode;
            }
            else
            {
                LastNode.Next = newLastNode;
                LastNode      = newLastNode;
            }

            Count++;
            return(newLastNode);
        }
Ejemplo n.º 3
0
        public INode <T> AddFirst(T value)
        {
            var newFirstNode = new SinglyLinkedNode <T>(value);

            if (FirstNode == null)
            {
                // Empty list
                FirstNode = LastNode = newFirstNode;
            }
            else
            {
                newFirstNode.Next = FirstNode;
                FirstNode         = newFirstNode;
            }

            Count++;
            return(newFirstNode);
        }
Ejemplo n.º 4
0
        public INode <T> AddAfter(INode <T> node, T value)
        {
            var newNode = new SinglyLinkedNode <T>(value);

            node = Find(node);
            try
            {
                newNode.Next = node.Next;
                if (LastNode == node)
                {
                    LastNode = newNode;
                }
                node.Next = newNode;
                Count++;
                return(newNode);
            }
            catch (NullReferenceException)
            {
                throw new InvalidOperationException(
                          "node " + node.Value + "not in list");
            }
        }
Ejemplo n.º 5
0
 public SinglyLinkedList(T value)
 {
     FirstNode = LastNode = new SinglyLinkedNode <T>(value);
 }