forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink2.java
More file actions
248 lines (211 loc) · 4.29 KB
/
Copy pathlink2.java
File metadata and controls
248 lines (211 loc) · 4.29 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
import java.util.*;
class Node
{
int data;
Node next;
}
class LinkedList
{
private Node head;
int size=0;
LinkedList()
{
head=null;
}
public void addB(int data)
{
Node node=new Node();
if(head==null)
{
node.data=data;
node.next=null;
head=node;
}
else
{
node.data=data;
node.next=head;
head=node;
}
size++;
}
public void addL(int data)
{
Node node=new Node();
Node temp=head;
if(head==null)
{
node.data=data;
node.next=null;
head=node;
}
else
{
node.data=data;
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=node;
}
size++;
}
public void show()
{
if(head==null)
{
System.out.print("Emplty list");
}
else
{
Node temp=head;
while(temp!=null)
{
System.out.print(temp.data+" --> ");
temp = temp.next;
}
System.out.println(temp);
}
}
public void delB()
{
if(head==null)
{
}
else
{
Node temp=head;
head=head.next;
temp.next=null;
size--;
}
}
public void delL()
{
if(head==null)
{
}
else
{
Node temp=head;
Node tail=null;
while(temp.next!=null)
{
tail=temp;
temp=temp.next;
}
tail.next=null;
size--;
}
}
public void insertpos(int data,int pos)
{
if(pos==1)
{
addB(data);
}
else if(pos==size+1)
{
addL(data);
}
else if(pos>1 && pos<=size)
{
Node node=new Node();
node.data=data;
Node temp=head;
for(int i=1;i<pos-1;i++)
{
temp=temp.next;
}
node.next=temp.next;
temp.next=node;
size++;
}
else
{
System.out.println("Inserion not posibile");
}
}
public void delpos(int index)
{
if(index==1)
{
delB();
}
else if(index==size)
{
delL();
}
else if(index>1 && index<size)
{
Node temp=head;
for(int i=1;i<index-1;i++)
{
temp=temp.next;
}
Node current=temp.next;
temp.next=temp.next.next;
current.next=null;
}
}
public void search(int data)
{
Node temp=head;
int c=1;
int x=0;
while(temp!=null)
{
if(temp.data==data)
{
x++;
break;
}
else
{
temp=temp.next;
c++;
}
}
if(x>=1)
{
System.out.println("found " + data + " at pos "+ c);
}
else
{
System.out.println("not found " + data);
}
// for(int i=1;i<index;i++)
// {
// temp=temp.next;
// }
// System.out.println(temp.data);
}
}
public class link2
{
public static void main(String[] args)
{
LinkedList l1=new LinkedList();
l1.addL(1);
l1.addL(2);
l1.addL(3);
l1.addL(4);
//l1.addB(1);
//l1.addB(2);
// l1.show();
// l1.delB();
// System.out.println("after del front");
// l1.show();
// System.out.println(l1.size);
// l1.delL();
// System.out.println("after del back");
// //System.out.println(l1.size);
// l1.show();
l1.insertpos(0,1);
l1.show();
System.out.println(l1.size);
l1.delpos(1);
l1.show();
l1.search(40);
}
}