-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublyLinkedList.py
250 lines (221 loc) · 6.66 KB
/
doublyLinkedList.py
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
class Node:
def __init__(self, key=None):
self.key = key
self.prev = self
self.next = self
def __str__(self):
return str(self.key)
class DoublyLinkedList:
def __init__(self):
self.head = Node() # create an empty list with only dummy node
def __iter__(self):
v = self.head.next
while v != self.head:
yield v
v = v.next
def __str__(self):
return " -> ".join(str(v.key) for v in self)
def printList(self):
v = self.head.next
print("h -> ", end="")
while v != self.head:
print(str(v.key)+" -> ", end="")
v = v.next
print("h")
# 나머지 코드
def splice(self, a, b, x):
if a == None or b == None or x == None:
return
a.prev.next = b.next
b.next.prev = a.prev
x.next.prev = b
b.next = x.next
a.prev = x
x.next = a
def search(self, key):
v = self.head.next
while v != self.head:
if v.key == key:
return v
v = v.next
return None
def isEmpty(self):
size = 0
v = self.head.next
while v != self.head:
size += 1
v = v.next
return size == 0
def first(self):
if self.isEmpty():
return None
return self.head.next
def last(self):
if self.isEmpty():
return None
return self.head.prev
def moveAfter(self, a, x):
self.splice(a, a, x)
def moveBefore(self, a, x):
self.splice(a, a, x.prev)
def insertAfter(self, a, key):
b = Node(key)
self.moveAfter(b, a)
def insertBefore(self, a, key):
b = Node(key)
self.moveBefore(b, a)
def pushFront(self, key):
self.insertAfter(self.head, key)
def pushBack(self, key):
self.insertBefore(self.head, key)
def deleteNode(self, x):
if x == None or x == self.head:
return
x.prev.next = x.next
x.next.prev = x.prev
del x
def popFront(self):
if self.isEmpty():
return None
else:
popHead = self.head.next
key = popHead.key
self.head.next = popHead.next
popHead.next.prev = self.head
del popHead
return key
def popBack(self):
if self.isEmpty():
return None
else:
popTail = self.head.prev
key = popTail.key
self.head.prev = popTail.prev
popTail.prev.next = self.head
del popTail
return key
def findMax(self):
if self.isEmpty():
return None
else:
v = self.head.next
maxVal = v.key
while v.next != self.head:
if maxVal < v.next.key:
maxVal = v.next.key
v = v.next
return maxVal
# prev = None
# tail = self.head
# maxVal = tail.key
# while tail.next != None:
# prev = tail
# tail = tail.next
# if maxVal < tail.key:
# maxVal = tail.key
# if prev == None:
# return maxVal
# else:
# if maxVal < tail.key:
# maxVal = tail.key
# return maxVal
# else:
# return maxVal
def deleteMax(self):
if self.isEmpty():
return None
else:
key = self.findMax()
maxNode = self.search(key)
self.deleteNode(maxNode)
return key
def size(self):
size = 0
v = self.head.next
while v != self.head:
size += 1
v = v.next
return size
def sort(self):
size = self.size()
key_list = []
for _ in range(size):
key_list.append(self.deleteMax())
for i in range(size):
self.pushFront(key_list[i])
return self
L = DoublyLinkedList()
while True:
cmd = input().split()
if cmd[0] == 'pushF':
L.pushFront(int(cmd[1]))
print("+ {0} is pushed at Front".format(cmd[1]))
elif cmd[0] == 'pushB':
L.pushBack(int(cmd[1]))
print("+ {0} is pushed at Back".format(cmd[1]))
elif cmd[0] == 'popF':
key = L.popFront()
if key == None:
print("* list is empty")
else:
print("- {0} is popped from Front".format(key))
elif cmd[0] == 'popB':
key = L.popBack()
if key == None:
print("* list is empty")
else:
print("- {0} is popped from Back".format(key))
elif cmd[0] == 'search':
v = L.search(int(cmd[1]))
if v == None:
print("* {0} is not found!".format(cmd[1]))
else:
print("* {0} is found!".format(cmd[1]))
elif cmd[0] == 'insertA':
# inserta key_x key : key의 새 노드를 key_x를 갖는 노드 뒤에 삽입
x = L.search(int(cmd[1]))
if x == None:
print("* target node of key {0} doesn't exit".format(cmd[1]))
else:
L.insertAfter(x, int(cmd[2]))
print("+ {0} is inserted After {1}".format(cmd[2], cmd[1]))
elif cmd[0] == 'insertB':
# inserta key_x key : key의 새 노드를 key_x를 갖는 노드 앞에 삽입
x = L.search(int(cmd[1]))
if x == None:
print("* target node of key {0} doesn't exit".format(cmd[1]))
else:
L.insertBefore(x, int(cmd[2]))
print("+ {0} is inserted Before {1}".format(cmd[2], cmd[1]))
elif cmd[0] == 'delete':
x = L.search(int(cmd[1]))
if x == None:
print("- {0} is not found, so nothing happens".format(cmd[1]))
else:
L.deleteNode(x)
print("- {0} is deleted".format(cmd[1]))
elif cmd[0] == "first":
print("* {0} is the value at the front".format(L.first()))
elif cmd[0] == "last":
print("* {0} is the value at the back".format(L.last()))
elif cmd[0] == "findMax":
m = L.findMax()
if m == None:
print("Empty list!")
else:
print("Max key is", m)
elif cmd[0] == "deleteMax":
m = L.deleteMax()
if m == None:
print("Empty list!")
else:
print("Max key", m, "is deleted.")
elif cmd[0] == 'sort':
L = L.sort()
L.printList()
elif cmd[0] == 'print':
L.printList()
elif cmd[0] == 'exit':
break
else:
print("* not allowed command. enter a proper command!")