1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import Revise.LinkedListNode; /* * LinkedList implementation * * 1: Getter and Setter methods setNEXT,getNext, setDATA, getNext * 2: Find length through traversing * 3: Insert elements *Nihal Kurre */ public class LinkedList { LinkedList next; private int data; public LinkedList() { } // Constructor LinkedList(int data) { this.data = data; } // getdata public void setData(int data) { this.data = data; } // Setdata public int getData() { return this.data; } //////////////////////////////////// // Get next public LinkedList getNext() { return this.next; } // SetNext public void setNext(LinkedList next) { this.next = next; } //////////////////////////////////// // 2: Length and traverse public int getLength(LinkedList head) { int length = 0; LinkedList current = head; while (current != null) { current = current.getNext(); length++; } return length; } ////////////////////////////////////// //3: Inserting elements LinkedList head; LinkedList number; public LinkedList insert(LinkedList head, LinkedList number, int position) { if (head == null) { return number; } /* * if (position == 0) { return head; } */ int size = getLength(head); if (position < 1 || position > size + 1) { System.out.println("Error"); return head; } if (position == 1) { number.setNext(head); return number; }else { LinkedList current; LinkedList previous = head; int count = 1; while (count < position - 1) { previous = previous.getNext(); count++; } current = previous.getNext(); previous.setNext(number); number.setNext(current); } return head; } public static void main(String[] args) { // TODO Auto-generated method stub LinkedList linkedList1 = new LinkedList(111); LinkedList linkedList2 = new LinkedList(222); linkedList1.setNext(linkedList2); LinkedList linkedList3 = new LinkedList(333); linkedList2.setNext(linkedList3); LinkedList linkedList4 = new LinkedList(444); linkedList3.setNext(linkedList4); LinkedList test = new LinkedList(); int length = test.getLength(linkedList1); System.out.println("Length of give head node is: " + length); LinkedList linkedList5 = new LinkedList(555); test.insert(linkedList1, linkedList5, 2); System.out.println("=========After INSERTING a node:============= "); length = test.getLength(linkedList1); System.out.println("Length after inserting a node: " + length); for (int i = 0; i < length; i++) { // System.out.println("Node value is: " + test.getData()); test.getNext(); } // linkedList.setData(1222222); // System.out.println(" " + linkedList1.getData()); // System.out.println(); } } |
Linked List Implementation
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment