|
| 1 | +import java.util.Scanner; |
| 2 | + |
| 3 | +class Node { |
| 4 | + int data; |
| 5 | + Node next; |
| 6 | + Node (int x) { |
| 7 | + data = x; |
| 8 | + next = null; |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +public class LL { |
| 13 | + public static void printLL(Node head) { |
| 14 | + Node current = head; |
| 15 | + while (current!=null) { |
| 16 | + System.out.print(current.data+" -> "); |
| 17 | + current = current.next; |
| 18 | + } |
| 19 | + System.out.println("NULL"); |
| 20 | + } |
| 21 | + |
| 22 | + public Node insertAtBeginning(Node head, int x) { |
| 23 | + Node temp = new Node(x); |
| 24 | + temp.next = head; |
| 25 | + return temp; |
| 26 | + } |
| 27 | + |
| 28 | + public Node insertAtEnd(Node head, int x){ |
| 29 | + Node temp = new Node(x); |
| 30 | + if (head == null) { |
| 31 | + return temp; |
| 32 | + } |
| 33 | + Node current = head; |
| 34 | + while (current.next != null) { |
| 35 | + current = current.next; |
| 36 | + } |
| 37 | + current.next = temp; |
| 38 | + return head; |
| 39 | + } |
| 40 | + |
| 41 | + public Node insertAtPosition(Node head, int data, int position) { |
| 42 | + Node temp = new Node(data); |
| 43 | + if (position == 1) { |
| 44 | + temp.next = head; |
| 45 | + return temp; |
| 46 | + } |
| 47 | + |
| 48 | + Node curr = head; |
| 49 | + for (int i = 1; i<=position-2 && curr!=null; i++) { |
| 50 | + curr=curr.next; |
| 51 | + } |
| 52 | + if (curr == null) { |
| 53 | + return head; |
| 54 | + } |
| 55 | + |
| 56 | + temp.next = curr.next; |
| 57 | + curr.next = temp; |
| 58 | + return head; |
| 59 | + } |
| 60 | + |
| 61 | + public Node deleteHeadNode(Node head) { |
| 62 | + if (head == null) { |
| 63 | + return null; |
| 64 | + } else { |
| 65 | + return head.next; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public Node deleteNodeAtEnd(Node head) { |
| 70 | + if (head == null) return null; |
| 71 | + if (head.next == null) return null; |
| 72 | + |
| 73 | + Node curr = head; |
| 74 | + while (curr.next.next!=null) { |
| 75 | + curr = curr.next; |
| 76 | + } |
| 77 | + curr.next = null; |
| 78 | + return head; |
| 79 | + } |
| 80 | + |
| 81 | + public Node deleteNodeAnyPosition(Node head, int position) { |
| 82 | + if (head==null) return null; |
| 83 | + if (position==1) return head.next; |
| 84 | + |
| 85 | + Node curr = head; |
| 86 | + for (int i=1; i<=position-2 && curr!=null; i++) { |
| 87 | + curr = curr.next; |
| 88 | + } |
| 89 | + |
| 90 | + if (curr == null || curr.next == null) { |
| 91 | + return head; |
| 92 | + } |
| 93 | + |
| 94 | + curr.next = curr.next.next; |
| 95 | + return head; |
| 96 | + } |
| 97 | + |
| 98 | + public static void main(String[] args) { |
| 99 | + |
| 100 | + Scanner sc = new Scanner(System.in); |
| 101 | + |
| 102 | + LL list = new LL(); |
| 103 | + Node head = null; |
| 104 | + int choice = 0; |
| 105 | + |
| 106 | + while (choice!=8) { |
| 107 | + System.out.println("\n"); |
| 108 | + System.out.println("Choose what to do?"); |
| 109 | + System.out.println("1. Insert Node at the beginning"); |
| 110 | + System.out.println("2. Insert Node at the end"); |
| 111 | + System.out.println("3. Insert Node at any position"); |
| 112 | + System.out.println("4. Delete Node at the beginning"); |
| 113 | + System.out.println("5. Delete Node at the end"); |
| 114 | + System.out.println("6. Delete Node at any position"); |
| 115 | + System.out.println("7. Print the Linked List"); |
| 116 | + System.out.print("Enter your choice: "); |
| 117 | + choice = sc.nextInt(); |
| 118 | + |
| 119 | + switch (choice) { |
| 120 | + case 1: // Insert at beginning |
| 121 | + System.out.print("Enter value: "); |
| 122 | + int val1 = sc.nextInt(); |
| 123 | + head = list.insertAtBeginning(head, val1); |
| 124 | + break; |
| 125 | + case 2: // Insert at any end |
| 126 | + System.out.print("Enter value: "); |
| 127 | + val1 = sc.nextInt(); |
| 128 | + head = list.insertAtEnd(head, val1); |
| 129 | + break; |
| 130 | + case 3: // Insert at any position |
| 131 | + System.out.print("Enter value: "); |
| 132 | + val1 = sc.nextInt(); |
| 133 | + System.out.print("Enter position: "); |
| 134 | + int pos = sc.nextInt(); |
| 135 | + head = list.insertAtPosition(head, val1, pos); |
| 136 | + break; |
| 137 | + case 4: // Delete at the beginning |
| 138 | + head = list.deleteHeadNode(head); |
| 139 | + System.out.println("Node deleted at beginning!"); |
| 140 | + break; |
| 141 | + case 5: // Delete at the end |
| 142 | + head = list.deleteNodeAtEnd(head); |
| 143 | + System.out.println("Node deleted at end!"); |
| 144 | + break; |
| 145 | + case 6: // Delete at any position |
| 146 | + System.out.print("Enter position: "); |
| 147 | + pos = sc.nextInt(); |
| 148 | + head = list.deleteNodeAnyPosition(head, pos); |
| 149 | + System.out.println("Node deleted at position "+pos); |
| 150 | + break; |
| 151 | + case 7: // Print List |
| 152 | + System.out.println("\n"); |
| 153 | + LL.printLL(head); |
| 154 | + System.out.println("\n"); |
| 155 | + break; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + sc.close(); |
| 160 | + } |
| 161 | +} |
0 commit comments