Important Questions On Stack
Important Questions On Stack
for
Term 2 Examination
Class 12 Computer Science (CBSE)
st=[ ]
def push():
sn=input("Enter name of student:")
st.append(sn)
print(st)
def pop():
if st==[]:
print("Stack is empty")
else:
print("Deleted student name :",st.pop())
def display():
if st==[]:
print("Stack is empty")
else:
for i in st[::-1]:
print(i)
Q2. Write a function Push() which to add in a stack named "MyStack". After calling push() three times, a
message should be displayed "Stack is Full"
st=[ ]
StackSize=3
def push():
sn=input("Enter name of student:")
if len(st)<StackSize:
st.append(sn)
else:
print("Stack is full!")
print(st)
Q3: Julie has created a dictionary containing names and marks as key value pairs of 6 students. Write a
program, with separate user defined functions to perform the following operations:
• Push the keys (name of the student) of the dictionary into a stack, where the corresponding value
(marks) is greater than 75.
• Pop and display the content of the stack.
st=[]
def push():
for i in R:
if R[i]>75:
st.append(i)
print(st)
def pop():
while True:
if st!=[]:
print(st.pop(),end=" ")
else:
break
Q4: Alam has a list containing 10 integers. You need to help him create a program with separate user
defined functions to perform the following operations based on this list.
• Traverse the content of the list and push the even numbers into a stack.
• Pop and display the content of the stack.
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
st=[]
def push():
for i in N:
if i%2==0:
st.append(i)
def pop():
while True:
if st!=[]:
print(st.pop(),end=" ")
else:
break
Q 5: Write a function push(l) to perform push opration in stack. Where a list is given:
L=[10,12,15,16,17,18,20,21,25]
Push only those element who is divisible by 5.
def push(l):
L2.append(l)
L2=[]
L1=[10,12,15,16,17,18,20,21,25]
for i in L1:
if i%5==0:
push(i)
print("Original List:\n",L1)
print("List after using push:\n",L2)
Q 6: Write a function Addvowel(l) to perform push opration in stack. Where a list is given:
L=[‘Ansh’,’Vipin’,’Ishan’,’Devesh’,’Om’,’Upashna’]
Push only those element who is started from vowels
def Addvowel(l):
L2.append(l)
L2=[]
L1=['Ansh','Vipin','Ishan','Devesh','Om','Upashna']
for i in L1:
if i[0] in 'aeiouAEIOU':
Addvowel(i)
print("Original List:\n",L1)
print("List after using push:\n",L2)