Repository navigation
Expand file tree
/
Copy pathLinkedList.java
More file actions
220 lines (191 loc) · 4.73 KB
/
Copy pathLinkedList.java
File metadata and controls
220 lines (191 loc) · 4.73 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
package com.map;
import com.list.LinkedList3;
/**
* @author xjn
* @since 2019-12-26
*/
public class LinkedList<K, V> implements List<K, V> {
private class Node {
public K key;
public V value;
public Node next;
public Node(K key, V value) {
this.key = key;
this.value = value;
this.next = null;
}
public Node() {
this.key = null;
this.value = null;
this.next = null;
}
@Override
public String toString() {
return "(" + key + "," + value + ")";
}
}
private Node dummyHead;
private int size;
public LinkedList() {
this.dummyHead = new Node();
this.size = 0;
}
@Override
public int getSize() {
return this.size;
}
@Override
public boolean isEmpty() {
return this.size == 0;
}
@Override
public void add(K key, V value) {
addRecursive(dummyHead, key, value);
}
private Node addRecursive(Node node, K key, V value) {
if (node.next == null) {
Node temp = new Node(key, value);
node.next = temp;
size++;
return node;
}
return addRecursive(node.next, key, value);
}
@Override
public void addFirst(K key, V value) {
Node temp = dummyHead.next;
Node node = new Node(key, value);
dummyHead.next = node;
node.next = temp;
}
@Override
public void addLast(K key, V value) {
Node cur = dummyHead.next;
while (cur.next != null) {
cur = cur.next;
}
Node node = new Node(key, value);
cur.next = node;
}
@Override
public V get(K key) {
Node cur = dummyHead.next;
while (cur != null) {
if (key.equals(cur.key)) {
return cur.value;
}
cur = cur.next;
}
return null;
}
@Override
public V getFirst() {
Node cur = dummyHead.next;
if (cur != null) {
return cur.value;
}
return null;
}
@Override
public V getLast() {
Node cur = dummyHead.next;
while (cur.next != null) {
cur = cur.next;
}
return cur.value;
}
@Override
public void set(K key, V value) {
Node cur = dummyHead.next;
while (cur != null) {
if (key.equals(key)) {
cur.value = value;
}
cur = cur.next;
}
}
@Override
public boolean contains(K key) {
Node cur = dummyHead.next;
while (cur != null) {
if (key.equals(key)) {
return true;
}
cur = cur.next;
}
return false;
}
@Override
public V remove(K key) {
if (isEmpty()) {
return null;
}
V v = get(key);
removeRecursive(dummyHead, key);
size--;
return v;
}
private Node removeRecursive(Node node, K key) {
if (node == null) {
return null;
}
Node res = removeRecursive(node.next, key);
if (key.equals(node.key)) {
return res;
} else {
node.next = res;
}
return node;
}
@Override
public V removeFirst() {
if (isEmpty()) {
return null;
}
V value = dummyHead.next.value;
dummyHead.next = dummyHead.next.next;
size--;
return value;
}
@Override
public V removeLast() {
if (isEmpty()) {
return null;
}
Node cur = dummyHead;
for (int i = 0; i < size - 1; i++) {
cur = cur.next;
}
cur.next = null;
return null;
}
@Override
public void removeElement(K k) {
removeRecursive(dummyHead, k);
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
// Node cur = dummyHead.next;
// while(cur != null){
// res.append(cur + "->");
// cur = cur.next;
// }
res.append("dummyHead->");
for (Node cur = dummyHead.next; cur != null; cur = cur.next)
res.append(cur + "->");
res.append("NULL");
return res.toString();
}
public static void main(String[] args) {
LinkedList<Integer, Integer> linkedList = new LinkedList<>();
linkedList.removeFirst();
for (int i = 0; i < 10; i++) {
linkedList.add(i, i);
}
System.out.println(linkedList);
// System.out.println(linkedList.removeFirst());
linkedList.removeLast();
System.out.println(linkedList);
}
}