This document defines a Node class to represent nodes in a linked list, with data and next node references. It also defines a LinkedList class with methods to initialize the list, insert and delete nodes from different positions, find nodes, and traverse the list. The main method demonstrates using the LinkedList by inserting some sample nodes and traversing the list.
This document defines a Node class to represent nodes in a linked list, with data and next node references. It also defines a LinkedList class with methods to initialize the list, insert and delete nodes from different positions, find nodes, and traverse the list. The main method demonstrates using the LinkedList by inserting some sample nodes and traversing the list.
3 private Node next; //Reference to the next Node 4 5 public Node(){ 6 this(-1,null); 7 } 8 9 public Node (int item) { 10 this.item = item; 11 this.next = null; 12 } 13 14 public Node(int item, Node next) { 15 this.item = item; 16 this.next = next; //Refer to the 'next' node. 17 } 18 19 public void setNext(Node next) { 20 this.next=next; 21 } 22 23 public Node getNext() { 24 return next; 25 } 26 27 public int getItem() { 28 return item; 29 } 30 } 31 32 public class LinkedList { 33 34 Node head; // First element in LL 35 Node tail; // Last element in LL 36 37 // Initialization of LL 38 public void LinkedList() { 39 head = tail = null; 40 } 41 42 public boolean isEmpty() { 43 return head == null; 44 } 45 46 public void insertBeginning(int item) { 47 Node temp = new Node(item); // create new node 48 // make next of new node to head 49 // reset head to new node 50 //if(tail == null) 51 // tail = head; 52 } 53 54 public void insertEnd(int item) { 55 Node newNode = new Node(item); //create new node 56 //Set the New node to refer to whatever tail is currently pointing to (NULL) 57 //Set the current tail node refer to the new Node 58 //Reset the tail to the new node 59 } 60 61 //Insertion at Middle - after the given node 62 public void insertAfter(Node p, int item) { 63 Node newNode = new Node(item);//create new node 64 // make next of new node to next of p 65 // set p’s next to new node 66 //See if we have to reset tail 67 } 68 69 //Insertion at Middle - before the given node 70 public void insertBefore(Node ref, int item) { 71 72 //Find Previous Node to the Node Ref. 73 74 //Create New Node 75 //make next of new node refer to Node ref. 76 //Make the previous node refer to the new node; 77 //Check if user is inserting this as first node and if so reset Head to the newNode; 78 79 } 80 81 public void delBeginning() { 82 //If list is empty, nothing to do 83 //If head== tail, there is only one node in the list, so reset tail also; 84 //Anycase Reset the head to its next node; 85 } 86 87 public void delete (Node n) throws Exception { 88 // if (isEmpty()) throw Exception ("List is Empty for Deletion to happen"); 89 90 // Set two temp values - prev and cur = Null; 91 92 93 //Traverse from head to the Node 'n' and keep track of the previous node to 'n' 94 95 // if(prev == null) n is a head node; so set head to its next node; 96 // otherwise set prev.setNext(cur.getNext()); 97 98 //if(tail == cur) tail = prev; //Node 'n' is a tail node. 99 } 100 101 //Delete the End node. 102 public void delEnd() { 103 //If list is not empty 104 //Find the previous node of the last node; 105 //Reset the reference in the previous node to the reference in the last node 106 //Reset the tail to refer to the last but previous node; 107 108 //If list is empty 109 //This means there is only node in the list; 110 //So both head and tail will refer to the same node; 111 } 112 113 //Deletion in the middle - After the given node 114 public void delAfter(Node n) throws Exception { 115 //Findout what Node n's next is referring to - if it is Null, throw an exception 116 //Otherwise check if it is referring to tail, and if yes reset tail to n; 117 //Otherwise set n to refer to next node; 118 } 119 120 public void delBefore(Node n) throws Exception { 121 //If 'n' is head node, throw exception, saying there does not exist any prior to this node 122 123 //Find the node previous to 'n' as 'prev1' 124 125 126 //If prev1 is head node, reset head to 'n'. 127 //Otherwise Find the node previous to 'prev1' as 'prev2' 128 129 //Reset the reference of 'prev2' to 'n' 130 } 131 132 public void traverse() { 133 if (!isEmpty()) 134 { 135 136 Node tmp = head; 137 do { 138 System.out.println (tmp.getItem()); 139 tmp = tmp.getNext(); 140 } while (tmp!= null); 141 } 142 else 143 System.out.println ("List is Empty"); 144 } 145 146 public Node findNode(int x) throws Exception 147 { 148 if (!isEmpty()) 149 { 150 Node tmp = head; 151 do 152 { 153 if (tmp.getItem()== x) return tmp; 154 else tmp = tmp.getNext(); 155 } while (tmp!=null); 156 throw new Exception ("Node not Found"); 157 } 158 else throw new Exception ("List is Empty"); 159 } 160 161 public Node findPreviousNode (Node ref) 162 { 163 Node prev = null; 164 Node curr = head; 165 166 while (curr!=null) 167 { 168 if (curr == ref) break; //We have found the node 169 else 170 { 171 prev=curr; 172 curr=curr.getNext(); 173 } 174 } 175 return prev; 176 } 177 178 public static void main (String[] args) throws Exception 179 { 180 LinkedList ll = new LinkedList(); 181 ll.insertBeginning(5); 182 Node found = ll.findNode(5); 183 ll.insertAfter(found, 6); 184 found = ll.findNode(6); 185 ll.insertBefore(found, 7); 186 ll.insertEnd(8); 187 ll.traverse(); 188 189 /* Try for these cases also 190 ll.delete(found); 191 ll.delAfter(found); 192 ll.delBefore(found); 193 194 ll.delBeginning(); 195 ll.delEnd(); */ 196 197 } 198 }