-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResizingAList.java
More file actions
234 lines (202 loc) · 6.3 KB
/
ResizingAList.java
File metadata and controls
234 lines (202 loc) · 6.3 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
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
public class ResizingAList implements List<Character>{
private static int capacity = 1; //动态变化数组长度
private int numInList = 0;
private int curr = 0;
private Character[] listArray;
public ResizingAList() {
setup();
}
private void setup() {
listArray = new Character[capacity];
}
private void setup(int size){
listArray = new Character[size];
}
public boolean isFull() {
return capacity == numInList;
}
public boolean isEmpty() {
return numInList == 0;
}
public void insert(Character item) throws ListException {
if (isFull()) {
Character[] oldlist = listArray;
setup(2*capacity); //扩表
for(int i = 0;i<numInList;i++){
listArray[i] = oldlist[i];
} //老表值给新表
capacity = 2*capacity;//容量翻倍
insert(item);
} else if (isEmpty()) {
listArray[0] = item;
curr = 0;
numInList++; //空表将curr变成0,指向当前元素
} else {
for (int i = numInList; i > curr+1; i--) {
listArray[i] = listArray[i - 1];
}
listArray[curr+1] = item; //curr的下一位作为插入位置
numInList++;
curr++; //curr指向插入元素
}
}
public void remove() {
if (!isEmpty()) { //表不空的时候
if(curr == numInList -1){
curr = 0;
numInList -- ; //如果在指针在表尾,则指到表头
}
else{
for(int i = curr;i<numInList-1;i++){
listArray[i] = listArray[i+1]; //元素移动
}
numInList --;
}
}
if(numInList<=capacity/4 && capacity>=4){
Character[] oldlist = listArray;
setup(capacity/2);
for(int i = 0;i<numInList;i++){
listArray[i] = oldlist[i];
}
capacity = capacity/2;
}
}
public void replace(Character Element) {
if(!isEmpty()){
listArray[curr] = Element;
}
}
public boolean gotoBeginning() {
if (!isEmpty()) {
curr = 0;
return true;
}
return false;
}
public boolean gotoEnd() {
if (!isEmpty()) {
curr = numInList-1;
return true;
}
return false;
}
public boolean gotoNext() {
if (!isEmpty() && curr < numInList-1) {
curr++;
return true;
}
return false;
}
public boolean gotoPrev() {
if (!isEmpty() && curr > 0) {
curr--;
return true;
}
return false;
}
@Override
public Character getCursor() {
return listArray[curr];
}
public boolean find(Character element) {
if(!isEmpty()) {
for (int i = curr; i < numInList; i++) {
if (listArray[i].equals(element)) {
curr = i;
return true;
}
}
curr = numInList-1;
return false;
}
return false;
}
@Override
public void moveToNth(int n) {
Character removed = listArray[curr];
remove();
curr = n;
try{
insert(removed);
}catch(ListException e){
throw new RuntimeException(e);
}
}
@Override
public void showStructure(PrintWriter pw) {
if (isEmpty()) {
pw.print("表是空的");
} else {
pw.print("< ");
for (int i = 0; i < numInList; i++) {
pw.print(listArray[i] + " ");
}
pw.print("> {capacity = "+ capacity+", length = " + numInList+", cursor = "+curr+"}");
}
}
public void clear() {
numInList = 0;
curr = 0;
capacity =1;
}
public static void main(String[] args) {
ResizingAList list = new ResizingAList();
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(ResizingAList 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();
}
}