-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDoublyLinkedList.java
191 lines (142 loc) · 3.27 KB
/
DoublyLinkedList.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
package dataStructures.doublyLinkedList;
import java.util.NoSuchElementException;
public class DoublyLinkedList {
public DoublyLinkedListNode head;
public DoublyLinkedListNode tail;
public int length;
public DoublyLinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
/**
* added new value to the end of the list
* @param data -value
* @time complexity O(1)
* @space complexity O(1)
*/
public void append(int data) {
DoublyLinkedListNode newNode = new DoublyLinkedListNode(data);
newNode.next = null;
if (isEmpty()) {
tail = newNode;
head = tail;
} else {
tail.next = newNode;
newNode.previous = tail;
tail = newNode;
}
length++;
}
/**
* add new value to the start of the list
* @param data
* @time complexity O(1)
* @space complexity O(1)
*/
public void preppend(int data) {
DoublyLinkedListNode newNode = new DoublyLinkedListNode(data);
newNode.next = null;
if (isEmpty()) {
head = newNode;
tail = head;
} else {
head.previous = newNode;
newNode.next = head;
head = newNode;
}
length++;
}
/**
* @return printed list from head to tail
*/
public String printHead() {
DoublyLinkedListNode current = head;
String out = "";
if (head == null) {
System.out.println("List is empty");
return out;
}
while (current != null) {
String prevoius = (current.previous != null) ? String.valueOf(current.previous.data) : "null";
String next = (current.next != null) ? String.valueOf(current.next.data) : "null";
out = out + "[" + prevoius + "<-(" + current.data + ")->" + next + "]";
current = current.next;
if (current != null) {
out = out + " <-> ";
}
}
return out;
}
/**
* @return printed list from tail to head
*/
public String printTail() {
DoublyLinkedListNode current = tail;
String out = "";
if (tail == null) {
System.out.println("List is empty");
return out;
}
while (current != null) {
String prevoius = (current.previous != null) ? String.valueOf(current.previous.data) : "null";
String next = (current.next != null) ? String.valueOf(current.next.data) : "null";
out = out + "[" + next + "<-(" + current.data + ")->" + prevoius + "]";
current = current.previous;
if (current != null) {
out = out + " <-> ";
}
}
return out;
}
/**
* @return boolean value if rhe list is empty
*/
public boolean isEmpty() {
return length == 0;
}
/**
* @return length of the list
*/
public int length() {
return length;
}
/**
* remove first element in the list
* @throws Exception then list is empty
* @time complexity O(1)
* @space complexity O(1)
*/
public void deleteFirst() throws Exception {
if (this.isEmpty()) {
throw new NoSuchElementException();
}
if (tail == head) {
tail = null;
} else {
head.next.previous = null;
}
head = head.next;
head.previous = null;
length--;
}
/**
* remove last element in the list
* @throws Exception then the list is empty
* @time complexity O(1)
* @space complexity O(1)
*/
public void deleteLast() throws Exception {
if (isEmpty()) {
throw new NoSuchElementException();
}
if (head == tail) {
head = null;
} else {
tail.next = null;
}
tail = tail.previous;
tail.next = null;
length--;
}
}