-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03. LinkedList_Operations.java
More file actions
322 lines (279 loc) · 8.67 KB
/
03. LinkedList_Operations.java
File metadata and controls
322 lines (279 loc) · 8.67 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package com.company;
///Name-Soumik Deb Niloy
///CSE 220 Section-04
///Lab 02///
///==================================///
public class Main {
//////////////////////////////////// Task 01 ////////////////////////////////////
public static void main(String[] args) {
int[] arr1 = {1,2,3};
System.out.println("///////////Creating list from array/////////////");
Mylist m1 = new Mylist(arr1);
for (Node i = m1.head; i!=null; i=i.next) {
System.out.print(i.value+"->");
}
System.out.println("\n\n/////////// Showlist////////");
m1.ShowList();
System.out.println("\n/////////// isEmpty?////////");
m1.isempty();
System.out.println("\n/////////// Clear////////");
m1.clear();
System.out.println("\n/////////// Insert////////");
int[] arr2 = {1,2,3,4};
Mylist m2 = new Mylist(arr2);
m2.insert(new Node(12,null));
for (Node i = m2.head; i!=null; i=i.next) {
System.out.print(i.value+"->");
}
System.out.println("\n\n/////////// InsertIndex////////");
int[] arr3 = {1,2,3,4};
Mylist m3 = new Mylist(arr3);
m3.insertindex(new Node(12,null),3);
for (Node i = m3.head; i!=null; i=i.next) {
System.out.print(i.value+"->");
}
System.out.println("\n\n/////////// Remove////////");
int[] arr4 = {1,2,3,4};
Mylist m4 = new Mylist(arr4);
m4.remove(2);
for (Node i = m4.head; i!=null; i=i.next) {
System.out.print(i.value+"->");
}
//////////////////////////////////// Task 03 ////////////////////////////////////
System.out.println("\n\n/////////// Even Number////////");
int[] arr5={1,2,5,3,8};
Mylist m5 = new Mylist(arr5);
m5.evennumbers();
System.out.println("\n\n/////////// Element in the list////////");
int[] arr6={101,102,25,91,87,1};
Mylist m6=new Mylist(arr6);
m6.availablity(87);
System.out.println("\n\n/////////// Reverse////////");
int[] arr7={1,2,5,3,8};
Mylist m7=new Mylist(arr7);
m7.reverselist();
for (Node i = m7.head; i!=null; i=i.next) {
System.out.print(i.value+"->");
}
System.out.println("\n\n/////////// Sorting////////");
int[] arr8={1,2,5,3,8};
Mylist m8=new Mylist(arr8);
m8.sorting();
for (Node i = m8.head; i!=null; i=i.next) {
System.out.print(i.value+"->");
}
System.out.println("\n\n/////////// Addition////////");
int[] arr9={1,2,3,5,8};
Mylist m9=new Mylist(arr9);
m8.addition();
System.out.println("\n/////////// Rotation ////////");
int[] arr10={3,2,5,1,8};
Mylist m10=new Mylist(arr10);
m9.rotation(2);
for (Node i = m10.head; i!=null; i=i.next) {
System.out.print(i.value+"->");
}
}
}
class Node {
int value;
Node next;
public Node(int v, Node n) {
value = v;
next = n;
}
}
class Mylist {
Node head = null;
//////////////////////////////////// Task 02 ////////////////////////////////////
public Mylist(int[] a) {
Node tail = null;
if (a.length == 0) {
System.out.println("Array cannot be empty");
} else {
for (int i = 0; i < a.length; i++) {
boolean result = true;
Node n = head;
while (n != null) {
if (n.value == a[i]) {
result = false;
break;
}
n = n.next;
}
if (result == true) {
Node temp = new Node(a[i], null);
if (head == null) {
head = temp;
tail = temp;
} else {
tail.next = temp;
tail = temp;
}
}
}
}
}
void ShowList() {
int count = 0;
for (Node n = head; n != null; n = n.next) {
count++;
System.out.println(n.value);
}
if (count == 0) {
System.out.println("Empty list");
}
}
void isempty() {
int count = 0;
for (Node n = head; n != null; n = n.next) {
count++;
}
boolean isempty;
if (count == 0) {
isempty = true;
} else {
isempty = false;
}
System.out.println(isempty);
}
void clear() {
int count = 0;
int count_2 = 0;
for (Node n = head; n != null; n = n.next) {
count_2++;
if (head != null) {
head = null;
for (Node j = head; j != null; j = j.next) {
count++;
}
}
}
if (count_2 == 0) {
System.out.println("List is empty");
} else {
if (count == 0) {
System.out.println("Elements removed");
}
}
}
void insert(Node newelement) {
int count = 0;
Node result = null;
for (Node n = head; n != null; n = n.next) {
result = n;
count++;
}
Node tail = result;
int index = count;
Node newNode = new Node(newelement.value, null);
if (index == count) {
tail.next = newNode;
tail = newNode;
}
}
void insertindex(Node newelement,int index){
Node newnode=new Node(newelement.value,null);
if(head!=null) {
Node temp = head;
int step =1;
while (temp!=null && step<index){
temp=temp.next;
step++;
}
newnode.next=temp.next;
temp.next=newnode;
}
}
void remove(int deletekey){
if(head==null){
System.out.println("Empty list");
}
else {
if(deletekey==head.value){
head=head.next;
}
}
Node pred=head;
while (pred.next!=null){
if(deletekey==pred.next.value){
break;
}
pred=pred.next;
}
if(pred.next==null){
System.out.println("Null");
}
else {
pred.next=pred.next.next;
}
}
//////////////////////////////////// Task 03 ////////////////////////////////////
void evennumbers(){
for(Node n =head;n!=null;n=n.next){
if(n.value%2==0){
System.out.println(n.value);
}
}
}
void availablity(int c){
Node current = head;
while (current!=null){
if (current.value==c){
System.out.println(true);
}
current=current.next;
}
}
void reverselist(){
Node prev=null;
Node current = head;
Node next=null;
while (current!=null){
next=current.next;
current.next=prev;
prev=current;
current=next;
}
head=prev;
}
void sorting(){
Node current = head;
Node serial=null;
int temp;
while(current != null) {
serial = current.next;
while(serial != null) {
if(current.value > serial.value) {
temp = current.value;
current.value = serial.value;
serial.value = temp;
}
serial = serial.next;
}
current = current.next;
}
}
void addition(){
int sum=0;
for(Node n =head;n!=null;n=n.next){
sum+=n.value;
}
System.out.println(sum);
}
void rotation(int k){
int step = 1;
Node current = head;
while (k>step){
current=current.next;
step++;
}
Node temp=current;
while (current.next!=null){
current=current.next;
}
current.next=head;
head=temp.next;
temp.next=null;
}
}