-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path27. IntroductiontoLinkedList.java
More file actions
247 lines (191 loc) · 5.78 KB
/
27. IntroductiontoLinkedList.java
File metadata and controls
247 lines (191 loc) · 5.78 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
-> the LinkedList class is almost identical to the ArrayList.
-> the LinkedList class is a collection which can contain many objects of the same type, just like the ArrayList.
-> the LinkedList class has all of the same methods as the ArrayList class because they both implement the list interface.
this means that you can add items, change items, remove items and clear the list in the same way.
-> however, while the ArrayList class and the LinkedList class can be used in the same way, they are built very differently.
the LinkedList stores its items in "containers." the list has a link to the first container and each container has a link
to the next container in the list. To add an element to the list, the element is placed into a new container and
that container is linked to one of the other containers in the list.
NOTE: use an ArrayList for storing and accessing data, and LinkedList to manipulate data.
LinkedList: 1) variable size
2) non-contiguous memory
3) insert in O(1)
4) search in O(n)
node: a node is a fundamental component of a linked list data structure. a node represents a single element or “item” in the linked list.
it contains two primary components:
1. data: this is the actual value or information stored in the node, which can be of any data type (e.g., integer, string, object).
2. reference (or pointer): this is a link to the next node in the linked list. it points to the node that comes after the current node in the sequence.
head -> first node
tail -> last node
there are total 3 types of LinkedList:
1. Singly LinkecList
2. Doubly LinkedList
3. Circular LinkedList (no null points)
*/
// ok, enough theory... let's learn it from scratch!
// << specially for the beginnerzzz :) >>
public class LL {
Node head;
private int size;
LL() {
this.size = 0;
}
class Node {
String data;
Node next;
Node(String data) {
this.data = data;
this.next = null;
size++;
}
}
//add first
public void addFirst(String data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
return;
}
newNode.next = head;
head = newNode;
}
//add last
public void addLast(String data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
return;
}
Node currNode = head;
while(currNode.next != null) {
currNode = currNode.next;
}
currNode.next = newNode;
}
//print
public void printList() {
if(head == null) {
System.out.println("list is empty.");
return;
}
Node currNode = head;
while(currNode != null) {
System.out.print(currNode.data + " -> ");
currNode = currNode.next;
}
System.out.println("NULL");
}
//delete first
public void deleteFirst() {
if(head == null) {
System.out.println("list is empty.");
return;
}
size--;
head = head.next;
}
//delete last
public void deleteLast() {
if(head == null) {
System.out.println("list is empty.");
return;
}
size--;
if(head.next == null) {
head = null;
return;
}
Node secondLast = head;
Node lastNode = head.next;
while(lastNode.next != null) {
lastNode = lastNode.next;
secondLast = secondLast.next;
}
secondLast.next = null;
}
//size
public int getSize() {
return size;
}
//main function
public static void main(String[] args) {
LL list = new LL();
list.addFirst("a");
list.addFirst("is");
list.printList();
list.addLast("list");
list.printList();
list.addFirst("this");
list.printList();
list.deleteFirst();
list.printList();
list.deleteLast();
list.printList();
System.out.println("size: " + list.getSize());
list.addFirst("this");
System.out.println("size: " + list.getSize());
}
}
// ok, good job captain! you are not beginner anymore.
// now we will do it using the collection framework.
// easy peasy lemon squeeeeezyyyy B)
import java.util.*;
class LL {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.addFirst("a");
list.addFirst("is");
System.out.println(list);
list.addFirst("this");
list.add("list"); // if we just use 'add' then by default the item will be added in the last of the list. same as 'addLast'
System.out.println(list);
System.out.println("size of list = " + list.size());
for(int i=0; i<list.size(); i++) {
System.out.print(list.get(i) + " -> ");
}
System.out.println("null");
list.removeFirst();
System.out.println(list);
list.removeLast();
System.out.println(list);
list.remove(1); // delete using index
System.out.println(list);
}
}
// let's solve some basic problems... try to do it by yourself, then see the solution!
/* Qs. Make a Linked List & add the following elements to it : (1, 5, 7, 3 , 8, 2, 3). Search for the number 7 & display its index. */
import java.util.*;
class LL {
public static void main(String[] args) {
LinkedList <Integer> list = new LinkedList<>();
list.addAll(Arrays.asList(1,5,7,3,8,2,3));
System.out.println(list);
for(int i=0; i<list.size(); i++) {
if(list.get(i) == 7) {
System.out.println(i);
}
}
}
}
/* Take elements(numbers in the range of 1-50) of a Linked List as input from the user.
Delete all nodes which have values greater than 25. */
import java.util.*;
public class LL {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
for(int i=1; i<=50; i++) {
list.add(i);
}
for(int i=0; i<list.size(); i++) {
if(list.get(i) > 25) {
list.remove(i);
i--;
}
}
System.out.println(list);
System.out.println(list.size());
}
}
// enough for now. but keep practicing :)
// byeeeeeeeeeeeeeeeeeeeeeeeee!!!!!!!!!!