コード例 #1
0
        public DoublyLinkedListNode DeleteNode(DoublyLinkedListNode doublyLinkedListNode, int data)
        {
            if (doublyLinkedListNode == null)
            {
                Console.WriteLine("The Doubly Linked List is empty! Cannot delete at all!");
                return(null);
            }

            DoublyLinkedListNode toBeDeleted = doublyLinkedListNode;

            while (toBeDeleted != null && toBeDeleted.GetDoublyLinkedListNodeData() != data)
            {
                toBeDeleted = toBeDeleted.GetDoublyLinkedListNodeNext();
            }

            if (toBeDeleted.GetDoublyLinkedListNodePrevious() != null)
            {
                toBeDeleted.GetDoublyLinkedListNodePrevious()
                .SetDoublyLinkedListNodeNext(toBeDeleted.GetDoublyLinkedListNodeNext());
            }
            if (toBeDeleted.GetDoublyLinkedListNodeNext() != null)
            {
                toBeDeleted.GetDoublyLinkedListNodeNext()
                .SetDoublyLinkedListNodePrevious(toBeDeleted.GetDoublyLinkedListNodePrevious());
            }
            size--;
            return(doublyLinkedListNode);
        }
コード例 #2
0
        public DoublyLinkedListNode DeleteLast(DoublyLinkedListNode doublyLinkedListNode)
        {
            if (doublyLinkedListNode == null)
            {
                Console.WriteLine("Doubly Linked List is empty! Cannot delete at all!");
                return(doublyLinkedListNode);
            }

            DoublyLinkedListNode newList = doublyLinkedListNode;

            while (newList != null && newList.GetDoublyLinkedListNodeNext() != null)
            {
                newList = newList.GetDoublyLinkedListNodeNext();
            }
            newList.GetDoublyLinkedListNodePrevious().SetDoublyLinkedListNodeNext(null);
            size--;
            return(doublyLinkedListNode);
        }