0% found this document useful (0 votes)
19 views4 pages

Python Stack Operations and Examples

The document provides a series of Python programs demonstrating various stack operations, including pushing, popping, peeking, and checking if the stack is empty. It also covers advanced operations like reversing the stack, checking for balanced parentheses, and finding minimum and maximum elements. Each operation is illustrated with code snippets for clarity.

Uploaded by

pvtparmeet
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Python Stack Operations and Examples

The document provides a series of Python programs demonstrating various stack operations, including pushing, popping, peeking, and checking if the stack is empty. It also covers advanced operations like reversing the stack, checking for balanced parentheses, and finding minimum and maximum elements. Each operation is illustrated with code snippets for clarity.

Uploaded by

pvtparmeet
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Stack Programs

1.​Push elements into stack​

stack = []
[Link](10)
[Link](20)
[Link](30)
print(stack)

2.​Pop element from stack​

stack = [10, 20, 30]


print("Popped:", [Link]())
print(stack)

3.​Peek top element​

stack = [10, 20, 30]


print("Top element:", stack[-1])

4.​Check if stack is empty​

stack = []
if not stack:
print("Empty Stack")
else:
print("Not Empty")

5.​Push elements using loop​

stack = []
for i in range(5):
[Link](i)
print(stack)

6.​Pop all elements​

stack = [1, 2, 3, 4, 5]
while stack:
print("Popped:", [Link]())

7.​Length of stack​

stack = [10, 20, 30, 40]


print("Stack size:", len(stack))

8.​Reverse stack using pop​

stack = [1, 2, 3, 4]
rev = []
while stack:
[Link]([Link]())
print(rev)

9.​Check element exists in stack​

stack = [5, 10, 15, 20]


if 15 in stack:
print("Found")
else:
print("Not Found")

10.​ Clear stack​

stack = [1, 2, 3]
[Link]()
print(stack)
11.​ Convert string to stack of characters​

s = "HELLO"
stack = list(s)
print(stack)

12.​ Pop characters to reverse string​

s = "HELLO"
stack = list(s)
rev = ""
while stack:
rev += [Link]()
print(rev)

13.​ Balanced parenthesis (very basic)​

expr = "(())"
stack = []
balanced = True
for ch in expr:
if ch == "(":
[Link](ch)
elif ch == ")":
if not stack:
balanced = False
break
[Link]()
if stack:
balanced = False
print("Balanced" if balanced else "Not Balanced")

14.​ Push numbers then pop all​

stack = []
for i in [2, 4, 6]:
[Link](i)
while stack:
print([Link]())
15.​ Peek after pushes​

stack = []
[Link](100)
[Link](200)
print("Top:", stack[-1])

16.​ Minimum element in stack​

stack = [5, 2, 9, 1, 7]
print("Min:", min(stack))

17.​ Maximum element in stack​

stack = [5, 2, 9, 1, 7]
print("Max:", max(stack))

18.​ Sum of stack elements​

stack = [1, 2, 3, 4, 5]
print("Sum:", sum(stack))

19.​ Sort stack (ascending)​

stack = [4, 1, 3, 2]
[Link]()
print(stack)

20.​ Sort stack (descending)​

stack = [4, 1, 3, 2]
[Link](reverse=True)
print(stack)

You might also like