forked from Krushna-Prasad-Sahoo/Python-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
61 lines (43 loc) · 1.09 KB
/
stack.py
File metadata and controls
61 lines (43 loc) · 1.09 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
# implementing the stack data structure using the python list
# Basic Code For Undestanding The Stack
# create empty list
stack = []
# stack follows the LIFO : Last In First Out
# push operation on stack : add elements at the end
stack.append(10)
stack.append(20)
stack.append(30)
print(stack)
# now the stack looks like this
# [10,20,30]
# pop operation on stack : remove the elements at the end
stack.pop()
stack.pop()
stack.pop()
print(stack)
# now the stack is empty because ll the elemnts removed
# []
# IMPLEMENTING STACK OPERATIONS
stack = []
def push():
element = input("enter the element")
stack.append(element)
print(stack)
def pop_elment():
if not stack:
print("the stack is empty")
else:
e = stack.pop()
print("removed element",e)
print(stack)
while True:
print("the select the operation 1.push 2.pop 3.quit")
choice = int(input())
if choice==1:
push()
elif choice==2:
pop_element()
elif choice==3:
break
else:
print("please select valid operation")