-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
243 lines (216 loc) · 6.23 KB
/
DoublyLinkedList.java
File metadata and controls
243 lines (216 loc) · 6.23 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
package Test;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
import java.util.*;
/**
*
* @author asraf
*/
public class DoublyLinkedList<E>{
public class PostNode<E>{
E post;
PostNode<E> next;
PostNode<E> prev;
public PostNode(E post, PostNode next, PostNode prev){
this.post = post;
this.next = next;
this.prev = prev;
}
public PostNode(E post){
this(post, null, null);
}
}
PostNode<E> head;
private PostNode<E> tail;
PostNode<E> iterate; //Used when iterating in DoublyLinkedList, Tester2 class
private int size;
public DoublyLinkedList(){
size = 0;
this.head = null;
this.tail = null;
}
public int Size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public void addFirst(E element) {
//create object tmp and set pointer of the new node
PostNode<E> tmp = new PostNode(element, head, null);
//set head.prev of current head to be linked to the new node
if(head != null ) {head.prev = tmp;}
head = tmp; //now tmp become head
iterate = head;
//if no tail, then tmp set to be a tail
if(tail == null) { tail = tmp;}
size++;//increase number of node
}
public void add(E element) { //implementing addLast method
//create object tmp and set pointer of the previous node
PostNode<E> tmp = new PostNode(element, null, tail);
//set tail.next point to object tmp
if(tail != null) {tail.next = tmp;}
//now tmp become tail
tail = tmp;
//if no head, then tmp set to be a head
if(head == null) { head = tmp; iterate=head;}
size++;//increase number of node
}
// public void iterateForward(){
//
// System.out.println("iterating forward..");
// PostNode<E> tmp = head;
// while(tmp != null){
// System.out.print(tmp.post);
// System.out.print(" ");
// tmp = tmp.next;
// }
// }
// public void iterateBackward(){
//
// System.out.println("iterating backward..");
// PostNode<E> tmp = tail;
// while(tmp != null){
// System.out.println(tmp.post);
// tmp = tmp.prev;
// }
// }
// public E next() {
// if (current != null){
// E tmp = (E) current.post;
// current = current.next;
// return tmp;
// }else{
// return null;
// }
// }
public E removeFirst() {
if (size == 0) throw new NoSuchElementException();
//copy head to node tmp
PostNode<E> tmp = head;
//head.next become a head
head = head.next;
//set pointer of prev of new head to be null
head.prev = null;
//reduce number of node
size--;
return tmp.post;
}
public E removeLast() {
if (size == 0) throw new NoSuchElementException();
//copy tail to node tmp
PostNode<E> tmp = tail;
//tail.prev become a tail
tail = tail.prev;
//set pointer of next of new tail to be null
tail.next = null;
//reduce number of node
size--;
return tmp.post;
}
public void add(int index, E element){
//index can only be 0 ~ size()
if(index < 0 || index > size)
throw new IndexOutOfBoundsException();
if(index == 0)
addFirst(element);
else if(index == size)
add(element);
else{
/* set from head and begin traverse
stop on required position */
PostNode<E> temp = head;
for(int i=0; i<index; i++){
temp = temp.next;
}
/* create object insert and set pointer of the next pointer
to the temp node and also set pointer of the prev pointer
to the temp.prev node
*/
PostNode<E> insert = new PostNode(element, temp, temp.prev);
//set pointer 'next' of the node temp.prev to new node insert
temp.prev.next = insert;
//set pointer 'prev' of the node temp to new node insert
temp.prev = insert;
size ++;
}
} //might not be used
public E remove(int index){
E element = null;
if(index < 0 || index >=size)
throw new IndexOutOfBoundsException();
if(index == 0)
element=removeFirst();
else if(index == size-1)
element=removeLast();
else{
PostNode<E> temp = head;
for(int i=0; i<index; i++){
temp = temp.next;
}
element = temp.post;
temp.next.prev = temp.prev;
temp.prev.next = temp.next;
temp.next = null;
temp.prev = null;
size --;
}
return element;
}
public void clear(){
PostNode<E> temp = head;
while(temp != null){
temp = head.next;
temp.prev = temp.next = null;
head = temp;
}
head = null;
tail = null;
size = 0;
}
/**
* Returns the current post in the iteration.
* @return current Confession Post data in the iteration
*/
public E returnCurrentPost() {
// try{
if(iterate != null){
return iterate.post;
}
// }catch (NullPointerException e){
// System.out.println("End of List...");
// }
return null;
}
/**
* Sets iterate value as the first Confession Post in the iteration.
*/
public void firstPost(){
iterate = head;
}
/**
* Sets iterate value as the last Confession Post in the iteration.
*/
public void lastPost(){
iterate = tail;
}
/**
* Sets iterate value as the next Confession Post in the iteration.
*/
public void nextPost(){
if(iterate != null){
iterate = iterate.next;
}
}
/**
* Sets iterate value as the previous Confession Post in the iteration.
*/
public void previousPost(){
if(iterate != null){
iterate = iterate.prev;
}
}
}