0% found this document useful (0 votes)
1 views7 pages

Ncert Stack Programs With Questions

Uploaded by

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

Ncert Stack Programs With Questions

Uploaded by

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

STACK PROGRAMS WITH QUESTIONS

------------------------------------------------------------
QUESTION 1
Write a menu-driven program to implement a stack using list with push, pop and display
operations.
------------------------------------------------------------
# Stack using list
stack = []

# Function to push element


def push():
item = int(input("Enter element to push: "))
[Link](item)
print("Element pushed")

# Function to pop element


def pop():
if len(stack) == 0:
print("Stack Underflow (Empty)")
else:
print("Popped element:", [Link]())

# Function to display stack


def display():
if len(stack) == 0:
print("Stack is Empty")
else:
print("Stack elements (top to bottom):")
for i in range(len(stack)-1, -1, -1):
print(stack[i])

# -------- Menu Driven Program --------


while True:
print("\n--- STACK MENU ---")
print("1. Push")
print("2. Pop")
print("3. Display")
print("4. Exit")

ch = int(input("Enter your choice: "))

if ch == 1:
push()
elif ch == 2:
pop()

elif ch == 3:
display()

elif ch == 4:
print("Exiting program...")
break

else:
print("Invalid choice")
------------------------------------------------------------
QUESTION 2 (NCERT)
Write a Python program to push all elements of a list into a stack and display the stack.
------------------------------------------------------------

def list_to_stack():
stack = []
lst = [10, 20, 30, 40]

for i in lst:
[Link](i)

print("Stack from list:", stack)

------------------------------------------------------------
QUESTION 3 (NCERT)
Write a Python program to push all values of a dictionaryinto a stack.
------------------------------------------------------------

def dict_to_stack():
stack = []
d = {1: 100, 2: 200, 3: 300}

for v in [Link]():
[Link](v)

print("Stack from dictionary:", stack)

------------------------------------------------------------
QUESTION 4 (NCERT)
Write a Python program to reverse a string using stack.
------------------------------------------------------------

def reverse_string():
stack = []
s = input("Enter string: ")

for ch in s:
[Link](ch)

rev = ""
while stack != []:
rev += [Link]()

print("Reversed string:", rev)

------------------------------------------------------------
QUESTION 5 (NCERT)
Write a Python program to check whether a string is a palindrome using stack.
------------------------------------------------------------

def palindrome_check():
stack = []
s = input("Enter string: ")

for ch in s:
[Link](ch)

rev = ""
while stack != []:
rev += [Link]()

if s == rev:
print("Palindrome")
else:
print("Not Palindrome")

------------------------------------------------------------
QUESTION 6 (NCERT)
Write a Python program to evaluate a postfix expression using stack.
------------------------------------------------------------
def postfix_eval():
stack = []
exp = input("Enter postfix expression: ")

for ch in exp:
if [Link]():
[Link](int(ch))
else:
b = [Link]()
a = [Link]()

if ch == '+':
[Link](a + b)
elif ch == '-':
[Link](a - b)
elif ch == '*':
[Link](a * b)
elif ch == '/':
[Link](a / b)

print("Postfix Result:", [Link]())

------------------------------------------------------------
QUESTION 7 (PREVIOUS YEAR – NCERT PATTERN)
A list containing product records is [Link] user-defined functions to push products costing
more than 50 into a stack and pop them.
------------------------------------------------------------

Product = []
L = [('Laptop', 90000), ('Mobile', 30000), ('Pen', 50), ('Headphones', 1500)]

def Push_list():
for item in L:
if item[1] > 50:
[Link](item)

def Pop_list():
if Product == []:
print("Stack Empty")
else:
while Product != []:
print([Link]())
print("Stack Empty")

------------------------------------------------------------
QUESTION 8 (PREVIOUS YEAR – NCERT PATTERN)
A dictionary contains product name and [Link] user-defined functions to push products
costing more than 50 into a stack and pop them.
------------------------------------------------------------

ProductDict = {
"Laptop": 90000,
"Mobile": 30000,
"Pen": 50,
"Headphones": 1500
}

Product2 = []

def Push_dict():
for k in ProductDict:
if ProductDict[k] > 50:
[Link]((k, ProductDict[k]))

def Pop_dict():
if Product2 == []:
print("Stack Empty")
else:
while Product2 != []:
print([Link]())
print("Stack Empty")

Question [Link] a Python program to check whether an expression has balanced parentheses
using stack.

Program: Parenthesis Matching

def check_parenthesis():
stack = []
exp = input("Enter expression: ")

for ch in exp:
if ch in "({[":
[Link](ch)
elif ch in ")}]":
if stack == []:
print("Not Balanced")
return

top = [Link]()

if (ch == ')' and top != '(') or \


(ch == '}' and top != '{') or \
(ch == ']' and top != '['):
print("Not Balanced")
return

if stack == []:
print("Balanced")
else:
print("Not Balanced")

check_parenthesis()

QUESTION 10

Write a Python program to convert an infix expression into postfix expression using stack.

Program: Infix → Postfix Conversion

def precedence(op):
if op == '+' or op == '-':
return 1
elif op == '*' or op == '/':
return 2
else:
return 0

def infix_to_postfix():
stack = []
postfix = ""
exp = input("Enter infix expression: ")

for ch in exp:
if [Link](): # operand
postfix += ch

elif ch == '(':
[Link](ch)

elif ch == ')':
while stack != [] and stack[-1] != '(':
postfix += [Link]()
[Link]() # remove '('

else: # operator
while (stack != [] and
precedence(ch) <= precedence(stack[-1])):
postfix += [Link]()
[Link](ch)

while stack != []:


postfix += [Link]()

print("Postfix expression:", postfix)

infix_to_postfix()

You might also like