-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheLinkedLists.java
More file actions
31 lines (27 loc) · 1.17 KB
/
Copy pathTheLinkedLists.java
File metadata and controls
31 lines (27 loc) · 1.17 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
// A linked list is a linear data structure where elements, called nodes,
// are connected sequentially, with each node containing data and a reference.
// Have advantge over arrays and arrayList in insertion and deletion.
// We can use LinkedLists With stack and queue.
// (push(), pop()...) with stack,
// (offer(), poll(), peekFirst(), peekLast(), addFirst(), addLast, removeFirst(), removeLast()...) with queue.
// Methods (add(), remove(), indexOf()).
import java.util.LinkedList;
public class TheLinkedLists {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.push("ABC");
list.push("DEF");
list.push("GHI");
list.push("JKL");
list.push("MNO");
list.pop();
// Add node between JKL and MNO
list.add(4, "Django"); // ==> [MNO, JKL, GHI, DEF, Django, ABC] bcs we working with stack DS
// Remove node
list.remove("JKL"); // ==> [MNO, GHI, DEF, Django, ABC]
// Searche for an index of element
System.out.println(list.indexOf("ABC"));
// ==> With stack it's 4 and with queue it's 1
System.out.println(list);
}
}