-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLinkedList_GFG.java
More file actions
260 lines (221 loc) · 5.78 KB
/
LinkedList_GFG.java
File metadata and controls
260 lines (221 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
248
249
250
251
252
253
254
255
256
257
258
259
260
package DATA_STRUCTURE;
import java.util.Scanner;
public class LinkedList_GFG {
Node head;
//creation
static class Node{
int value;
Node next;
Node(int d){
value = d;
next = null;
}
}
//printing
public void printlist(){
Node n = head;
while (n!=null){
System.out.println(n.value+" ");
n = n.next;
}
}
//counting nodes
public int countNODE(){
Node c = head;
int count = 1;
while (c.next != null){
count++;
c = c.next;
}
System.out.println("Total number of Nodes : "+count);
return count;
}
//insertion at Start
public void add_start(int data){
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}
//insertion at end
public void add_end(int data){
Node new_Node = new Node(data);
if (head == null){
head = new Node(data);
return;
}
new_Node.next = null;
Node last = head;
while(last.next!=null){
last = last.next;
}
last.next = new_Node;
return;
}
//Insert anywhere
public void add_anywhere(int data,int p){
Node node = new Node(data);
node.next = null;
if(this.head == null){
if(p !=0){
return;
}else{ //when head is null
this.head = node;
}
}
if(head!=null && p == 0){
node.next = this.head; //when position is zero
this.head = node;
return;
}
Node current_node = this.head;
Node pre = null;
int i = 1;
while(i<p){
pre = current_node;
current_node = current_node.next;
if(current_node ==null){
break;
}
i++;
}
node.next = current_node;
pre.next = node;
}
//Delete Node from start
public void del_start(){
if (head == null){
return;
}
head = head.next;
}
//Delete Node from end
public void del_end(){
Node check = head;
if(check!=null){
while(check.next.next != null){
check = check.next;
}
check.next = null;
}
else{
System.out.println("underflow");
}
}
//Delete Node from a specific position
public void del_anywhere(int p){
Node current = head;
Node pre = null;
int i = 1;
if(current==null){
System.out.println("underflow");
return;
}
else if (current.next==null){
del_start();
}
else{
while(i<p){
pre = current;
current = current.next;
if(current == null){
break;
}
i++;
}
if (pre != null) {
pre.next = pre.next.next;
}
}
}
public void search(int d){
int p = 0;
Node n = head;
while(n!=null){
p++;
if(d == n.value){
System.out.println(d+" is in Node "+p);
break;
}
n = n.next;
}
}
public void reverseList(int count){
if (count>=2){
Node p = null,q = head,r = head.next;
q.next = null;
while (r!=null){
p=q;
q=r;
r=r.next;
q.next=p;
}
head = q;
}
}
public void sort(){
Node current = head;
Node index = null;
int temp;
if (head == null){
return;
}
else{
while(current != null){
index = current.next;
while(index != null){
if(current.value > index.value){
temp = current.value;
current.value = index.value;
index.value = temp;
}
index = index.next;
}
current = current.next;
}
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
LinkedList_GFG list = new LinkedList_GFG();
// list.head = new Node(1);
// Node two = new Node(2);//creating Nodes
// Node three = new Node(3);
// Node four = new Node(4);
//Linking nodes;
//list.head.next = two;// 1 --> 2
//two.next = three;//1 --> 2 --> 3
//three.next = four;//1 --> 2 --> 3 -->4 -->null
list.add_end(1);
list.add_end(2);
list.add_end(5);
list.add_end(4);
list.add_end(3);
list.add_end(37);
list.add_end(3);
list.add_anywhere(1000,4);
list.del_end();
list.del_start();
// list.reverseList(list.countNODE());
// list.add_end(1);
// list.add_end(2);
// list.add_end(3);
// list.add_end(4);
// list.add_end(5);
// list.add_end(6);
// list.del_start();
// list.del_end();
// list.del_anywhere(4);
// list.del_anywhere(2);
// list.add_anywhere(5000,3);
// list.add_anywhere(4000,4);
// list.add_anywhere(1000,5);
list.sort();
// list.add_anywhere(123456, list.countNODE(),2);
list.printlist();//printing list
// list.search(3);
// list.countNODE();//counting Node
// list.reverseList(list.countNODE());//reversing
// list.printlist();//printing list
s.close();
}
}