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 | class DoubleLinkedList():
class Node():
def __init__(self, data, prev=None, next=None):
self.prev = prev
self.data = data
self.next = next
def __init__(self):
self.head = self.Node('머리', None, None)
self.tail = self.Node('꼬리', self.head, None)
self.head.next = self.tail
self.size = 0
def getSize(self):
return self.size
def isEmpty(self):
return self.size == 0
def insertBefore(self, preNode, data):
t = preNode.prev
n = self.Node(data, t, preNode)
preNode.prev = n
t.next = n
self.size += 1
def insertAfter(self, preNode, data):
t = preNode.next
n = self.Node(data, preNode, t)
t.prev = n
preNode.next = n
self.size += 1
def delete(self, x):
f = x.prev
r = x.next
f.next = r
r.prev = f
self.size -= 1
return x.data
def search(self, data):
check = self.head
for i in range(self.size):
if check.next.data == data:
print(data, "데이터는", i + 1, "번째에 있습니다.")
return None
check = check.next
print(data, "데이터는 리스트에 없습니다.")
return None
def printList(self):
if self.isEmpty():
print("the list is empty")
else:
p = self.head.next
while p != self.tail:
if p.next != self.tail:
print(p.data, end=" <=> ")
else:
print(p.data)
p = p.next
class EmptyError(Exception):
pass
s = DoubleLinkedList()
s.insertAfter(s.head, "apple")
s.insertBefore(s.tail, "cacao")
s.insertAfter(s.head.next, "banana")
s.printList()
s.delete(s.tail.prev)
s.printList()
s.insertAfter(s.head.next.next, "grape1")
s.printList()
s.insertAfter(s.head.next.next, "grape2")
s.printList()
|
댓글
댓글 쓰기