-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyDoublyLinkedList.java
178 lines (157 loc) · 4.49 KB
/
MyDoublyLinkedList.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
175
176
177
178
public class MyDoublyLinkedList {
private MyDoublyLinkedData head;
private MyDoublyLinkedData tail;
private MyDoublyLinkedData prevCurrent;
private MyDoublyLinkedData nextCurrent;
private MyDoublyLinkedData current;
private int compareCount;
MyDoublyLinkedList() {
head = new MyDoublyLinkedData("*header*", "");
tail = head;
current = head;
tail.next = null;
}
public MyDoublyLinkedData getCurrent() {
return current;
}
public boolean movePrev(){
if (current.prev != null) {
nextCurrent = current;
current = current.prev;
return true;
} else {
return false;
}
}
public void setCurrent(String str) {
if ("head".equals(str)) {
current = head;
} else if ("tail".equals(str)) {
current = tail;
}
}
public boolean moveNext() {
if (current.next != null) {
prevCurrent = current;
current = current.next;
return true;
} else {
return false;
}
}
public void resetCompareCount() {
compareCount = 0;
}
public void setCompareCount(int count) {
compareCount = count;
}
public int getCompareCount() {
return compareCount;
}
// current の後ろに新規データを挿入
public void insertAfter(String key, String data) {
MyDoublyLinkedData newData = new MyDoublyLinkedData(key, data);
newData.prev = current;
if(current.next == null){
newData.next = null;
tail.next = newData;
}
else{
newData.next = current.next;
current.next.prev = newData;
current.next = newData;
}
}
public void insertBefore(String key, String data) {
MyDoublyLinkedData newData = new MyDoublyLinkedData(key, data);
newData.next = current;
if(current.prev == null){
newData.prev = null;
head.prev = newData;
}
else{
newData.prev = current.prev;
current.prev.next = newData;
current.prev = newData;
}
}
public void printInfo() {
MyDoublyLinkedData data = head;
while (data != null) {
System.out.println(data.toStringNodeData());
data = data.next;
}
}
// 連結リストの末尾に新規データを挿入
public void insertLast(String key, String data) {
MyDoublyLinkedData newData = new MyDoublyLinkedData(key, data);
newData.next = null;
tail.next = newData;
prevCurrent = tail;
tail = newData;
current = newData;
}
public MyDoublyLinkedData searchByKey(String key) {
MyDoublyLinkedData p = head;
while (p != null) {
if (strcmp(key, p.getKey()) == 0) {
current = p;
return p;
}
prevCurrent = p;
p = p.next;
}
prevCurrent = null;
current = head;
return null;
}
public MyDoublyLinkedData searchByData(String data) {
MyDoublyLinkedData p = head;
while (p != null) {
if (strcmp(data, p.getData()) == 0) {
current = p;
return p;
}
prevCurrent = p;
p = p.next;
}
prevCurrent = null;
current = head;
return null;
}
public MyDoublyLinkedData delete() {
if (current != null) {
if (current.getKey().equals("*header*")) {
return null;
}
MyDoublyLinkedData prevData = prevCurrent;
prevData.next = current.next;
MyDoublyLinkedData oldCurrent = current;
current = current.next;
if (current == null) {
tail = prevCurrent;
prevCurrent = null;
current = head;
}
return oldCurrent;
} else {
return null;
}
}
public void printData() {
MyDoublyLinkedData p = head;
while (p != null) {
if (p == current){
System.out.print("*cur* ");
}else{
System.out.print(" ");
}
System.out.println(p.toStringSimple());
p = p.next;
}
}
public int strcmp(String key1, String key2) {
compareCount++;
return key1.compareTo(key2);
}
}