コード例 #1
0
        // removes the element at the beginning of the collection
        public T RemoveFirst()
        {
            if (first == null) // 0 items
            {
                throw new InvalidOperationException("Linked list empty!");
            }

            T currentFirstValue = first.Value;

            if (first == last) // 1 item
            {
                first = null;
                last  = null;
            }
            else // more than 1 item
            {
                LinkedListItem <T> newFirst = first.Next;
                newFirst.Previous = null;
                first             = newFirst;
            }

            return(currentFirstValue);
        }
コード例 #2
0
        // removes the element at the end of the collection
        public T RemoveLast()
        {
            if (last == null)
            {
                throw new InvalidOperationException("Linked list empty!");
            }

            T currentLastValue = last.Value;

            if (first == last)
            {
                first = null;
                last  = null;
            }
            else
            {
                LinkedListItem <T> newLast = last.Previous;
                newLast.Next = null;
                last         = newLast;
            }

            return(currentLastValue);
        }