-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedLIst.py
More file actions
113 lines (83 loc) · 2.43 KB
/
linkedLIst.py
File metadata and controls
113 lines (83 loc) · 2.43 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
class Node:
def __init__(self, data, next=None):
self.data = data
self.next = next
#Linked List의 원소를 클래스로 지정. 클래스의 data에는 입력 변수를, next에는 다음 Node를 지정
class linkedList:
def __init__(self):
self.head = None
#Linked List의 head Node를 가리킨다
def addLast(self, data):
if self.head == None:
self.head = Node(data)
else:
current = self.head
while current.next != None:
current = current.next
current.next = Node(data)
def addFirst(self, data):
newNode = Node(data)
if self.head == None:
self.head = newNode
else:
newNode.next = self.head
self.head = newNode
def addNode(self, data, key):
current = self.head
if current == None:
self.head = Node(data)
else:
while current.data != key:
current = current.next
if current == None:
print("Key value doesn't exist")
return
newNode = Node(data)
newNode.next = current.next
current.next = newNode
def delFirst(self):
current = self.head
if current == None:
return
else:
self.head = current.next
del current
def delLast(self):
current = self.head
if self.head == None:
return
else:
while current.next != None:
prev = current
current = current.next
prev.next = None
del current
def delNode(self, key):
current = self.head
if current == None:
return
else:
while current.data != key:
prev = current
current = current.next
if current == None:
print("Key value doesn't exist")
return
prev.next = current.next
del current
def printNode(self):
current = self.head
while (current != None):
print(current.data)
current = current.next
n = int(input())
num = []
Nodes = linkedList()
for i in range(n):
num.append(int(input()))
Nodes.addLast(num[i])
Nodes.addNode(4, 3)
Nodes.addFirst(0)
Nodes.delNode(3)
Nodes.delLast()
Nodes.printNode()