forked from sukritishah15/DS-Algo-Point
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion and deletion
246 lines (222 loc) · 4.28 KB
/
insertion and deletion
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
package duo;
import java.util.*;
public class linkedlist {
static class Node{
int data;
Node next;
public Node(int data)
{
this.data=data;
this.next=null;
}
}
public Node head=null;
public Node tail=null;
public int size;
//creating node
void addnode(int data)
{
Node node = new Node(data);
//checking
if(head==null)
{
head=node;
tail=node;
}
else
{
tail.next=node;
tail=node;
}
size++;
}
//inserting in front of node
void addhead(int data)
{
Node node = new Node(data);
//checking
if(head==null)
{
head=node;
tail=node;
}
else
{
Node temp=head;
head=node;
head.next=temp;
}
}
//adding in tail
void addtail(int data)
{
Node node = new Node(data);
//checking
if(head==null)
{
head=node;
tail=node;
}
else
{
Node temp=head;
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=node;
}
}
//deleting from head
void deletehead()
{
Node temp=head;
head=temp.next;
}
//deleting from tail
void deletetail()
{
Node temp=head;
while(temp.next!=tail)
{
temp=temp.next;
}
temp.next=null;
tail=temp;
}
//inserting in middle position
void insertAtMiddle(int data)
{
Node node = new Node(data);
if(head==null)
{
head=node;
tail=node;
}
else
{
Node temp,current;
int count = (size%2==0)?(size/2):((size+1)/2);
temp=head;
current=null;
//looping
for(int i=0;i<count;i++)
{
current=temp;
temp=temp.next;
}
current.next=node;
node.next=temp;
}
size++;
}
//deleting from middle
void deleteFromMiddle()
{
Node n, temp;
int count = (size%2==0)?(size/2):((size+1)/2);
n=head;
temp=head;
//loopint till count
for(int i=0;i<count-1;i++)
{
n=n.next;
temp=temp.next;
}
temp=n.next;
n.next=temp.next;
temp=null;
}
//deleting from index
void deleteFromIndex(int index)
{
Node n=head;
Node temp=null;
for(int i=0;i<index-1;i++)
{
n=n.next;
}
temp=n.next;
n.next=temp.next;
temp=null;
}
//inserting at given index
void insertAt(int index, int data)
{
Node node = new Node(data);
//checking
if(head==null)
{
head=node;
tail=node;
}
else
{
Node n;
n=head;
for(int i=0;i<index-1;i++)
{
n=n.next;
}
node.next=n.next;
n.next=node;
}
}
//display method
void display()
{
Node current=head;
if(head==null)
{
System.out.println("list is empty");
return;
}
while(current!=null)
{
System.out.print(current.data+" ");
current=current.next;
}
}
public static void main(String[] args) {
linkedlist list = new linkedlist();
list.addnode(10);
list.addnode(11);
list.addnode(12);
list.addnode(13);
System.out.println("Orginal data : ");
list.display();
System.out.println();
System.out.println("Deleting tail which is 13 : ");
list.deletetail();
list.display();
System.out.println();
System.out.println("Adding 5 in head position : ");
list.addhead(5);
list.display();
System.out.println();
System.out.println("Adding 6 in tail position : ");
list.addtail(6);
list.display();
System.out.println();
System.out.println("deleting head which is 5 : ");
list.deletehead();
list.display();
System.out.println();
System.out.println("Inserting 55 in middle which is in 12 position : ");
list.insertAtMiddle(55);
list.display();
System.out.println();
System.out.println("Deleting from middle which is 12 in this case : ");
list.deleteFromMiddle();
list.display();
System.out.println();
System.out.println("Inserting 35 at index 2 : ");
list.insertAt(2, 35);
list.display();
System.out.println();
System.out.println("Deleting at index 4 which is 6 : ");
list.deleteFromIndex(4);
list.display();
System.out.println();
}
}