예제 #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);
        }
 public void PrintCacheElements(DoublyLinkedList doublyLinkedList)
 {
     if (doublyLinkedList == null)
     {
         Console.WriteLine("The Doubly Linked List is empty!" +
                           " Cannot print any element :)");
     }
     else
     {
         DoublyLinkedListNode doublyLinkedListNode =
             doublyLinkedList.GetDoublyLinkedListNodeHead();
         if (doublyLinkedListNode == null)
         {
             Console.WriteLine("The head of the Doubly linked list is empty!");
             return;
         }
         Console.WriteLine("Printing elements in the cache memory------");
         while (doublyLinkedListNode != null)
         {
             Console.Write(doublyLinkedListNode.GetDoublyLinkedListNodeData()
                           + " ");
             doublyLinkedListNode = doublyLinkedListNode.GetDoublyLinkedListNodeNext();
         }
     }
     Console.WriteLine();
 }
예제 #3
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);
        }