Repository navigation
Expand file tree
/
Copy pathStack
More file actions
78 lines (68 loc) · 1.75 KB
/
Copy pathStack
File metadata and controls
78 lines (68 loc) · 1.75 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
# Global
sample = [None for i in range(6)]
null_pointer = -1
head_pointer = null_pointer
# Initialising stack
def init_stack(Array):
global head_pointer
for i in range(len(Array)):
Array[i] = None
head_pointer = null_pointer
print("Initialised Successfully")
# Push
def push(Array, Value):
global head_pointer
head_pointer += 1
if head_pointer >= len(Array):
print("ERROR! Stack is full")
else:
Array[head_pointer] = Value
print("Pushed Successfully")
# Pop
def pop(Array):
global head_pointer, null_pointer
if head_pointer == null_pointer:
print("ERROR! Stack is empty")
return -1
else:
head_pointer -= 1
print("Popped Successfully")
return Array[head_pointer + 1]
# Display
def display(array):
if head_pointer == null_pointer:
print("Stack is empty")
else:
for i in range(len(array)-1, null_pointer, -1):
if i == head_pointer:
print(array[i], "\t <-- Head")
else:
print(array[i])
# Menu
status = False
while not status:
print("Choice 1: Initialise Stack")
print("Choice 2: Push an item")
print("Choice 3: Pop an item")
print("Choice 4: Display the stack")
print("Choice 5: Terminate")
print("")
choice = input("Enter choice number: ")
if choice == '1':
init_stack(sample)
elif choice == '2':
print("")
i = input("Enter data to be pushed: ")
push(sample, i)
elif choice == '3':
print(pop(sample))
input("")
elif choice == '4':
display(sample)
input()
elif choice == '5':
break
else:
print("_______________")
continue
print("_______________")