-
Notifications
You must be signed in to change notification settings - Fork 2
/
linkedlist.java
220 lines (219 loc) · 5.21 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
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
import java.util.*;
class linkedlist
{
public static void main()
{
Scanner sc=new Scanner(System.in);
linkedlist list=new linkedlist();
boolean flag=true;
int p;
while(flag)
{
int pos;
int n;
System.out.println();
System.out.println("1. Add item to the list at starting");
System.out.println("2. Add item to the list at the end");
System.out.println("3. Add item to the list at any position");
System.out.println("4. delete first node");
System.out.println("5. delete last node");
System.out.println("6. delete the node at any pos");
System.out.println("7. view size of the list");
System.out.println("8. view list");
System.out.println("9. exit");
System.out.print("enter your choice: ");
System.out.println();
p=sc.nextInt();
switch(p)
{
case 1:System.out.print("enter the value of the item: ");
n=sc.nextInt();
list.insertatfirst(n);
break;
case 2:System.out.print("enter the value of the item: ");
n=sc.nextInt();
list.insertatlast(n);
break;
case 3:System.out.print("enter the value of the item: ");
n=sc.nextInt();
System.out.print("enter the position where you want to insert a node: ");
pos=sc.nextInt();
list.insertatpos(n,pos);
break;
case 4:list.deletefirst();
break;
case 5:list.deletelast();
break;
case 6:System.out.print("enter the position of the node to be deleted");
pos=sc.nextInt();
list.deleteatpos(pos);
break;
case 7:int q=list.getsize();
System.out.println("the size of the list is: "+q);
break;
case 8:list.viewlist();
break;
case 9:flag=false;
}
}
}
}
class node
{
private int data;
private node next;
public node()
{
data=0;
next=null;
}
public node(int d, node n)
{
data=d;
next=n;
}
public void setdata(int d)
{
data=d;
}
public void setnext(node n)
{
next=n;
}
public int getdata()
{
return data;
}
public node getnext()
{
return next;
}
}
class linkedlist
{
private int size;
private node start;
public void insertatfirst(int val)
{
node n=new node();
n.setdata(val);
n.setnext(start);
start=n;
size++;
}
public void insertatlast(int val)
{
node t;
node n=new node();
n.setdata(val);
t=start;
if(t==null)
{
start=n;
}
else
{
while(t.getnext()!=null)
t=t.getnext();
t.setnext(n);
}
size++;
}
public void insertatpos(int val, int pos)
{
if(pos==1)
{
insertatfirst(val);
}
else if(pos==size+1)
{
insertatlast(val);
}
else if(pos>1&&pos<=size)
{
node t;
node n=new node(val,null);
t=start;
for(int i=1;i<pos-1;i++)
t=t.getnext();
n.setnext(t.getnext());
t.setnext(n);
size++;
}
else
System.out.println("the element can't be inserted");
}
public void deletefirst()
{
if(start==null)
System.out.println("the list is already empty");
else
{
start=start.getnext();
size--;
}
}
public boolean isempty()
{
if(start==null)
return true;
else
return false;
}
public void deletelast()
{
if(start==null)
System.out.println("the list is already empty");
else if(size==1)
{
start=null;
size--;
}
else
{
node t=start;
for(int i=1;i<size-1;i++)
t=t.getnext();
t.setnext(null);
size--;
}
}
public void deleteatpos(int pos)
{
if(pos==1)
deletefirst();
else if(pos==size)
deletelast();
else if(pos<1||pos>size)
System.out.println("invalid position");
else
{
node t=start;
node t1;
for(int i=1;i<pos-1;i++)
t=t.getnext();
t1=t.getnext();
t.setnext(t1.getnext());
size--;
}
}
public int getsize()
{
return size;
}
public void viewlist()
{
node t;
if(isempty()==true)
System.out.println("the list is empty");
else
{
t=start;
while(t!=null)
{
System.out.print(" "+t.getdata());
t=t.getnext();
}
}
}
}