Skip to content

Commit a8bf550

Browse files
committed
feat(LinkedListDemo3): showcase advanced LinkedList operations and traversal
What - Added `LinkedListDemo3` to demonstrate: - Construction with `add()`, `addAll()`, `set()`, `addFirst()`, `addLast()`. - Iteration using `forEach` with method reference. - Retrieval operations: `getLast()`, `peek()`. - Size retrieval with `size()`. - Reverse iteration with `descendingIterator()`. - Added helper `show(int n)` to print each element during forEach. Why - Illustrates that `LinkedList` supports both `List` and `Deque` interfaces. - Highlights methods unique to `Deque` (addFirst, addLast, poll, peek). - Shows how to traverse in reverse order without manual indexing. How 1. Created two linked lists (`ll1` empty, `ll2` from `List.of`). 2. Added integers step by step: - Inserted elements at specific positions with `add` and `addAll`. - Updated element at index 5 with `set`. - Pushed elements at front and back (`addFirst`, `addLast`). 3. Traversed with `forEach` to print elements using `show()`. 4. Retrieved last element with `getLast()`. 5. Printed size with `size()`. 6. Used `descendingIterator` to traverse in reverse order. 7. Used `peek()` to retrieve first element without removing it. Logic - Initial `ll1 = [ ]`. - After operations → `[3, 5, 50, 60, 70, 80, 70, 10, 200]`. - `forEach` prints each element in order. - `getLast()` → `200`. - `size()` → `9`. - Reverse traversal prints → `200 10 70 80 70 60 50 5 3`. - `peek()` → `3`. Complexity - `addFirst()`, `addLast()`, `peek()`, `poll()` → O(1). - `add(index, elem)`, `set(index, elem)` → O(n) because of traversal. - Iteration (normal or descending) → O(n). Applications - LinkedList is well-suited for: - Queue/Deque operations (FIFO/LIFO). - Scenarios needing frequent insert/remove at both ends. - Reverse traversal without manually reversing the list. - Not ideal for random access due to O(n) `get(index)`. Notes - `peek()` is safer than `getFirst()`; returns `null` if list is empty instead of throwing `NoSuchElementException`. - `descendingIterator()` provides clean reverse iteration, unlike ArrayList which requires manual indexing. Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>
1 parent 0a18444 commit a8bf550

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

Section25CollectionFramework/src/ListDemo/LinkedListDemo.java renamed to Section 25 Collections Frameworks/List Interface/LinkedList/src/LinkedListDemo3.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
1-
package ListDemo;
21
import java.util.*;
32

4-
public class LinkedListDemo {
3+
public class LinkedListDemo3 {
54
public static void main(String[] args) {
65
LinkedList<Integer> ll1 = new LinkedList<>();
76

87
LinkedList<Integer> ll2 = new LinkedList<>(List.of(50, 60, 70, 80, 90));
98

10-
ll1.add(10); // ll1 = [10]
9+
ll1.add(10); // ll1 = [10]
1110
ll1.add(0, 5); // ll1 = [5, 10]
1211
ll1.addAll(1, ll2); // ll1 = [5, 50, 60, 70, 80, 90, 10]
13-
ll1.set(5, 70); // ll1 = [5, 50, 60, 70, 80, 70, 10]
12+
ll1.set(5, 70); // ll1 = [5, 50, 60, 70, 80, 70, 10]
1413

1514
ll1.addFirst(3);
1615
ll1.addLast(200);
1716

1817
ll1.forEach(n->show(n));
1918

2019
System.out.println("Last Element is: " + ll1.getLast());
21-
/*System.out.println(ll1.pollFirst());*/
20+
/* System.out.println(ll1.pollFirst()); */
2221

2322
System.out.print("The size of the LinkedList is: " + ll1.size()+ "\n" );
2423

2524
Iterator<Integer> descItr = ll1.descendingIterator();
2625
System.out.print("Elements in reverse order:");
2726

28-
while (descItr.hasNext())
29-
{
27+
while (descItr.hasNext()) {
3028
System.out.print(" "+descItr.next());
3129
}
3230
System.out.println("\n"+"First Element in list is: "+ll1.peek());
3331
}
34-
static void show(int n)
35-
{
32+
33+
static void show(int n) {
3634
System.out.println(n);
3735
}
38-
}
36+
}

0 commit comments

Comments
 (0)