mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-01-31 23:51:28 +08:00
Add doubly linked list print reverse (#2797)
This commit is contained in:
@@ -271,6 +271,18 @@ public class DoublyLinkedList {
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints the contents of the list in reverse order
|
||||
*/
|
||||
public void displayBackwards() {
|
||||
Link current = tail;
|
||||
while (current != null) {
|
||||
current.displayLink();
|
||||
current = current.previous;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -311,15 +323,19 @@ class Link {
|
||||
myList.insertHead(7);
|
||||
myList.insertHead(10);
|
||||
myList.display(); // <-- 10(head) <--> 7 <--> 13(tail) -->
|
||||
myList.displayBackwards();
|
||||
|
||||
myList.insertTail(11);
|
||||
myList.display(); // <-- 10(head) <--> 7 <--> 13 <--> 11(tail) -->
|
||||
myList.displayBackwards();
|
||||
|
||||
myList.deleteTail();
|
||||
myList.display(); // <-- 10(head) <--> 7 <--> 13(tail) -->
|
||||
myList.displayBackwards();
|
||||
|
||||
myList.delete(7);
|
||||
myList.display(); // <-- 10(head) <--> 13(tail) -->
|
||||
myList.displayBackwards();
|
||||
|
||||
myList.insertOrdered(23);
|
||||
myList.insertOrdered(67);
|
||||
@@ -327,12 +343,15 @@ class Link {
|
||||
myList.display(); // <-- 3(head) <--> 10 <--> 13 <--> 23 <--> 67(tail) -->
|
||||
myList.insertElementByIndex(5, 1);
|
||||
myList.display(); // <-- 3(head) <--> 5 <--> 10 <--> 13 <--> 23 <--> 67(tail) -->
|
||||
|
||||
myList.displayBackwards();
|
||||
myList.reverse(); // <-- 67(head) <--> 23 <--> 13 <--> 10 <--> 5 <--> 3(tail) -->
|
||||
myList.display();
|
||||
|
||||
myList.clearList();
|
||||
myList.display();
|
||||
myList.displayBackwards();
|
||||
myList.insertHead(20);
|
||||
myList.display();
|
||||
myList.displayBackwards();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user