-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLList.java
More file actions
297 lines (255 loc) · 8.15 KB
/
DLList.java
File metadata and controls
297 lines (255 loc) · 8.15 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
public class DLList implements List<Character> {
private LinkedNode head = null;
private LinkedNode tail = null;
private LinkedNode curr = null;
private static class LinkedNode<Character> {
Character element; //以泛型类型定义栈的element
LinkedNode<Character> next; //以LinkedNode类型定义指针next
LinkedNode<Character> prev; //定义前向指针
public LinkedNode(Character element, LinkedNode<Character> prev, LinkedNode<Character> next) {
this.element = element;
this.next = next;
this.prev = prev;
}
public LinkedNode getNext() {
return this.next; //获取当前结点指针域的值
}
public LinkedNode getPrev() {
return this.prev;
}
public Character getElement() {
return this.element; //获取当前结点的元素值
}
public void setElement(Character element) {
this.element = element;
}
public void setNext(LinkedNode<Character> next) {
this.next = next;
}
public void setPrev(LinkedNode<Character> prev) {
this.prev = prev;
}
}
public DLList() {
head = null;
tail = null;
curr = null;
}
public boolean isFull() {
return false;
}
public boolean isEmpty() {
return head == null;
}
@Override
public void insert(Character newElement) throws ListException {
if (isEmpty()) { //如果表空,插入元素的同时,创建链表
head = new LinkedNode(newElement, null, null);
curr = head;
tail = head;
}
else if (curr == tail) { //如果curr在表尾
curr.setNext(new LinkedNode(newElement, curr, null));
tail = tail.getNext();
gotoNext();
} else {
curr.setNext(new LinkedNode(newElement, curr, curr.getNext()));
gotoNext(); //curr移向新元素
curr.getNext().setPrev(curr); //后继的前驱设置为curr
}
}
@Override
public void remove() {
if (!isEmpty()) {
if (head == tail) { //删除表尾
curr = null;
head = null;
tail = null;
//清空表
} else if (curr == tail) {
//删除表尾元素
tail = tail.getPrev();
tail.setNext(null); //断开与表尾元素的链接
gotoBeginning(); //curr到表头
} else if (head == curr) { //如果要删表头元素
gotoNext(); //curr指向新的表头
curr.setPrev(null);
head = curr;
} else {
curr.getPrev().setNext(curr.getNext());
curr.getNext().setPrev(curr.getPrev());
gotoNext();
}
}
}
@Override
public boolean gotoBeginning() {
if (!isEmpty()) {
curr = head;
return true;
}
return false;
}
@Override
public boolean gotoEnd() {
if (!isEmpty()) {
curr = tail;
return true;
}
return false;
}
@Override
public boolean gotoNext() {
if (curr != tail && !isEmpty()) {
curr = curr.getNext();
return true;
}
return false;
}
@Override
public boolean gotoPrev() {
if (curr != head && !isEmpty()) {
curr = curr.getPrev();
return true;
}
return false;
}
public void moveToNth(int n) {
Character removed = (Character) curr.getElement();
remove(); //删除目标结点
int index = 0;
gotoBeginning();
while (index < n) {
gotoNext();
index++;
}
try {
insert(removed);
} catch (ListException e) {
throw new RuntimeException(e);
}
}
@Override
public Character getCursor() {
return (Character) curr.getElement();
}
@Override
public boolean find(Character searchElement) {
if (!isEmpty()) {
for (LinkedNode i = curr; i.getNext() != null; i = i.getNext()) {
if (i.getElement().equals(searchElement)) {
curr = i;
return true;
}
}
gotoEnd(); //curr到列表尾巴
return false;
}
return false;
}
@Override
public void clear() {
head = null;
tail = null;
curr = null;
}
@Override
public void replace(Character newElement) {
if(!isEmpty()) {
curr.setElement(newElement);
}
}
public int NumInList(){
int index = 0;
LinkedNode i = head;
while(i!=null){
index++;
i = i.getNext();
}
return index;
}
public int GetPos() {
if (isEmpty()) {
return 0;
} else {
int index = 0;
LinkedNode i = head;
while (i != curr) {
index++;
i = i.getNext(); //头指针前
}
return index;
}
}
@Override
public void showStructure(PrintWriter pw) {
if (isEmpty()) {
pw.print("表是空的");
} else {
pw.print("< ");
for (LinkedNode i = head; i!=null; i=i.getNext()) {
pw.print(i.getElement() + " ");
}
pw.print("> {capacity = "+ NumInList()+", length = " + NumInList()+", cursor = "+GetPos()+"}");
}
}
public static void main(String[] args) {
DLList list = new DLList();
try (BufferedReader bf = new BufferedReader(new FileReader("D:/list_testcase.txt"))){
String line;
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
while((line = bf.readLine())!=null){
handleCommand(list,line,pw);
}
System.out.println(sw);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void handleCommand(DLList list,String command,PrintWriter pw){
String[] parts = command.split(" ");
for(String item : parts){
try {
switch (item.charAt(0)) {
case '+':
list.insert(item.charAt(1));
break;
case '-':
list.remove();
break;
case '=':
list.replace(item.charAt(1));
break;
case '*':
list.gotoEnd();
break;
case '#':
list.gotoBeginning();
break;
case '~':
list.clear();
break;
case '>':
list.gotoNext();
break;
case '<':
list.gotoPrev();
break;
default:
System.out.println("没有这个命令");
}
}
catch(ListException e){
System.out.println("ListException"+e.getMessage());
}
}
list.showStructure(pw);
pw.println();
}
}