-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubleLinkedList.py
More file actions
80 lines (56 loc) · 1.83 KB
/
doubleLinkedList.py
File metadata and controls
80 lines (56 loc) · 1.83 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
class Node:
def __init__(self, data, next = None, prev = None):
self.data = data
self.next = next
self.prev = prev
class doubleLinkedList:
def __init__(self):
self.head = None #double linked-list의 Head Node를 가리킨다
def addFirst(self, data):
if self.head == None:
self.head = Node(data)
else:
newNode = Node(data)
current = self.head
newNode.next = current
current.prev = newNode
self.head = newNode
def addLast(self, data): #double linked-list의 마지막에 Node(data)를 연결
newNode = Node(data)
if self.head == None:
self.head = newNode
else:
current = self.head
while current.next != None:
current = current.next
current.next = newNode
newNode.prev = current.next
def addNode(self, data, key): #linked-list의 data값을 key로 검색해 그 뒤에 Node를 추가
newNode = Node(data)
if self.head == None:
self.head = newNode
else:
current = self.head
while current.data != key:
current = current.next
if current == None:
print("Key value doesn't exist")
Next = current.next
current.next = newNode
newNode.prev = current
newNode.next = Next
Next.prev = newNode
def printNode(self):
if self.head == None:
return
else:
current = self.head
while current != None:
print(current.data)
current = current.next
N = int(input())
dLL = doubleLinkedList()
for i in range(N):
dLL.addLast(int(input()))
dLL.addNode(3, 2)
dLL.printNode()