Source of DoublyLinkedListDemoApp.java


  1: //DoublyLinkeListDemoApp.java

  3: public class DoublyLinkedListDemoApp
  4: {
  5:     public static void main(String[] args)
  6:     {
  7:         LinkedList numList = new LinkedList();
  8:         Node nodeA = new Node(14);
  9:         Node nodeB = new Node(2);
 10:         Node nodeC = new Node(20);
 11:         Node nodeD = new Node(31);
 12:         Node nodeE = new Node(16);
 13:         Node nodeF = new Node(55);

 15:         numList.append(nodeA);   // Add 14
 16:         numList.append(nodeB);   // Add 2, make the tail
 17:         numList.append(nodeC);   // Add 20, make the tail

 19:         numList.prepend(nodeD);  // Add 31, make the head

 21:         numList.insertAfter(nodeB, nodeE);  // Insert 16 after 2
 22:         numList.insertAfter(nodeC,
 23:                             nodeF);  // Insert 55 after tail, 55 becomes new tail

 25:         // Output list
 26:         System.out.print("List after adding nodes: ");
 27:         numList.printList();

 29:         // Remove the tail node, then the head node
 30:         numList.remove(nodeF);
 31:         numList.remove(nodeD);

 33:         // Output final list
 34:         System.out.print("List after removing nodes: ");
 35:         numList.printList();
 36:     }
 37: }