-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest42.py
More file actions
61 lines (47 loc) · 1.14 KB
/
test42.py
File metadata and controls
61 lines (47 loc) · 1.14 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
# Stack Has total 4 Operations :
# 1. Push
# 2. Pop
# 3.isEmpty ()
# 4.top or peek
# 1.Implement Stack Using List :
# To push elements in Stack we use append function in List Method :
stack =[]
stack.append(10)
stack.append(20)
stack.append(30)
stack.append(40)
print(stack)
# [10, 20, 30, 40]
# To pop element from the stack we use pop () function :
stack.pop()
print(stack)
# [10, 20, 30]
# To check whether Stack is Empty or not :
print(len(stack)==0)
# False
# To access the last element of the stack :
print(stack[-1])
# 30
# Actual Implementation of Stack using list :
stack=[]
def pushelement():
n=int(input("Enter the elements to push in stack :"))
stack.append(n)
print(stack)
def popelement():
if not stack:
print("Stack is Empty ! Insert Elements in the Stack :")
else:
stack.pop()
print(stack)
while True:
print("Select the operation : 1. Push 2. Pop 3. Quit ")
choice = int(input("Enter your choice :"))
if choice == 1 :
pushelement()
elif choice == 2:
popelement()
elif choice == 3:
break
else :
print("You have entered invalid Choice :")