-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.java
More file actions
217 lines (214 loc) · 5.51 KB
/
List.java
File metadata and controls
217 lines (214 loc) · 5.51 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
import java.util.*;
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 int getData()
{
return(data);
}
public Node getNext()
{
return(next);
}
public void setData(int d)
{
data =d;
}
public void setNext(Node n)
{
next=n;
}
}
class LinkList
{
private int size;
private Node start;
Scanner sc = new Scanner(System.in);
public LinkList()
{
size=0;
start=null;
}
public void InsertAtFirst()
{
System.out.println("Enter Value to Insert");
int val = sc.nextInt();
Node n = new Node();
n.setData(val);
n.setNext(start);
start=n;
size++;
System.out.println("Value Inserted Sucessfully.");
}
public void InsertAtLast()
{
System.out.println("Enter Value to Insert");
int val = sc.nextInt();
Node n = new Node();
Node n1 = new Node();
n.setData(val);
n1=start;
if(n1==null)
start=n;
else
{
while(n1.getNext()!=null)
n1=n1.getNext();
n1.setNext(n);
}
size++;
System.out.println("Value Inserted Sucessfully.");
}
public void InsertAtPosition()
{
System.out.println("Enter Position where Insert Value ");
int pos = sc.nextInt();
if(pos==1)
InsertAtFirst();
else if(pos==size+1)
InsertAtLast();
else if(pos<1&&pos<=size)
{
System.out.println("Enter Value to Insert");
int val = sc.nextInt();
Node n = new Node();
Node n1 = start;
n.setData(val);
for(int i=1;i<pos-1;i++)
n1=n1.getNext();
n.setNext(n1.getNext());
n1.setNext(n);
size++;
System.out.println("Value Inserted Sucessfully.");
}
else
System.out.println("Insert is not possible at position "+pos);
}
public void DeleteAtFirst()
{
if(start==null)
System.out.println("List is Already Empty.");
else{
start = start.getNext();
size--;
}
System.out.println("Value Deleted Sucessfully.");
}
public void DeleteAtLast()
{
if(start==null)
System.out.println("List is Already Empty.");
else if(size==1){
start=null;
size--;
}
else{
Node n = start;
for(int i=1;i<size-1;i++)
n=n.getNext();
n.setNext(null);
size--;
}
System.out.println("Value Deleted Sucessfully.");
}
public void DeleteAtPosition()
{
System.out.println("Enter Position where Delete Value ");
int pos = sc.nextInt();
if(start==null)
System.out.println("List is Already Empty.");
else if(pos==1)
DeleteAtFirst();
else if(pos==size)
DeleteAtLast();
else if(pos>1&&pos<=size-1)
{
Node n = start;
for(int i=1;i<=pos-1;i++)
n=n.getNext();
Node n1 = n.getNext();
n.setNext(n1.getNext());
size--;
System.out.println("Value Deleted Sucessfully.");
}
else
System.out.println("Position not found.");
}
public void ViewList()
{
if(start==null)
System.out.println("List is Empty.");
else
{
Node n = start;
for(int i=1;i<=size;i++)
{
System.out.print(" "+n.getData());
n=n.getNext();
}
}
}
}
public class List
{
public static void main(String[] args)
{
LinkList l = new LinkList();
Scanner sc = new Scanner(System.in);
boolean b = true;
while(b)
{
System.out.println("\n1: Add Value At First position");
System.out.println("2: Add Value At Last Position");
System.out.println("3: Add Value At Fixed Position");
System.out.println("4: Delete Value From Frist Position");
System.out.println("5: Delete Value From Last Position");
System.out.println("6: Delete Value From Fixted Position");
System.out.println("7: View Data");
System.out.println("8: Exit");
System.out.println("Enter Your Choice");
int choice = sc.nextInt();
switch(choice)
{
case 1:
l.InsertAtFirst();
break;
case 2:
l.InsertAtLast();
break;
case 3:
l.InsertAtPosition();
break;
case 4:
l.DeleteAtFirst();
break;
case 5:
l.DeleteAtLast();
break;
case 6:
l.DeleteAtPosition();
break;
case 7:
l.ViewList();
break;
case 8:
b=false;
System.out.println("Program Closed.");
break;
default :
System.out.println("Enter Valid Choice Between 1 to 8.");
}
}
}
}