Kulas_Code20

DoubleLinkedList

Oct 6th, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.80 KB | None | 0 0
  1. package linkedlist;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class DoublyLinkedList {
  6.     public static void main(String[] args) {
  7.         DoublyLinkedList2 list = new DoublyLinkedList2();
  8.         Scanner sc = new Scanner(System.in);
  9.         String[] menu = { "Doubly Linked List Operations", "[1] - Add to Head", "[2] - Add to Tail ",
  10.                 "[3] - Add an Item after the target", "[4] - Remove From Head ", "[5] - Remove from Tail",
  11.                 "[6] - Remove the target item", "[7] - Print Head", "[8] - Print Tail", "[9] - Print Forward",
  12.                 "[10] - Print Backward", "[0] - Exit" };
  13.         int choice = 0;
  14.         do {
  15.             for (String menuPrint : menu) {
  16.                 System.out.println(menuPrint);
  17.             }
  18.             System.out.print("Enter the number of your choice: ");
  19.             choice = sc.nextInt();
  20.  
  21.             switch (choice) {
  22.                 case 1:
  23.                     System.out.print("Enter number to add to head: ");
  24.                     list.addToHead(sc.nextInt());
  25.                     break;
  26.                 case 2:
  27.                     System.out.print("Enter number to add to tail: ");
  28.                     list.addToTail(sc.nextInt());
  29.                     break;
  30.                 case 3:
  31.                     System.out.print("Enter number to insert: ");
  32.                     int insertNumeber = sc.nextInt();
  33.                     System.out.print("Enter number of target position to insert: ");
  34.                     int targetPosition = sc.nextInt();
  35.                     // list.addToTail(sc.nextInt());
  36.                     break;
  37.                 case 4:
  38.                     list.deleteHead();
  39.                     break;
  40.                 case 5:
  41.                     list.deleteTail();
  42.                     break;
  43.                 case 6:
  44.                     System.out.print("Enter number of target position to remove: ");
  45.                     int targetRemove = sc.nextInt();
  46.                     list.deletTarget(targetRemove);
  47.                     break;
  48.                 case 7:
  49.                     list.printHead();
  50.                     break;
  51.                 case 8:
  52.                     list.printTail();
  53.                     break;
  54.                 case 9:
  55.                     list.printForward();
  56.                     break;
  57.                 case 10:
  58.                     list.printBackward();
  59.                     break;
  60.                 case 0:
  61.                     System.out.println("Exited...");
  62.                     break;
  63.             }
  64.         } while (choice != 0);
  65.  
  66.     }
  67. }
  68.  
  69. //Other files
  70.  
  71. package linkedlist;
  72.  
  73. //doubly linked list file
  74.  
  75. public class DoublyLinkedList2 {
  76.     Node head;
  77.     Node tail;
  78.  
  79.     public DoublyLinkedList2() {
  80.         head = tail = null;
  81.  
  82.     }
  83.  
  84.     public DoublyLinkedList2(Node head, Node tail) {
  85.         this.head = head;
  86.         this.tail = tail;
  87.     }
  88.  
  89.     public boolean isCget() {
  90.         return head == null && tail == null ? true : false;
  91.     }
  92.  
  93.     public void addToHead(int item) {
  94.         if (isCget()) {
  95.             Node newNode = new Node(null, item, null);
  96.             head = tail = newNode;
  97.         }
  98.  
  99.         else {
  100.             Node newNode = new Node(null, item, head);
  101.             head = newNode;
  102.             head.next.previous = head;
  103.         }
  104.     }
  105.  
  106.     public void addToTail(int item) {
  107.         if (isCget()) {
  108.             Node newNode = new Node(null, item, head);
  109.             tail = head = newNode;
  110.         }
  111.  
  112.         else {
  113.             Node newNode = new Node(null, item, head);
  114.             tail = newNode;
  115.             tail.next.previous = tail;
  116.         }
  117.     }
  118.  
  119.     public void printHead() {
  120.         System.out.println("Head: " + head.item);
  121.     }
  122.  
  123.     public void printTail() {
  124.         System.out.println("Tail: " + tail.item);
  125.     }
  126.  
  127.     public void printForward() {
  128.         Node current;
  129.         current = head;
  130.         System.out.println("Printing Forward");
  131.         while (current != null) {
  132.             System.out.print(" " + current.item);
  133.             current = current.next;
  134.  
  135.         }
  136.         System.out.println("");
  137.     }
  138.  
  139.     public void printBackward() {
  140.         Node current;
  141.         current = tail;
  142.         System.out.println("Printing Backward");
  143.         while (current != null) {
  144.             System.out.print(" " + current.item);
  145.             current = current.previous;
  146.  
  147.         }
  148.         System.out.println("");
  149.     }
  150.  
  151.     public void deleteHead() {
  152.         if (this.head != null) {
  153.             Node temp = this.head;
  154.             this.head = this.head.next;
  155.             temp = null;
  156.             if (this.head != null)
  157.                 this.head.previous = null;
  158.         }
  159.     }
  160.  
  161.     // Delete last node of the list
  162.     public void deleteTail() {
  163.         int item = 0;
  164.         if (this.head != null) {
  165.             if (this.head.next == null) {
  166.                 this.head = null;
  167.             } else {
  168.                 Node temp = new Node(tail, item, head);
  169.                 temp = this.head;
  170.                 while (temp.next.next != null)
  171.                     temp = temp.next;
  172.                 Node lastNode = temp.next;
  173.                 temp.next = null;
  174.                 lastNode = null;
  175.             }
  176.         }
  177.     }
  178.  
  179.     public void deletTarget(int n) {
  180.         if (head == null) {
  181.             return;
  182.         } else {
  183.             Node current = head;
  184.  
  185.             int pos = n;
  186.  
  187.             for (int i = 1; i < pos; i++) {
  188.                 current = current.next;
  189.             }
  190.  
  191.             if (current == head) {
  192.                 head = current.next;
  193.             } else if (current == tail) {
  194.                 tail = tail.previous;
  195.             } else {
  196.                 current.previous = current.next;
  197.                 current.next.previous = current.previous;
  198.             }
  199.             current = null;
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment