-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlla.java
More file actions
309 lines (305 loc) · 5.62 KB
/
Copy pathlla.java
File metadata and controls
309 lines (305 loc) · 5.62 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
import java.util.*;
class LL
{
static Node head=null;
static class Node{
int data;
Node next;
Node(int d){
this.data=d;
}
}
public static LL insert(LL l,int d){
Node newnode=new Node(d);
newnode.next=null;
Node ptr=l.head;
if(l.head==null){
l.head=newnode;
}
else{
ptr=l.head;
while(ptr.next!=null){
ptr=ptr.next;
}
ptr.next=newnode;
}
return l;
}
public static void printList(LL l){
Node ptr1=l.head;
while (ptr1!=null){
System.out.println(ptr1.data);
ptr1=ptr1.next;
}
}
public static int count_node(LL l)
{
int count=0;
Node ptr=l.head;
while(ptr!=null)
{
count++;
ptr=ptr.next;
}
return count;
}
public static void mid_element(LL l,int a){
int b=0;
Node ptr=l.head;
while(b!=a/2){
ptr=ptr.next;
b++;
}
System.out.println(ptr.data);
}
public static void mid_elementt(LL l){
Node ptr=l.head;
Node ptr1=l.head;
while(ptr1.next!=null&&ptr1.next.next!=null){
ptr=ptr.next;
ptr1=ptr1.next.next;
}
System.out.println(ptr.data);
}
public static void search(LL l, int n){
Node ptr=l.head;
int j=1;
while(ptr.data!=n){
j++;
ptr=ptr.next;
}
System.out.println("Elemnent found At pos"+j);
}
public static LL insert_beg(LL l,int d)
{
Node newnode=new Node(d);
newnode.data=d;
newnode.next=l.head;
l.head=newnode;
return l;
}
public static LL insert_kpos(LL l,int d,int e)
{
Node ptr=new Node(e);
ptr.next=null;
Node prev=l.head;
if(d==0){
insert(l,e);
}
else{
for(int i=0;i<d-1;i++){
prev=prev.next;
}
ptr.next=prev.next;
prev.next=ptr;
}
return l;
}
public static LL delete(LL l, int d){
Node ptr=l.head;
Node n=null;
if(ptr!=null&&ptr.data==d){
l.head=ptr.next;
return l;
}
else{
while(ptr!=null&&ptr.data!=d){
n=ptr;
ptr=ptr.next;
if(ptr==null){
return l;
}
else{
n.next=ptr.next;
}
}
}
return l;
}
public static LL move_last(LL l){
Node ptr=l.head;
Node prev=null;
int temp;
if(head==null){
System.out.println("List is empty");
}
else if(head.next==null){
return l;
}
else{
while(ptr.next!=null){
prev=ptr;
ptr=ptr.next;
}
prev.next=null;
ptr.next=l.head;
head=ptr;
}
return l;
}
public static LL delete_kpos_last(LL l,int n){
Node ptr=l.head;
int a=0;
if(head==null){
System.out.println("List is empty");
}
else{
while(a!=n-2){
ptr=ptr.next;
a++;
}
if(ptr.next==null){
}
Node b=ptr.next.next;
ptr.next=b;
}
return l;
}
public static LL reverse(LL l){
Node current=l.head;
Node prev=null;
Node nextt=null;
while(current!=null){
nextt=current.next;
current.next=prev;
prev=current;
current=nextt;
}
l.head=prev;
return l;
}
// public static Node reverserecur(Node l){
// Node ptr;
// if(l.next==null){
// return l;
// }
// ptr=reverserecur(l.next);
// l.next.next=ptr;
// ptr.next=null;
// return ptr;
// }
// public static LL reverseRecursively(LL l){
// Node headd =l.head;
// headd=reverserecur(headd);
// return l;
// }
public static boolean isPalindrome(Node head) {
if(head == null){
return true;
}
Node p = head;
Node prev = new Node(head.data);
while(p.next != null){
Node temp = new Node(p.next.data);
temp.next = prev;
prev = temp;
p = p.next;
}
Node p1 = head;
Node p2 = prev;
while(p1!=null){
if(p1.data != p2.data)
return false;
p1 = p1.next;
p2 = p2.next;
}
return true;
}
public static void Pall(LL l){
boolean b=isPalindrome(l.head);
System.out.println(b);
}
}
class lla
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
boolean v=true;
LL obj1=new LL();
while(v){
System.out.println("enter the function to perform \n1:to insert\n2:to print mid element\n3:to count no of nodes\n4:to printlist \n5:to print mid after counting nodes\n6:to search element\n7:to insert in begining\n8:to insert at k position\n9:to delete\n10:move last element to first\n11:to delete at particular pos from back\n12:reverse a list\n13:reverse a list using recursion\n14:to check pallindrome");
int z=s.nextInt();
switch(z)
{
case 1:{
int a=s.nextInt();
obj1.insert(obj1,a);
break;
}
case 2:{
obj1.mid_elementt(obj1);
break;
}
case 3:{
int a= obj1.count_node(obj1);
System.out.println(a);
break;
}
case 4:{
obj1.printList(obj1);
break;
}
case 5:{
int a= obj1.count_node(obj1);
obj1.mid_element(obj1 ,a);
break;
}
case 6:{
System.out.println("Enter element to search");
int n=s.nextInt();
obj1.search(obj1,n);
break;
}
case 7:{
int c=s.nextInt();
obj1.insert_beg(obj1,c);
break;
}
case 8:{
System.out.println("enter the pos at which data to added");
int d=s.nextInt();
System.out.println("enter the data");
int f=s.nextInt();
obj1.insert_kpos(obj1,d,f);
break;
}
case 9:{
System.out.println("enter the data to delete");
int r=s.nextInt();
obj1.delete(obj1,r);
break;
}
case 10:{
obj1.move_last(obj1);
obj1.printList(obj1);
break;
}
case 11:{
System.out.println("Enter the position");
int n=s.nextInt();
obj1.delete_kpos_last(obj1,n);
break;
}
case 12:{
obj1.reverse(obj1);
break;
}
case 13:{
//obj1.reverseRecursively(obj1);
//System.out.println(obj1);
break;
}
case 14:{
obj1.Pall(obj1);
break;
}
default:{
System.out.println("GOOD BYE");
v=false;
break;
}
}
}
}
}