public override void AddFirst(T item)
        {
            //create node.
            var node = new DoublyNode <T>(item);

            // Make next of new node as head and previous as null.
            node.Next = head;
            node.Prev = null;

            //change prev of head node to new null.
            if (head != null)
            {
                head.Prev = node;
            }

            //move the head to point to the new node
            head = node;
            count++;
        }
        public override bool AddAfter(T nodeData, T item)
        {
            var current = head;

            while (current != null)
            {
                if (IsEquals(current.Data, nodeData))
                {
                    var node = new DoublyNode <T>(item);
                    node.Prev    = current;
                    node.Next    = current.Next;
                    current.Next = node;
                    count++;
                    return(true);
                }
                current = current.Next;
            }
            return(false);
        }
Beispiel #3
0
        public override bool AddBefore(T nodeData, T item)
        {
            var current = head;
            var node    = new DoublyNode <T>(item);

            if (current != null && IsEquals(current.Data, nodeData))
            {
                node.Next = head;
                head.Prev = node;
                if (count == 1)
                {
                    node.Prev = head;
                    head.Next = node;
                }
                else
                {
                    node.Prev      = head.Prev;
                    head.Prev.Next = node;
                }
                head = node;
                count++;
                return(true);
            }
            else
            {
                while (current != null && current.Next != head)
                {
                    if (IsEquals(current.Next.Data, nodeData))
                    {
                        node.Next         = current.Next;
                        node.Prev         = current;
                        current.Next      = node;
                        current.Next.Prev = node;
                        count++;
                        return(true);
                    }
                    current = current.Next;
                }
            }
            return(false);
        }
 public virtual void Clear()
 {
     head  = null;
     count = 0;
 }
 protected T GetData(DoublyNode <T> node) => node != null ? node.Data : default(T);