-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.java
266 lines (231 loc) · 5.56 KB
/
linked_list.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
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
class LinkedList {
public static void main(String[] a) {
System.out.println(new LL().Start());
}
}
class Element {
int Age;
int Salary;
boolean Married;
// Initialize some class variables
public boolean Init(int v_Age, int v_Salary, boolean v_Married) {
Age = v_Age;
Salary = v_Salary;
Married = v_Married;
return true;
}
public int GetAge() {
return Age;
}
public int GetSalary() {
return Salary;
}
public boolean GetMarried() {
return Married;
}
// This method returns true if the object "other"
// has the same values for age, salary and
public boolean Equal(Element other) {
boolean ret_val;
int aux01;
int aux02;
int nt;
ret_val = true;
aux01 = other.GetAge();
if (!this.Compare(aux01, Age)) ret_val = false;
else {
aux02 = other.GetSalary();
if (!this.Compare(aux02, Salary)) ret_val = false;
else if (Married)
if (!other.GetMarried()) ret_val = false;
else nt = 0;
else if (other.GetMarried()) ret_val = false;
else nt = 0;
}
return ret_val;
}
// This method compares two integers and
// returns true if they are equal and false
// otherwise
public boolean Compare(int num1, int num2) {
boolean retval;
int aux02;
retval = false;
aux02 = num2 + 1;
if (num1 < num2) retval = false;
else if (!(num1 < aux02)) retval = false;
else retval = true;
return retval;
}
}
class List {
Element elem;
List next;
boolean end;
// Initialize the node list as the last node
public boolean Init() {
end = true;
return true;
}
// Initialize the values of a new node
public boolean InitNew(Element v_elem, List v_next, boolean v_end) {
end = v_end;
elem = v_elem;
next = v_next;
return true;
}
// Insert a new node at the beginning of the list
public List Insert(Element new_elem) {
boolean ret_val;
List aux03;
List aux02;
aux03 = this;
aux02 = new List();
ret_val = aux02.InitNew(new_elem, aux03, false);
return aux02;
}
// Update the the pointer to the next node
public boolean SetNext(List v_next) {
next = v_next;
return true;
}
// Delete an element e from the list
public List Delete(Element e) {
List my_head;
boolean ret_val;
boolean aux05;
List aux01;
List prev;
boolean var_end;
Element var_elem;
int aux04;
int nt;
my_head = this;
ret_val = false;
aux04 = 0 - 1;
aux01 = this;
prev = this;
var_end = end;
var_elem = elem;
while ((!var_end) && (!ret_val)) {
if (e.Equal(var_elem)) {
ret_val = true;
if (aux04 < 0) {
// delete first element
my_head = aux01.GetNext();
} else { // delete a non first element
System.out.println(0 - 555);
aux05 = prev.SetNext(aux01.GetNext());
System.out.println(0 - 555);
}
} else nt = 0;
if (!ret_val) {
prev = aux01;
aux01 = aux01.GetNext();
var_end = aux01.GetEnd();
var_elem = aux01.GetElem();
aux04 = 1;
} else nt = 0;
}
return my_head;
}
// Search for an element e on the list
public int Search(Element e) {
int int_ret_val;
List aux01;
Element var_elem;
boolean var_end;
int nt;
int_ret_val = 0;
aux01 = this;
var_end = end;
var_elem = elem;
while (!var_end) {
if (e.Equal(var_elem)) {
int_ret_val = 1;
} else nt = 0;
aux01 = aux01.GetNext();
var_end = aux01.GetEnd();
var_elem = aux01.GetElem();
}
return int_ret_val;
}
public boolean GetEnd() {
return end;
}
public Element GetElem() {
return elem;
}
public List GetNext() {
return next;
}
// Print the linked list
public boolean Print() {
List aux01;
boolean var_end;
Element var_elem;
aux01 = this;
var_end = end;
var_elem = elem;
while (!var_end) {
System.out.println(var_elem.GetAge());
aux01 = aux01.GetNext();
var_end = aux01.GetEnd();
var_elem = aux01.GetElem();
}
return true;
}
}
// this class invokes the methods to insert, delete,
// search and print the linked list
class LL {
public int Start() {
List head;
List last_elem;
boolean aux01;
Element el01;
Element el02;
Element el03;
last_elem = new List();
aux01 = last_elem.Init();
head = last_elem;
aux01 = head.Init();
aux01 = head.Print();
// inserting first element
el01 = new Element();
aux01 = el01.Init(25, 37000, false);
head = head.Insert(el01);
aux01 = head.Print();
System.out.println(10000000);
// inserting second element
el01 = new Element();
aux01 = el01.Init(39, 42000, true);
el02 = el01;
head = head.Insert(el01);
aux01 = head.Print();
System.out.println(10000000);
// inserting third element
el01 = new Element();
aux01 = el01.Init(22, 34000, false);
head = head.Insert(el01);
aux01 = head.Print();
el03 = new Element();
aux01 = el03.Init(27, 34000, false);
System.out.println(head.Search(el02));
System.out.println(head.Search(el03));
System.out.println(10000000);
// inserting fourth element
el01 = new Element();
aux01 = el01.Init(28, 35000, false);
head = head.Insert(el01);
aux01 = head.Print();
System.out.println(2220000);
head = head.Delete(el02);
aux01 = head.Print();
System.out.println(33300000);
head = head.Delete(el01);
aux01 = head.Print();
System.out.println(44440000);
return 0;
}
}