Example #1
0
        public int RemoveLast()
        {
            if (this.Count == 0)
            {
                throw new InvalidOperationException("The List is empty");
            }

            int removedElement = this.tail.Value;

            if (this.Count == 1)
            {
                this.head = this.tail = null;
            }
            else
            {
                this.tail          = this.tail.PreviousNode;
                this.tail.NextNode = null;
            }

            this.Count--;
            return(removedElement);
        }
Example #2
0
        public int RemoveLast()
        {
            if (this.Count == 0)
            {
                throw new InvalidOperationException("The list is empty.");
            }

            var lastElement = this.tail.Value;

            this.tail = this.tail.PreviousNode;

            if (this.tail != null)
            {
                this.tail.NextNode = null;
            }
            else
            {
                this.head = null;
            }

            this.Count--;
            return(lastElement);
        }
        /// <summary>
        /// Adds an element at the end of the collection.
        /// </summary>
        /// <param name="element">The value to enter.</param>
        public void AddLast(T element)
        {
            if (this.Count == 0)
            {
                this.AddFirst(element);
            }
            else if (this.Count == 1)
            {
                var oldTail = this.Tail;

                this.Head      = oldTail;
                this.Tail      = new ListNode <T>(element, oldTail, null);
                this.Head.Next = this.Tail;
                this.Count++;
            }
            else
            {
                var innerElement = this.Tail;
                this.Tail         = new ListNode <T>(element, innerElement, null);
                innerElement.Next = this.Tail;
                this.Count++;
            }
        }
Example #4
0
        public T RemoveFirst()
        {
            T removedEl = this.head.Value;

            if (this.Count == 0)
            {
                throw new InvalidOperationException("List is empty.");
            }
            else if (this.Count == 1)
            {
                this.head = null;
                this.tail = null;
            }
            else
            {
                ListNode <T> newHead = this.head.NextNode;
                newHead.PreviousNode = null;
                this.head            = newHead;
            }

            this.Count--;

            return(removedEl);
        }
        /// <summary>
        /// Adds an element at the beginning of the collection.
        /// </summary>
        /// <param name="element">The value to enter.</param>
        public void AddFirst(T element)
        {
            if (this.Count == 0)
            {
                this.Head = new ListNode <T>(element, null, null);
                this.Tail = this.Head;
            }
            else if (this.Count == 1)
            {
                var oldHead = this.Head;

                this.Tail          = oldHead;
                this.Head          = new ListNode <T>(element, null, this.Tail);
                this.Tail.Previous = this.Head;
            }
            else
            {
                var innerElement = this.Head;
                this.Head             = new ListNode <T>(element, null, innerElement);
                innerElement.Previous = this.Head;
            }

            this.Count++;
        }