-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack with LL.py
More file actions
74 lines (67 loc) · 2.15 KB
/
Copy pathStack with LL.py
File metadata and controls
74 lines (67 loc) · 2.15 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
#global variables
NULLPOINTER = -1
CurrentNodePtr = TopOfStack = FreeListPtr = 0
Stack = []
class Node:
def __init__(self, Data, Pointer):
self.Data = Data
self.Pointer = Pointer
def InitialiseStack():
global TopOfStack, FreeListPtr
TopOfStack = NULLPOINTER
FreeListPtr = 0
for Index in range(8):
Stack.append(Node("", Index + 1))
Stack[7].Pointer = NULLPOINTER
def Push(NewItem):
global TopOfStack, FreeListPtr
if FreeListPtr != NULLPOINTER:
NewNodePtr = FreeListPtr
Stack[NewNodePtr].Data = NewItem
FreeListPtr = Stack[FreeListPtr].Pointer
Stack[NewNodePtr].Pointer = TopOfStack
TopOfStack = NewNodePtr
else:
print("no space for more data")
def Pop():
global TopOfStack, FreeListPtr
if TopOfStack == NULLPOINTER:
print("no data on stack")
Value = ""
else:
Value = Stack[TopOfStack].Data
ThisNodePtr = TopOfStack
TopOfStack = Stack[TopOfStack].Pointer
Stack[ThisNodePtr].Pointer = FreeListPtr
FreeListPtr = ThisNodePtr
return Value
def OutputAllNodes():
global TopOfStack, FreeListPtr
CurrentNodePtr = TopOfStack
if TopOfStack == NULLPOINTER:
print("no data on stack")
while CurrentNodePtr != NULLPOINTER:
print(CurrentNodePtr, Stack[CurrentNodePtr].Data)
CurrentNodePtr = Stack[CurrentNodePtr].Pointer
print(CurrentNodePtr, Stack[CurrentNodePtr].Data)
def GetOption():
print("1: Push a value\n2: Pop a value\n3: Output Stack\n4: End Program")
choice = int(input("Enter your choice: "))
return choice
InitialiseStack()
Choice = GetOption()
while Choice != 4:
if Choice == 1:
Data = input("Enter the value: ")
Push(Data)
OutputAllNodes()
if Choice == 2:
Data = Pop()
print("Data popped:", Data)
if Choice == 3:
OutputAllNodes()
print("\n\n")
print("TopOfStack:",TopOfStack, "FreeListPtr:",FreeListPtr)
for i in range(8):
print(i, Stack[i].Data, Stack[i].Pointer)
Choice = GetOption()