-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.java
122 lines (121 loc) · 3.19 KB
/
linkedlist.java
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
public class MyClass {
public listnode head;
public static class listnode{
public int data;
public listnode next;
public listnode(int data)
{
this.data=data;
this.next=null;
}
}
public void display()
{
int size=0;
listnode current=head;
while(current!=null)
{
System.out.print(current.data+"->");
size++;
current=current.next;
}
System.out.print("null");
System.out.println("");
System.out.println(size);
}
public void insertatbeg(int data)
{
listnode start=new listnode(data);
start.next=head;
head=start;
}
public void insertatend(int data)
{
listnode end=new listnode(data);
listnode temp=head;
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=end;
}
public void deleteatbeg()
{
listnode temp=head.next;
head=temp;
}
public void deleteatend()
{
listnode current=head;
listnode prev=null;
while(current.next!=null)
{
prev=current;
current=current.next;
}
prev.next=null;
}
public void insertAt(int index,int data)
{
listnode node= new listnode(data);
listnode n=head;
for(int i=0;i<index-1;i++)
{
n=n.next;
}
node.next=n.next;
n.next=node;
}
public void deleteAt(int index)
{
}
public void reverse()
{
listnode current=head;
listnode prev=null;
listnode next=null;
while(current!=null)
{
next=current.next;
current.next=prev;
prev=current;
current=next;
}
}
public static void main(String args[]) {
MyClass m =new MyClass();
m.head=new listnode(10);
listnode s1=new listnode(20);
listnode s2=new listnode(30);
listnode s3=new listnode(40);
listnode s4=new listnode(50);
m.head.next=s1;
s1.next=s2;
s2.next=s3;
s3.next=s4;
// m.insertatbeg(67);
//m.insertatbeg(627);
//m.insertatbeg(673);
//m.insertatend(987);
//m.insertatend(12);
//m.deleteatbeg();
//m.deleteatbeg();
//m.deleteatend();
//m.deleteatend();
// m.reverse();
m.insertAt(2,76);
m.display();
}
}