-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathStack Impletation.py
More file actions
79 lines (64 loc) · 1.67 KB
/
Stack Impletation.py
File metadata and controls
79 lines (64 loc) · 1.67 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
def isEmpty(stk):
if stk == []:
return True
else:
return False
def push(stk,item):
stk.append(item)
top = len(stk) - 1
def pop(stk):
if isEmpty(stk) == True:
return "UNDERFLOW\n"
else:
popped_item = stk.pop()
if len(stk) == 0:
top = None
else:
top = len(stk) - 1
return popped_item
def peek(stk):
if isEmpty(stk) == True:
return "UNDERFLOW\n"
else:
top = len(stk) - 1
return stk[top]
def display(stk):
if isEmpty(stk):
print("STACK IS EMPTY\n")
else:
top = len(stk) - 1
print(stk[top],"<------- Top")
for i in range(top-1,-1,-1):
print(stk[i],"\n")
#____MAIN____
stack = []
top = None
while True:
print("STACK OPERATIONS:")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display")
print("5. Exit\n")
choice_for_operation = int(input("Enter your choice (1 - 5):\n"))
if choice_for_operation == 1:
item = int(input("Enter the value you want to PUSH: "))
push(stack,item)
elif choice_for_operation == 2:
item = pop(stack)
if item == "UNDERFLOW":
print("UNDERFLOW!!!\n")
else:
print("Popped item is",item,"\n")
elif choice_for_operation == 3:
item = peek(stack)
if item == "UNDERFLOW":
print("NO TOP AVAILABLE\n")
else:
print("Topmost element is",item,"\n")
elif choice_for_operation == 4:
display(stack)
elif choice_for_operation == 5:
exit()
else:
print("WRONG CHOICE.....CHOOSE AGAIN!!!\n")