-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLList.java
More file actions
261 lines (227 loc) · 7.59 KB
/
LList.java
File metadata and controls
261 lines (227 loc) · 7.59 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
import java.io.*;
public class LList implements List<Character>{
private LinkedNode head = null;
private LinkedNode DummyNode = new LinkedNode(null,null);
private LinkedNode curr = null;
private static class LinkedNode<Character>{
Character element; //以泛型类型定义栈的element
LinkedNode<Character> next; //以LinkedNode类型定义指针next
public LinkedNode(Character element ,LinkedNode<Character> next){
this.element = element;
this.next = next;
}
public LinkedNode getNext(){
return this.next; //获取当前结点指针域的值
}
public Character getElement(){
return this.element; //获取当前结点的元素值
}
public void setElement(Character element) {
this.element = element;
}
public void setNext(LinkedNode<Character> next) {
this.next = next;
}
}
public LList(){
head = DummyNode; // 创建表的时候把头指针指向哑结点
curr = DummyNode; //curr指向当前结点的前一个结点
}
public boolean isFull(){
return false;
}
public boolean isEmpty(){
return DummyNode.getNext() == null;
}
public boolean gotoPrev(){
if(curr != head && !isEmpty()){ //如果不在表头
while(head.getNext() != curr){
head = head.getNext();
}
curr = head;
head = DummyNode; //head归位
return true;
}
return false;
}
public boolean gotoBeginning(){
if(!isEmpty()){
curr = head;
return true;
}
return false;
}
public boolean gotoEnd(){
if(!isEmpty()){
while(curr.getNext().getNext()!=null){ //目标元素的指针域为null则终止循环
curr = curr.getNext();
}
return true;
}
return false;
}
public boolean gotoNext(){
if(!isEmpty() && curr.getNext().getNext() != null){
curr = curr.getNext();
return true;
}
return false;
}
public boolean find(Character Element){
if(!isEmpty()){
for(LinkedNode i = curr;i.getNext()!=null; i =i.getNext()){
if(i.getNext().getElement().equals(Element)){
curr = i;
return true;
}
}
gotoEnd(); //curr到列表尾巴
return false;
}
return false;
}
@Override
public void clear(){
DummyNode.setNext(null);
head = DummyNode;
curr = head;
}
public void moveToNth(int n){
Character removed = (Character)curr.getNext().getElement();
remove(); //删除目标结点
curr = head;
int index = 0;
while(index<n-1){
gotoNext();
index++;
}
try{
insert(removed);
}catch(ListException e){
throw new RuntimeException(e);
}
}
@Override
public Character getCursor() {
return (Character) curr.getNext().getElement();
}
public void insert(Character element) throws ListException {
if (isEmpty()) {
DummyNode.setNext(new LinkedNode(element, null));
// curr不做变动,curr = DummyNode.getNext();
} else {
curr.getNext().setNext(new LinkedNode(element, curr.getNext().getNext()));
curr = curr.getNext(); //curr后移
//curr.getNext()是实际指向的元素,在其后插入元素
}
}
public void remove(){
if(!isEmpty()){
if(curr.getNext().getNext() != null) {
curr.setNext(curr.getNext().getNext()); //被删除结点的前结点指向后结点
//curr不用移动
}
else if(head != curr){ //删除尾结点
curr.setNext(null);
gotoBeginning(); //如果是删除尾结点,删完让curr前移
}
else{
DummyNode.setNext(null); //如果curr已经指头了,那么要把哑结点的指针域变成null
}
}
}
@Override
public void replace(Character newElement) {
if(!isEmpty()) {
curr.getNext().setElement(newElement);
}
}
public int NumInList(){
int index = 0;
while(head.getNext()!=null){
index++;
head = head.getNext();
}
head = DummyNode;
return index;
}
public int GetPos() {
if (isEmpty()) {
return 0;
} else {
int index = 0;
while (head != curr) {
index++;
head = head.getNext(); //头指针前进
}
head = DummyNode; //归位
return index;
}
}
@Override
public void showStructure(PrintWriter pw) {
if (isEmpty()) {
pw.print("表是空的");
} else {
pw.print("< ");
for (LinkedNode i = head.getNext(); i!=null;i=i.getNext()) {
pw.print(i.getElement() + " ");
}
pw.print("> {capacity = "+ NumInList()+", length = " + NumInList()+", cursor = "+GetPos()+"}");
}
}
public static void main(String[] args) {
LList list = new LList();
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(LList 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();
}
}