-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListExample.java
More file actions
85 lines (52 loc) · 2.65 KB
/
LinkedListExample.java
File metadata and controls
85 lines (52 loc) · 2.65 KB
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
import java.util.LinkedList;
import java.util.List;
public class LinkedListExample {
public static void main(String[] args) {
List<String> linkedList = new LinkedList<>();
linkedList.add("Apple");
linkedList.add("Banana");
linkedList.add("Cherry");
linkedList.add("Date");
linkedList.add("Elderberry");
System.out.println("Original LinkedList: " + linkedList);
printNodeAddresses(linkedList);
linkedList.add(0, "Apricot");
System.out.println("After inserting Apricot at the start: " + linkedList);
printNodeAddresses(linkedList);
linkedList.add("Fig");
System.out.println("After inserting Fig at the end: " + linkedList);
printNodeAddresses(linkedList);
linkedList.add(2, "Blueberry");
System.out.println("After inserting Blueberry at index 2: " + linkedList);
printNodeAddresses(linkedList);
System.out.println("Element at index 3: " + linkedList.get(3));
linkedList.set(3, "Blackberry");
System.out.println("After updating element at index 3 to Blackberry: " + linkedList);
printNodeAddresses(linkedList);
linkedList.remove(0);
System.out.println("After removing element at the start: " + linkedList);
printNodeAddresses(linkedList);
linkedList.remove(linkedList.size() - 1);
System.out.println("After removing element at the end: " + linkedList);
printNodeAddresses(linkedList);
linkedList.remove(3);
System.out.println("After removing element at index 3: " + linkedList);
printNodeAddresses(linkedList);
linkedList.remove("Banana");
System.out.println("After removing element with value Banana: " + linkedList);
printNodeAddresses(linkedList);
boolean containsCherry = linkedList.contains("Cherry");
System.out.println("LinkedList contains Cherry: " + containsCherry);
int size = linkedList.size();
System.out.println("Size of the LinkedList: " + size);
linkedList.clear();
System.out.println("After clearing the LinkedList: " + linkedList);
}
public static void printNodeAddresses(List<String> list) {
System.out.println("Addresses of nodes in the LinkedList:");
for (String element : list) {
System.out.println(element + " : " + System.identityHashCode(element));
}
System.out.println("\n");
}
}