-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (46 loc) · 1.39 KB
/
main.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
def insertAfter(self, prev_node, new_data):
# 1. check if the given prev_node exists
if prev_node is None:
print("The given previous node must inLinkedList.")
return
# 2. create new node &
# Put in the data
new_node = Node(new_data)
# 4. Make next of new Node as next of prev_node
new_node.next = prev_node.next
# 5. make next of prev_node as new_node
prev_node.next = new_node
def append(self, new_data):
new_node = Node(new_data)
if self.head.next is None:
self.head = new_node
return
last = self.head
while (last.next):
last = last.next
last.next = new_node
def printList(self):
temp = self.head
while (temp):
print(temp.data)
temp = temp.next
if __name__ == '__main__':
lista = LinkedList()
lista.push(24)
lista.push(26)
lista.push(80)
lista.insertAfter(lista.head.next, 30)
lista.insertAfter(lista.head.next, 60)
lista.append(13)
lista.printList()