-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListGui.java
More file actions
331 lines (268 loc) · 7.81 KB
/
ListGui.java
File metadata and controls
331 lines (268 loc) · 7.81 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//Author Name :- Sonu Kumar
//This code will help u understand how to use data Structure is implemented using java.
//I have creating GUI for Data Structure which will help understanding the concept of DS easily.
package udemy;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;
import javax.swing.*;
import udemy.win.closing;
class Manipulation extends JFrame implements ActionListener
{
//Creating list of JButtons
JButton b[] = new JButton[15];
LinkedList<Integer> llist= new LinkedList();
Manipulation()
{
//Setting Frame Name
super("Demo of LinkedList");
//Creating an object of buttons
b[0] = new JButton("Inset Element");
b[1] = new JButton("Insert in Postition");
b[4] = new JButton("Swap");
b[5] = new JButton("Remove a specified element");
b[7] = new JButton("display");
b[6] = new JButton("display element and position");
b[2] = new JButton("Display from a position");
b[3] = new JButton("Display Reverse");
b[8] = new JButton("Insert First");
b[9] = new JButton("Remove First");
b[10] = new JButton("Remove from Index");
b[11] = new JButton("Remove Last");
b[12] = new JButton("Searching if Element Exist");
//Setting the Frame layout as null
setLayout(null);
//Aligning the components for Insertion button
b[0].setBounds(10,10,230,20);
add(b[0]);
b[1].setBounds(250,10,230,20);
add(b[1]);
b[8].setBounds(10,45,230,20);
add(b[8]);
b[4].setBounds(250,45,230,20);
add(b[4]);
//Aligning the components for Remove button
b[5].setBounds(10,135,230,20);
add(b[5]);
b[9].setBounds(10,100,230,20);
add(b[9]);
b[10].setBounds(250,100,230,20);
add(b[10]);
b[11].setBounds(250,135,230,20);
add(b[11]);
//Aligning the components for delete button
b[7].setBounds(10,190,230,20);
add(b[7]);
b[3].setBounds(250,190,230,20);
add(b[3]);
b[6].setBounds(10,225,230,20);
add(b[6]);
b[2].setBounds(250,225,230,20);
add(b[2]);
b[12].setBounds(10,280,230,20);
add(b[12]);
//Adding ActionListener to all the buttons
b[0].addActionListener(this);
b[1].addActionListener(this);
b[2].addActionListener(this);
b[3].addActionListener(this);
b[4].addActionListener(this);
b[5].addActionListener(this);
b[6].addActionListener(this);
b[7].addActionListener(this);
b[8].addActionListener(this);
b[9].addActionListener(this);
b[10].addActionListener(this);
b[11].addActionListener(this);
b[12].addActionListener(this);
//Code for Terminating the frame
class ClosingGui extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
addWindowListener(new ClosingGui());
}
//code for Performing action on each button
public void actionPerformed(ActionEvent e)
{
//Code for insertion
if(e.getSource()==b[0])
{
String name;
name = JOptionPane.showInputDialog("Enter a number ");
int num=Integer.parseInt(name);
llist.add(num);
}
//Code for Insert in Position
else if(e.getSource()==b[1])
{
String name,pos;
name = JOptionPane.showInputDialog("Enter a number ");
pos = JOptionPane.showInputDialog("Enter a Position");
int ele=Integer.parseInt(name);
int ind = Integer.parseInt(pos);
if(ind > llist.size()-1)
{
JOptionPane.showMessageDialog(null,"Enter a valid position");
}
else
{
llist.add(ind, ele);
}
}
//code for Displaying from a position
else if(e.getSource()==b[2])
{
String pos2 = JOptionPane.showInputDialog("Enter a Position");
int ind = Integer.parseInt(pos2);
String str1 = " ";
if(ind > llist.size()-1)
{
JOptionPane.showMessageDialog(null,"Enter a valid position");
}
else
{
for(int i= ind;i<llist.size();i++)
{
str1 =str1+llist.get(i)+" ";
}
JOptionPane.showMessageDialog(null,str1);
}
}
//code for Displaying Reverse
else if(e.getSource()==b[3])
{
String str2 = " ";
for(int i= llist.size()-1;i>=0;i--)
{
str2 =str2 +llist.get(i)+" ";
}
JOptionPane.showMessageDialog(null,str2);
}
//code for Swap
else if(e.getSource()==b[4])
{
String pos1 = JOptionPane.showInputDialog("Enter a 1st Position");
String pos2 = JOptionPane.showInputDialog("Enter a 2nd Position");
int ind1 = Integer.parseInt(pos1);
int ind2 = Integer.parseInt(pos2);
if(ind1> llist.size()-1 || ind2> llist.size()-1 || ind1<=-1 || ind2<= -1)
{
JOptionPane.showMessageDialog(null,"Enter a valid Number");
}
else
{
LinkedList<Integer> dummtlist = new LinkedList();
dummtlist = (LinkedList) llist.clone();
Collections.swap(llist,ind1,ind2);
JOptionPane.showMessageDialog(null,"Befor Swapping"+"\n"+dummtlist+"\n"+"After Swapping"+"\n"+llist);
}
}
//code for Remove a specified element
else if(e.getSource()==b[5])
{
String pos1 = JOptionPane.showInputDialog("Enter the element");
int ind1 = Integer.parseInt(pos1);
for(int rem=0;rem<llist.size();rem++)
{
if(llist.get(rem)==ind1)
{
llist.remove(rem);
JOptionPane.showMessageDialog(null,"Item Successfully removed");
}
}
}
//code for Display position and element
else if(e.getSource()==b[6])
{
String str1="";
for(int i = 0; i<llist.size();i++)
{
str1 =str1+llist.get(i)+" at position "+i+"\n";
}
JOptionPane.showMessageDialog(null,str1);
}
//code for Display List
else if(e.getSource()==b[7])
{
JOptionPane.showMessageDialog(null,llist);
}
//code Insert First
else if(e.getSource()==b[8])
{
if(e.getSource()==b[8])
{
String name;
name = JOptionPane.showInputDialog("Enter a number ");
int num=Integer.parseInt(name);
llist.addFirst(num);
}
}
//code Remove First Element
else if(e.getSource()==b[9])
{
llist.removeFirst();
JOptionPane.showMessageDialog(null,"Successfully Removed First element");
}
//code for Remove From Index
else if(e.getSource()==b[10])
{
String pos1 = JOptionPane.showInputDialog("Enter the Position");
int ind1 = Integer.parseInt(pos1);
if(ind1> llist.size()-1 || ind1<=-1)
{
JOptionPane.showMessageDialog(null,"Enter a valid Number");
}
else
{
llist.remove(ind1);
JOptionPane.showMessageDialog(null,"Successfully Removed");
}
}
//code for Remove Last
else if(e.getSource() == b[11])
{
llist.remove();
JOptionPane.showMessageDialog(null,"Successfully Removed Last Element");
}
//code for Searching if Element Exist
else if(e.getSource()==b[12])
{
String pos1 = JOptionPane.showInputDialog("Enter the element to be searched");
int ind1 = Integer.parseInt(pos1);
// Initializing the answer to the index -1
int ans = -1;
// Traversing through the Linked List
for (int i = 0; i < llist.size(); i++) {
int llElement = llist.get(i);
if (llElement == ind1)
{
ans = i;
break;
}
}
if (ans == -1) {
JOptionPane.showMessageDialog(null,"Element not Found");
}
else {
JOptionPane.showMessageDialog(null,"Element found in Linked List at " + ans +" position");
}
}
}
}
public class ListGui
{
public static void main(String arge[])
{
Manipulation man = new Manipulation();
man.setSize(500,350);
man.setVisible(true);
man.setResizable(false);
}
}