-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.java
More file actions
37 lines (27 loc) · 1.01 KB
/
linkedlist.java
File metadata and controls
37 lines (27 loc) · 1.01 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
LinkedList<String> myLinkedList = new LinkedList<String>();
// Add a node with data="First" to back of the (empty) list
myLinkedList.add("First");
// Add a node with data="Second" to the back of the list
myLinkedList.add("Second");
// Insert a node with data="Third" at front of the list
myLinkedList.addFirst("Third");
// Insert a node with data="Fourth" at back of the list
myLinkedList.addLast("Fourth");
// Insert a node with data="Fifth" at index 2
myLinkedList.add(2, "Fifth");
// Print the list: [Third, First, Fifth, Second, Fourth]
System.out.println(myLinkedList);
// Print the value at list index 2:
System.out.println(myLinkedList.get(2));
// Empty the list
myLinkedList.clear();
// Print the newly emptied list: []
System.out.println(myLinkedList);
// Adds a node with data="Sixth" to back of the (empty) list
myLinkedList.add("Sixth");
System.out.println(myLinkedList); // print the list: [Sixth]
//The above code produces the following output:
[Third, First, Fifth, Second, Fourth]
Fifth
[]
[Sixth]