-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
48 lines (41 loc) · 971 Bytes
/
stack.py
File metadata and controls
48 lines (41 loc) · 971 Bytes
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
stack =[]
def push():
element = input("Enter element to push: ")
stack.append(element)
print(element, "pushed to stack.")
def pop():
if stack:
print("Popped element: ",stack.pop())
else:
print("Stack is empty!")
def peek():
if stack:
print("Top element: ",stack[-1])
else:
print("Stack is empty!")
def display():
if stack:
print("Stack elements: ", stack[::-1])
else:
print("Stack is empty!")
while True:
print("\nStack Operations:")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display")
print("5. Exit")
choice = input("Enter your choice (1-5): ")
if choice == '1':
push()
elif choice == '2':
pop()
elif choice == '3':
peek()
elif choice == '4':
display()
elif choice == '5':
print("Program exited.")
break
else:
print("Invalid choice! Please try again.")