Skip to content

Commit 6a94d06

Browse files
committed
Double linked list print reverse
1 parent 9e36810 commit 6a94d06

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

DataStructures/Lists/DoublyLinkedList.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,18 @@ public void display() { // Prints contents of the list
243243
}
244244
System.out.println();
245245
}
246+
247+
/**
248+
* Prints the contents of the list in reverse order
249+
*/
250+
public void displayBackwards() {
251+
Link current = tail;
252+
while (current != null) {
253+
current.displayLink();
254+
current = current.previous;
255+
}
256+
System.out.println();
257+
}
246258
}
247259

248260
/**
@@ -283,25 +295,32 @@ public static void main(String args[]) {
283295
myList.insertHead(7);
284296
myList.insertHead(10);
285297
myList.display(); // <-- 10(head) <--> 7 <--> 13(tail) -->
298+
myList.displayBackwards();
286299

287300
myList.insertTail(11);
288301
myList.display(); // <-- 10(head) <--> 7 <--> 13 <--> 11(tail) -->
302+
myList.displayBackwards();
289303

290304
myList.deleteTail();
291305
myList.display(); // <-- 10(head) <--> 7 <--> 13(tail) -->
306+
myList.displayBackwards();
292307

293308
myList.delete(7);
294309
myList.display(); // <-- 10(head) <--> 13(tail) -->
310+
myList.displayBackwards();
295311

296312
myList.insertOrdered(23);
297313
myList.insertOrdered(67);
298314
myList.insertOrdered(3);
299315
myList.display(); // <-- 3(head) <--> 10 <--> 13 <--> 23 <--> 67(tail) -->
300316
myList.insertElementByIndex(5, 1);
301317
myList.display(); // <-- 3(head) <--> 5 <--> 10 <--> 13 <--> 23 <--> 67(tail) -->
318+
myList.displayBackwards();
302319
myList.clearList();
303320
myList.display();
321+
myList.displayBackwards();
304322
myList.insertHead(20);
305323
myList.display();
324+
myList.displayBackwards();
306325
}
307326
}

0 commit comments

Comments
 (0)