-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
166 lines (139 loc) · 3 KB
/
DoublyLinkedList.java
File metadata and controls
166 lines (139 loc) · 3 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
import java.util.Iterator;
import java.util.NoSuchElementException;
public class DoublyLinkedList<T> implements List<T> {
private Node head, tail;
private int numberOfElements;
public DoublyLinkedList() {
head = null;
tail = null;
numberOfElements = 0;
}
@Override
public void addLast(T item) {
Node newNode = new Node(item);
if (tail == null) { // empty list
head = tail = newNode;
} else {
tail.next = newNode;
newNode.previous = tail;
tail = newNode;
}
numberOfElements++;
}
@Override
public void addFirst(T item) {
Node newNode = new Node(item);
if (head == null) { // empty list
head = tail = newNode;
} else {
newNode.next = head;
head.previous = newNode;
head = newNode;
}
numberOfElements++;
}
@Override
public T get(int position) {
if (position < 0 || position >= numberOfElements) {
return null;
}
Node current = head;
for (int i = 0; i < position; i++) {
current = current.next;
}
return current.data;
}
@Override
public void print() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
@Override
public void printBackwards() {
Node current = tail;
while (current != null) {
System.out.print(current.data + " ");
current = current.previous;
}
System.out.println();
}
@Override
public boolean remove(T item) {
if (head == null) {
return false;
}
Node current = head;
while (current != null) {
if (current.data.equals(item)) {
// only one element
if (head == tail) {
head = tail = null;
}
// removing head
else if (current == head) {
head = head.next;
head.previous = null;
}
// removing tail
else if (current == tail) {
tail = tail.previous;
tail.next = null;
}
// removing middle
else {
current.previous.next = current.next;
current.next.previous = current.previous;
}
numberOfElements--;
return true;
}
current = current.next;
}
return false;
}
@Override
public boolean isEmpty() {
return numberOfElements == 0;
}
@Override
public int getLength() {
return numberOfElements;
}
/**
* Inner class representing a node in the linked list
*/
private class Node
{
private T data;
private Node next, previous;
private Node(T data) {
this(data,null,null);
}
private Node (T data, Node next, Node prev) {
this.data = data;
this.next = next;
this.previous = prev;
}
}
public Iterator<T> iterator() {
return new DLLIterator();
}
private class DLLIterator implements Iterator<T> {
private Node current = head;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public T next() {
if (current == null) throw new NoSuchElementException();
T data = current.data;
current = current.next;
return data;
}
}
}