-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
293 lines (276 loc) · 8.88 KB
/
LinkedList.java
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
import java.util.Scanner;
class Node {
protected int regd_no;
protected float mark;
protected Node next;
public Node(int regd_no, float mark) {
this.regd_no = regd_no;
this.mark = mark;
this.next = null;
}}
class LinkedList {
static Node start;
static Scanner sc = new Scanner(System.in);
public static Node create() {
System.out.print("Enter registration number: ");
int regd_no = sc.nextInt();
System.out.print("Enter mark: ");
float mark = sc.nextFloat();
return new Node(regd_no, mark);
}
public static void display(Node start) {
Node temp = start;
while (temp != null) {
System.out.print("Registration Number: " + temp.regd_no + ", Mark: " + temp.mark+"->");
temp = temp.next;
}
System.out.println("null");
}
//a) insertion
//i. At begining
public static Node InsBeg(Node start) {
Node newNode = create();
newNode.next = start;
start = newNode;
return start;
}
//ii.At end
public static Node InsEnd(Node start) {
Node newNode = create();
if (start == null) {
start = newNode;
} else {
Node temp = start;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
return start;
}
//At any index
public static Node InsAny(Node start) {
Node newNode = create();
System.out.print("Enter position: ");
int position = sc.nextInt();
if (position == 1) {
newNode.next = start;
start = newNode;
} else {
Node temp = start;
while(newNode.next!=null)
temp = temp.next;
if (temp != null) {
newNode.next = temp.next;
temp.next = newNode;
} else {
System.out.println("The previous node is null.");
}
}
return start;
}
// deletion
//i. At beginning
public static Node DelBeg(Node start) {
if (start == null) {
System.out.println("The list is empty.");
} else {
start = start.next;
}
return start;
}
//ii. From the end of the list
public static Node DelEnd(Node start) {
if (start == null) {
System.out.println("The list is empty.");
} else if (start.next == null) {
start = null;
} else {
Node temp = start;
while (temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
}
return start;
}
//iii.From any position in the list
public static Node DelAny(Node start) {
if (start == null)
System.out.println("The list is empty.");
else {
System.out.print("Enter position: ");
int position = sc.nextInt();
if (position == 1) {
start = start.next;
}
else {
Node temp = start;
while(temp.next!=null){
temp = temp.next;
}
if (temp != null && temp.next != null)
temp.next = temp.next.next;
else
System.out.println("The position does not exist.");
} }
return start;
}
//iv.Deleting a node based on student regd_no. If the specified node is not present
public static Node DelRegdNo(Node start) {
if (start == null) {
System.out.println("The list is empty.");
return start;
}
System.out.print("Enter registration number to delete: ");
int regd_no = sc.nextInt();
// If the node to be deleted is the first node
if (start.regd_no == regd_no) {
start = start.next;
System.out.println("Node deleted successfully.");
return start;
}
// If the node to be deleted is in the middle or last
Node temp = start;
while (temp.next != null && temp.next.regd_no != regd_no) {
temp = temp.next;
}
// If the node to be deleted was found
if (temp.next != null) {
temp.next = temp.next.next;
System.out.println("Node deleted successfully.");
} else {
System.out.println("Student with registration number " + regd_no + " not found.");
}
return start;
}
//c) Search a node based on student regd_no and update the mark of the student.
public static void search(Node start) {
if (start == null) {
System.out.println("The list is empty.");
return;
}
System.out.print("Enter registration number to search: ");
int regd_no = sc.nextInt();
Node temp = start;
while (temp != null) {
if (temp.regd_no == regd_no) {
System.out.print("Enter new mark: ");
float new_mark = sc.nextFloat();
temp.mark = new_mark;
System.out.println("Mark updated successfully.");
return;
}
temp = temp.next;
}
System.out.println("Student with registration number " + regd_no + " not found.");
}
//d) Sort the nodes of the linked list according to the mark secured by the student from
public static void sort(Node start) {
if (start == null || start.next == null) {
return;
}
for (Node i = start; i.next != null; i = i.next) {
for (Node j = i.next; j != null; j = j.next) {
if (i.mark < j.mark) {
float tempMark = i.mark;
int tempRegdNo = i.regd_no;
i.mark = j.mark;
i.regd_no = j.regd_no;
j.mark = tempMark;
j.regd_no = tempRegdNo;
}
}
}
}
//e) Count the number of nodes present in the linked list
public static int count(Node start) {
int count = 0;
Node temp = start;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
//f)Reverse the linked list
public static Node reverse(Node start) {
Node prev = null;
Node current = start;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
start = prev;
return start;
}
public static void main(String[] args) {
int choice = 0;
while (choice != 13) {
System.out.println("1. Insert at beginning");
System.out.println("2. Insert at end");
System.out.println("3. Insert at any position");
System.out.println("4. Delete from beginning");
System.out.println("5. Delete from end");
System.out.println("6. Delete from any position");
System.out.println("7. Delete a node based on student registration no");
System.out.println("8. Search and update mark");
System.out.println("9. Sort the nodes(according to higher to lower mark)");
System.out.println("10. Count number of Nodes");
System.out.println("11. Reverse the LinkedList");
System.out.println("12. Display list");
System.out.println("13. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
start = InsBeg(start);
break;
case 2:
start = InsEnd(start);
break;
case 3:
start = InsAny(start);
break;
case 4:
start = DelBeg(start);
break;
case 5:
start = DelEnd(start);
break;
case 6:
start = DelAny(start);
break;
case 7:
start = DelRegdNo(start);
break;
case 8:
search(start);
break;
case 9:
sort(start);
break;
case 10:
System.out.println("The number of nodes: "+count(start));
break;
case 11:
start = reverse(start);
System.out.println("List after reversing:");
display(start);
break;
case 12:
display(start);
break;
case 13:
System.out.println("Exiting...");
System.exit(choice);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}