-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedlist insertion.java
More file actions
119 lines (87 loc) · 1.95 KB
/
Copy pathLinkedlist insertion.java
File metadata and controls
119 lines (87 loc) · 1.95 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
public class Linkedl{
//Linked list implemetation
class Node
{
int data;
Node next;
Node(int dat)
{
data=dat;
next=null;
}
}
Node head;
Linkedl()
{
head=null;
}
public void insertbeg(int dat)
{
if(head==null)
{
this.head= new Node(dat);
return ;
}
Node pt=new Node(dat);
pt.next=head;
head=pt;
//return 0;
}
public void insertafter(int pos,int dat)
{
Node x=this.head;
for(int i=0;i<pos-1;i++)
{
x=x.next;
if(x==null)
{
System.out.println("Sorry beyond the linked list");
return;
}
}
// System.out.println("this is x "+x.data);
Node z=x.next;
Node y=new Node(dat);
x.next=y;
y.next=z;
}
public void insertbef(int pos,int dat)
{
Node f= new Node(dat);
Node d,e;
if(pos==1)
{
f.next=head;
head=f;
return;
}
e=d=this.head;
for(int i=0;i<pos-1;i++)
{
d=e;
e=e.next;
}
d.next=f;
f.next=e;
return;
}
public void plist()
{
Node te = this.head;
while(te!=null)
{
System.out.println(te.data);
te=te.next;
}
}
public static void main(String []args){
Linkedl list= new Linkedl();
list.insertbeg(22);
list.insertbeg(32);
list.insertbeg(42);
list.insertbeg(52);
list.insertafter(3,30);
list.insertbef(1,29);
list.plist();
}
}