-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTenLinkedList.java
More file actions
288 lines (237 loc) · 6.03 KB
/
Copy pathTenLinkedList.java
File metadata and controls
288 lines (237 loc) · 6.03 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
public class TenLinkedList<E> {
private static final class Entry<E>{
E element;
Entry<E> next;
Entry<E> previous;
Entry<E> f_next;
Entry<E> b_next;
public Entry(E Element) {
this.element = Element;}
}
private Entry<E> first;
private Entry<E> last;
private int size = 0;
public TenLinkedList() {
this.size = 0; // this is the constructor for the linked list list, it initiliazes the list with the size zero
}
public void addLast(Entry<E> e) {
// this is the helper method that we have created in order to help us with the add boolean method
// it defines the element to the last entry of the list, and help us to differntiate between the entry
// at the first postion or at the positon greater than the 0th position
if(size==0) {
first =e;
last=e;
}
else if(size>0) {
last.next=e;
e.previous=last;
last=e;
Entry<E> back=last;
if(size>=10) {
for(int i=0;i<10;i++) {
back=back.previous;
}
e.b_next=back;
back.f_next=e;
}
}
size++;
}
public boolean add(E o) {
// this method calls the helper function, and helps to input the entries into it
addLast(new Entry<E>(o));
return true;
}
public void last(Entry<E> e) {
// this is another helper method to input the entires for the last entry in the linked list
last.next=e;
e.previous=last;
last=e;
size++;
Entry<E> back=last;
}
public E get(int index) {
// in this method what we did was to search through thelinked list,
// to get the entry at the specific index and to return that specific node element .
Entry<E> h ;
int a = index/10; int c = a*10; int e=index-c; int g=size/2;
int b = (size-1-index)/10; int d = b*10; int f=(size-index-d);
if(g>=index) {
h=first;
for(int i=0;i<a;i++) {
h=h.f_next;
}
for(int j=0; j < e;j++) {
h = h.next;
}
}
else {
h=last;
for(int i=0;i<b;i++) {
h=h.b_next;
}
for(int j=0; j < f-1;j++) {
h= h.previous;
}
}
return h.element;
}
public Entry<E> getObject(int index) {
// in this helper method what we did was to search through the linked list,
// to get the entry at the specific index and to return that specific node element .
Entry<E> h ;
int a = index/10; int c = a*10; int e=index-c; int g=size/2;
int b = (size-1-index)/10; int d = b*10; int f=(size-index-d);
if(g>=index) {
h=first;
for(int i=0;i<a;i++) {
h=h.f_next;
}
for(int j=0; j < e;j++) {
h = h.next;
}
}
else {
h=last;
for(int i=0;i<b;i++) {
h=h.b_next;
}
for(int j=0; j < f-1;j++) {
h= h.previous;
}
}
return h;
}
public E remove(int index) {
// in this method what we did was to to remove a specific node of the linked list,
// and set it to null, moreover what we did was to set the previous node and the node after the one being removed to refer each other
Entry<E> m = getObject(index);
if( size <= 1) {
first=last=null;
}
else {
if(index>0 && index < size){
Entry<E> n = getObject(index+1);
Entry<E> h = getObject(index-1);
h.next=n;
n.previous=h;
}
else if(index == size) {
Entry<E> h = getObject(index-1);
last=h;
h.next=null;
}
else if(m==first) {
first=m.next;
m.next.previous = null;
}
}
if(index >10 && index < size) {
Entry<E> f1 = getObject(index);
Entry<E> f2 = getObject(index);
f2=f2.next;
for(int i=0;i<9;i++) {
f1=f1.previous;
}
for(int j=0;j<10;j++){
if( f1 == null || f2 ==null) {
break;
}
f1.f_next=f2;
f2.b_next=f1;
f2=f2.next;
f1=f1.next;
}
}
size --;
return m.element;
}
public void add(int index, E element) {
// in this method what we did was to use a specific index and insert the node at the specifc place,
// and making sure the other nodes also refer to the right node
Entry<E> e = new Entry<E>(element);
Entry<E> pointer=first; Entry<E> p=first;
Entry<E> po=first;
int number=0; int counter=0;
if(index != 0 && size - index > 0) {
while(number<index-1) {
pointer=pointer.next;
number++;
}
while(counter<index) {
p=p.next; counter++;
}
}
if(index != 0 && index != size) {
pointer.next=e;
e.previous=pointer;
e.next=p;
p.previous=e;
size++;
}
else if(index == 0 ) {
if(size == 0) {
first =e;
last=e;
size++;
}
else if(size > 0) {
first.previous=e;
e.next=first;
first=e;
size++;
}
}
else if(index == size) {
last(e) ;
}
}
public void clear() {
// this method makes the list equal to zero, and so there is nothing there in the list
if(size > 0) {
first=null;
last=null;
size=0;
}
}
public int size() {
// this method returns the size of the list being referred
return this.size;
}
public void tester(int q) {
// this was test method created to check the f_next and b_next of the respective node
Entry<E> point = first;
Entry<E> pt ;
int printer =0;
while(printer != q) {
point=point.next;
printer++;
}
System.out.println(point.element);
pt=(TenLinkedList.Entry<E>) point.b_next;
System.out.println(pt.element);
}
public String toString(){
// this uses the list, and converts them into a string
// String str = "[";
// Entry<E> present = first;
// int counter=0;
//
// while(present != null){
//
// if(counter<size) {
// str += present.element + ",";
// counter++;
// }
// present= present.next;
//
// }
// return str + "]";
// }
String result = "[";
if (first != null) {
result += first.toString();
}
return result+"]";
}
}