@@ -24,12 +24,18 @@ public class DoublyLinkedList {
2424 */
2525 private Link tail ;
2626
27+ /**
28+ * Size refers to the number of elements present in the list
29+ */
30+ private int size ;
31+
2732 /**
2833 * Default Constructor
2934 */
3035 public DoublyLinkedList () {
3136 head = null ;
3237 tail = null ;
38+ size = 0 ;
3339 }
3440
3541 /**
@@ -43,6 +49,7 @@ public DoublyLinkedList(int[] array) {
4349 for (int i : array ) {
4450 insertTail (i );
4551 }
52+ size = array .length ;
4653 }
4754
4855 /**
@@ -58,6 +65,7 @@ public void insertHead(int x) {
5865 head .previous = newLink ; // newLink <-- currenthead(head)
5966 newLink .next = head ; // newLink <--> currenthead(head)
6067 head = newLink ; // newLink(head) <--> oldhead
68+ ++size ;
6169 }
6270
6371 /**
@@ -76,6 +84,38 @@ public void insertTail(int x) {
7684 newLink .previous = tail ; // currentTail(tail) <--> newLink -->
7785 tail = newLink ; // oldTail <--> newLink(tail) -->
7886 }
87+ ++size ;
88+ }
89+
90+ /**
91+ * Insert an element at the index
92+ *
93+ * @param x Element to be inserted
94+ * @param index Index(from start) at which the element x to be inserted
95+ *
96+ */
97+ public void insertElementByIndex (int x ,int index ){
98+ if (index > size ) throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size );
99+ if (index == 0 ){
100+ insertHead (x );
101+ }else {
102+ if (index == size ){
103+ insertTail (x );
104+ }else {
105+ Link newLink = new Link (x );
106+ Link previousLink = head ; //
107+ for (int i = 1 ; i < index ; i ++){ //Loop to reach the index
108+ previousLink = previousLink .next ;
109+ }
110+ // previousLink is the Link at index - 1 from start
111+ previousLink .next .previous = newLink ;
112+ newLink .next = previousLink .next ;
113+ newLink .previous = previousLink ;
114+ previousLink .next = newLink ;
115+ }
116+ }
117+ ++size ;
118+
79119 }
80120
81121 /**
@@ -92,6 +132,7 @@ public Link deleteHead() {
92132 } else {
93133 head .previous = null ; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
94134 }
135+ --size ;
95136 return temp ;
96137 }
97138
@@ -109,7 +150,7 @@ public Link deleteTail() {
109150 } else {
110151 tail .next = null ; // 2ndLast(tail) --> null
111152 }
112-
153+ -- size ;
113154 return temp ;
114155 }
115156
@@ -140,6 +181,7 @@ else if (current == tail)
140181 current .previous .next = current .next ; // 1 --> 3
141182 current .next .previous = current .previous ; // 1 <--> 3
142183 }
184+ --size ;
143185 }
144186
145187 /**
@@ -165,6 +207,7 @@ else if (current == null)
165207 newLink .next = current ; // 1 <--> newLink --> 2(current) <--> 3
166208 current .previous = newLink ; // 1 <--> newLink <--> 2(current) <--> 3
167209 }
210+ ++size ;
168211 }
169212
170213 /**
@@ -178,9 +221,10 @@ public void deleteNode(Link z) {
178221 } else if (z == head ){
179222 deleteHead ();
180223 } else { //before <-- 1 <--> 2(z) <--> 3 -->
181- z .previous .next = z .next // 1 --> 3
182- z .next .previous = z .previous // 1 <--> 3
224+ z .previous .next = z .next ; // 1 --> 3
225+ z .next .previous = z .previous ; // 1 <--> 3
183226 }
227+ --size ;
184228 }
185229
186230 public static void removeDuplicates (DoublyLinkedList l ) {
@@ -196,6 +240,16 @@ public static void removeDuplicates(DoublyLinkedList l ) {
196240 }
197241 }
198242
243+ /**
244+ * Clears List
245+ *
246+ */
247+ public void clearList (){
248+ head = null ;
249+ tail = null ;
250+ size = 0 ;
251+ }
252+
199253 /**
200254 * Returns true if list is empty
201255 *
@@ -279,5 +333,11 @@ public static void main(String args[]) {
279333 myList .insertOrdered (67 );
280334 myList .insertOrdered (3 );
281335 myList .display (); // <-- 3(head) <--> 10 <--> 13 <--> 23 <--> 67(tail) -->
336+ myList .insertElementByIndex (5 , 1 );
337+ myList .display (); // <-- 3(head) <--> 5 <--> 10 <--> 13 <--> 23 <--> 67(tail) -->
338+ myList .clearList ();
339+ myList .display ();
340+ myList .insertHead (20 );
341+ myList .display ();
282342 }
283343}
0 commit comments