-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9618-43-2021-MJ.py
69 lines (66 loc) · 1.98 KB
/
9618-43-2021-MJ.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
class node():
def __init__(self):
self.data = 0
self.nextNode = 0
LinkedList = [node() for i in range(10)]
NULLPOINTER = -1
startPointer = 0
freeP = 0
def initialize():
global startPointer, freeP, NULLPOINTER
LinkedList[0].data = 1
LinkedList[0].nextNode = 1
LinkedList[1].data = 5
LinkedList[1].nextNode = 4
LinkedList[2].data = 6
LinkedList[2].nextNode = 7
LinkedList[3].data = 7
LinkedList[3].nextNode = -1
LinkedList[4].data = 2
LinkedList[4].nextNode = 2
LinkedList[5].data = 0
LinkedList[5].nextNode = 6
LinkedList[6].data = 0
LinkedList[6].nextNode = 8
LinkedList[7].data = 56
LinkedList[7].nextNode = 3
LinkedList[8].data = 0
LinkedList[8].nextNode = 9
LinkedList[9].data = 0
LinkedList[9].nextNode = -1
startPointer = NULLPOINTER
freeP = 5
def OutputNode(startp):
while startp != -1:
print(LinkedList[startp].data, " ", LinkedList[startp].nextNode)
startp = LinkedList[startp].nextNode
initialize()
OutputNode(0)
print("############")
print("############")
def addNode(Val):
global freeP , NULLPOINTER, startPointer
if freeP != NULLPOINTER:
NewNode = freeP
LinkedList[NewNode].data = Val
freeP = LinkedList[freeP].nextNode
pp = 3
cn = startPointer
try:
while cn!= NULLPOINTER and LinkedList[cn].data < Val:
pp = cn
cn = LinkedList[cn].nextNode
except:
pass
if pp == NULLPOINTER:
LinkedList[NewNode].nextNode = startPointer
startPointer = NewNode
else:
LinkedList[pp].nextNode = NewNode
LinkedList[NewNode].nextNode = cn
else:
print("LinkedList full")
addNode(5)
print("Pointer data pointer")
for i in range(10):
print(i , " " , LinkedList[i].data , " " , LinkedList[i].nextNode)