-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.java
More file actions
230 lines (157 loc) · 4.42 KB
/
linkedlist.java
File metadata and controls
230 lines (157 loc) · 4.42 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
public class linkedlist {
Node head; //first element of the list
static class Node {
int data; //data and pointer to next node indicated as data and next
Node next;
Node(int d){
data=d;
next = null;
}
public Node(int position, int new_value) {
// TODO Auto-generated constructor stub
}
}
//PRINT THE LIST.....!!!!!!!!!!!!!!!!!!!!!!!
public void printlist()
{
Node n= head;
while(n!=null){
System.out.println(n.data);
n= n.next;
}
}
// Insert data at the starting ie. changing head value
public void Insertatbeginning(int head_data){
Node new_root_data= new Node(head_data);
new_root_data.next= head;
head= new_root_data;
}
//Insert just after the next node of head...
public void InsertAfter(Node prev_node, int new_data){
if(prev_node==null){
System.out.println("Previous values cannnot be null");
return;
}
Node data= new Node(new_data);
data.next= prev_node.next;
prev_node.next= data;
}
// insert at the end that is creating new tail
public void InsertEnd(int new_data){
Node data1= new Node(new_data);
if(head==null){
head= new Node(new_data);
return;
}
data1.next=null;
Node Last=head;
while(Last.next!=null)
Last=Last.next;
Last.next= data1;
return;
}
public void insertatPosition(int position,int new_value){
Node data1= new Node(position,new_value);
// Node data2= new Node(new_value);
data1.data=new_value;
data1.next=null;
Node tmp=head;
if(tmp==null && position!=0){
return; }
else if (tmp ==null && position==0)
{
head.next=head;
return;
}
if(position==0){
data1.next= head;
head= data1;
return;
}
Node prev= null;
Node current= head;
int i=0;
while(current!=null && i<position){
prev= current;
current= current.next;
i++;
}
data1.next = prev.next;
prev.next = data1;
return;
}
public void deleteelement(int key){
Node temp= head, previous=null;
//case1: when head itself hold the key
if(temp!=null &&temp.data==key){
head= temp.next;
return;
}
// case2 when head do not hold key
while(temp!=null && temp.data!=key){
previous= temp;
temp=temp.next;
}
// case3 whne key was not in the list
if(temp==null)
return;
previous.next=temp.next;
}
public void deletefromposition(int position){
if(head==null)
return;
Node temp= head;
if(position==0){
head=temp.next;
return;
}
for(int i=0; temp!=null && i<position-1;i++)
temp=temp.next;
if(temp==null || temp.next==null )
return;
Node next= temp.next.next;
temp.next= next;
}
public static void main(String args[]){
linkedlist l= new linkedlist();
System.out.println("Created Linked list:");
l.head= new Node(1);
Node second= new Node(2);
Node third= new Node(3);
Node fourth= new Node(12);
l.head.next=second;
second.next=third;
third.next= fourth;
l.printlist();
System.out.println("After inserting an element at beginning:");
l.Insertatbeginning(144);
l.printlist();
System.out.println("After inserting an element after head:");
l.InsertAfter(l.head.next, 33);
l.InsertAfter(l.head.next, 35);
l.printlist();
System.out.println("After inserting an element at end:");
l.InsertEnd(222);
l.printlist();
System.out.println("Delete operation:");
l.deleteelement(33);
l.deleteelement(222);
l.deleteelement(1);
l.printlist();
System.out.println("Delete from the specific position:");
l.deletefromposition(2);
l.printlist();
System.out.println();
l.deletefromposition(3);
l.printlist();
System.out.println("Insert at specific position:");
l.insertatPosition(0, 101);
l.printlist();
System.out.println();
l.insertatPosition(0, 111);
l.printlist();
System.out.println("Next Insertion result:");
l.insertatPosition(3, 1888);
l.printlist();
}
}