|
| 1 | +class LinkedList { |
| 2 | + private static ListNode head = null; |
| 3 | + private static ListNode tail = null; |
| 4 | + LinkedList(int data) { |
| 5 | + ListNode node = new ListNode(data); |
| 6 | + head = node; |
| 7 | + tail = node; |
| 8 | + } |
| 9 | + |
| 10 | + public void printList(){ |
| 11 | + System.out.print("START -> "); |
| 12 | + ListNode ptr = head; |
| 13 | + while(ptr != null){ |
| 14 | + System.out.print(ptr.getData()+" -> "); |
| 15 | + ptr = ptr.getNext(); |
| 16 | + } |
| 17 | + System.out.println("END"); |
| 18 | + } |
| 19 | + |
| 20 | + public void insertHead(int data){ |
| 21 | + ListNode node = new ListNode(data); |
| 22 | + if(head == null) |
| 23 | + head = tail = node; |
| 24 | + else{ |
| 25 | + node.setNext(head); |
| 26 | + head = node; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public void insertTail(int data){ |
| 31 | + ListNode node = new ListNode(data); |
| 32 | + if(head == null) |
| 33 | + head = tail = node; |
| 34 | + else{ |
| 35 | + tail.setNext(node); |
| 36 | + tail = node; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public void insertAfterNodeData(int afterThisData, int data){ |
| 41 | + ListNode node = new ListNode(data); |
| 42 | + if(head == null) |
| 43 | + head = tail = node; |
| 44 | + else{ |
| 45 | + ListNode ptr = head; |
| 46 | + while(ptr.getData() != afterThisData) |
| 47 | + ptr = ptr.getNext(); |
| 48 | + node.setNext(ptr.getNext()); |
| 49 | + ptr.setNext(node); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public void deleteHead(){ |
| 54 | + if(head == null){ |
| 55 | + System.out.print("List Empty !"); |
| 56 | + return; |
| 57 | + } |
| 58 | + else{ |
| 59 | + ListNode ptr = head; |
| 60 | + head = ptr.getNext(); |
| 61 | + ptr = null; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public void deleteTail(){ |
| 66 | + if(head == null){ |
| 67 | + System.out.print("List Empty !"); |
| 68 | + return; |
| 69 | + } |
| 70 | + else{ |
| 71 | + ListNode ptr = head; |
| 72 | + while(ptr.getNext().getNext() != null) |
| 73 | + ptr = ptr.getNext(); |
| 74 | + ptr.setNext(null); |
| 75 | + tail = ptr; |
| 76 | + tail.setNext(null); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public void deleteNodeWithData(int toBeDeleted){ |
| 81 | + if(head == null){ |
| 82 | + System.out.print("List Empty !"); |
| 83 | + return; |
| 84 | + } |
| 85 | + else{ |
| 86 | + ListNode ptr = head; |
| 87 | + while(ptr.getNext().getData() != toBeDeleted) |
| 88 | + ptr = ptr.getNext(); |
| 89 | + ptr.setNext(ptr.getNext().getNext()); |
| 90 | + ptr = ptr.getNext(); |
| 91 | + ptr = null; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public static void main(String[] args) { |
| 96 | + // TODO Auto-generated method stub |
| 97 | + LinkedList L = new LinkedList(21); |
| 98 | + L.insertHead(15); |
| 99 | + L.insertTail(36); |
| 100 | + L.insertTail(50); |
| 101 | + L.insertTail(45); |
| 102 | + L.insertAfterNodeData(36, 10); |
| 103 | + L.printList(); |
| 104 | + L.deleteNodeWithData(10); |
| 105 | + L.printList(); |
| 106 | + } |
| 107 | +} |
0 commit comments