-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDoubleLinkedList.java
174 lines (155 loc) · 4.04 KB
/
DoubleLinkedList.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
package Lists;
import java.util.Collection;
public class DoubleLinkedList<T> {
private Node head, tail;
private int size = 0;
private class Node<T> {
T val;
Node prev;
Node next;
public Node(T val, Node prev, Node next) {
this.val = val;
this.prev = prev;
this.next = next;
}
}
public int size() {
return size;
}
public boolean isEmpty() {
return size() == 0;
}
public void clear() {
size = 0;
head = null;
tail = head;
}
public boolean add(T val) {
if (head == null) {
head = new Node(val, null, null);
tail = head;
size++;
return true;
}
tail.next = new Node(val, tail, tail.next);
tail = tail.next;
size++;
return tail.val.equals(val);
}
public boolean addAll(Collection<? extends T> c) {
int prevSize = size();
c.forEach(elem -> add(elem));
return size() - prevSize == c.size();
}
public boolean contains(T val) {
Node temp = head;
while (temp != null) {
if (temp.val.equals(val)) {
return true;
}
temp = temp.next;
}
return false;
}
public boolean add(int index, T val) throws Exception {
if (index > size) {
throw new Exception("No such index found exception");
}
if (index == 0) {
Node node = new Node(val, null, head);
head = node;
size++;
return true;
}
if (index == size) {
add(val);
return true;
}
Node temp = head;
for (int i = 0; i < index - 1; i++) {
temp = temp.next;
}
Node node = new Node(val, temp, temp.next);
temp.next = node;
size++;
return true;
}
public T set(int index, T val) throws Exception {
if (index >= size) {
throw new Exception("No such index found exception");
}
Node temp = head;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
temp.val = val;
return (T) temp.val;
}
public T get(int index) throws Exception {
if (index > size - 1) {
throw new Exception("No such index found exception");
}
Node temp = head;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
return (T) temp.val;
}
public T remove(int index) throws Exception {
if (index > size - 1) {
throw new RuntimeException("No such index found exception");
}
Node temp = head;
if (index == 0) {
T ret = (T) head.val;
head = head.next;
head.prev = null;
size--;
return ret;
}
if (index == size - 1) {
T ret = (T) tail.val;
tail = tail.prev;
tail.next = null;
size--;
return ret;
}
for (int i = 1; i < index; i++) {
temp = temp.next;
}
T ret = (T) temp.next.val;
temp.next.next.prev = temp;
temp.next = temp.next.next;
size--;
return ret;
}
@Override
public String toString() {
StringBuilder ret = new StringBuilder("[");
Node temp = head;
if (temp == null) {
ret.append("]");
return ret.toString();
}
while (temp.next != null) {
ret.append(temp.val + ",");
temp = temp.next;
}
ret.append(temp.val + "]");
return ret.toString();
}
public String reverseToString() {
String ret = "[";
Node temp = tail;
if (temp == null) {
ret += "]";
return ret;
}
while (temp.prev != null) {
ret += temp.val + ",";
temp = temp.prev;
}
ret += temp.val + "]";
return ret;
}
}