Пример #1
0
        public T RemoveLast()
        {
            if (this.Count == 0)
            {
                throw new InvalidOperationException("List empty");
            }
            var currentTail = this.Tail.Value;

            //get current tail pravious element and make it the new tail
            this.Tail = this.Tail.Previous;



            if (this.Tail != null)
            {
                //if tail exist make next element null since it is las element in the collection
                this.Tail.Next = null;
            }
            else
            {
                //if not this mean that collection is empty
                //so head must be asign null since the head is old Tail  element

                this.Head = null;
            }
            this.Count--;
            return(currentTail);
        }
Пример #2
0
 public void AddLast(T element)
 {
     if (this.Count == 0)
     {
         this.Head = this.Tail = new DoublyListNode <T>(element);
     }
     else
     {
         var newHead = new DoublyListNode <T>(element);
         newHead.Previous = this.Tail;
         this.Tail.Next   = newHead;
         this.Tail        = newHead;
     }
     this.Count++;
 }
Пример #3
0
        public T RemoveFirst()
        {
            if (this.Count == 0)
            {
                throw new InvalidOperationException("List empty");
            }

            var currentHead = this.Head.Value;

            this.Head = this.Head.Next;

            if (this.Head != null)
            {
                this.Head.Previous = null;
            }
            else
            {
                this.Tail = null;
            }
            this.Count--;
            return(currentHead);
        }