stackQuestionBanksolution
stackQuestionBanksolution
Class XII
Computer science(083)
1. What do you understand by data structure? Give some examples.
Ans: A stack is a linear data structure where data is arranged objects on over another. It stores the
data in LIFO (Last in First Out) manner. The data is stored in a similar order as plates are arranged
one above another in the kitchen. The simple example of a stack is the Undo feature in the editor.
The Undo feature works on the last event that we have done.
2. “Lists are dynamic in nature” What do you mean by this statement? Explain with example.
Ans. This means that list can be grown or shrink in size.Elements can be deleted or added in list.
3. What do you mean by Stack? Give at least two applications in python where stack concepts used?
Ans. A stack is a linear data structure where elements can be inserted or deleted at one end.
Application:
Evolution of arithmetic expressions
To display string in reverse order
4. What is the full form of LIFO? Name the end where we add or remove element in stack.
Ans. Last In First Out
Top
5. Give some characteristics of Stack.
Ans. Characteristics of stack-
It is a LIFO data structure.
The insertion and deletion happens in one end.
6. Give two example of stack from daily life.
Ans. A pile of book, a stack of carom coins etc.
7. Name operations performed in stack.
Ans. Push: Pushes an item at the top of the stack.
pop: Remove and return the item from the top of the stack.
peek: Returns the item at the top of the stack without removing it.
size: Returns the total number of items in the stack.
isEmpty: Checks whether the stack is empty.
isFull: Checks whether the stack is full.
8. What is the order of inserting elements in the following stack?
17. Write a program which has a function pushname that takes "name" as argument and add in a
stack named "stuname"
Ans
stuname=[]
def pushname(name):
stuname.append(name)
pushname("Rahul")
18. Write a program which has a function pop() which remove name from stack named stuname.
Ans.
def pop(stuname):
if len(stuname) > 0:
stuname.pop()
else:
print("Stack is empty.")
19. Define the term “Overflow” and “Underflow” in stack.
Ans Overflow- A condition that occurs when an item want to be push, but stack is full.
Underflow -A condition that occurs when an item is called for from the stack, but the stack is
empty.
20. What the purpose of peek operation? Explain with example
Ans Returns the item at the top of the stack without removing it.
21. Write push(rollno) and pop() method in python:
push(rollno) --add roll number in Stack
pop() --- remove roll number from Stack.
Ans.
stk=[]
def Push(rollno):
stk.append(rollno)
def Pop(stk):
if len(stk) > 0:
stk.pop()
else:
print("Stack is empty.")
SECTION B
1. A dictionary containing names and grades as key value pairs of 8 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 (grade) is ‘A’.
● Pop and display the content of the stack.
For example: If the sample content of the dictionary is as follows: stu={"RAM":’A’,
"SHYAM":’B’, "MOHAN":’A’, "KISHAN":’C’, "SHIV":’A’,
"RAHUL":’C’,”VINAYAK”:”A”,”AMAN”:”A”}
The output from the program should be:
AMAN VINAYAK SHIV MOHAN RAM
SOL:
2. A list containing numbers. Write a program, with separate user defined functions to perform
the following operations:
● Push only that numbers into a stack, which are only divisible by 7.
● Pop and display the content of the stack.
For example: If the sample content of the list
[23, 21, 45, 44, 28, 35, 53, 42, 63]
The output from the program should be:
63 42 35 28 21
SOL
N=[23, 21, 45, 44, 28, 35, 53, 42, 63]
def PUSH(S,n):
S.append(n)
def POP(S):
if S==[]:
print("underflow, stack is empty")
else:
print(S.pop(),end=" ")
ST=[]
for k in N:
if k%7==0:
PUSH(ST,k)
while True:
POP(ST)
if ST==[]:
break
3. A tuple containing name of 10 students. Write a program, with separate user defined
functions to perform the following operations:
● Push only those names into a stack, whose name started with ‘S’ only.
● Pop and display the content of the stack.
For example: If the sample content of the list
("RAM", "SHYAM", "MOHAN", "KISHAN",
“SHIV","RAHUL",”VINAYAK”,”AMAN”,”SITA”,”SHUHANI”)
The output from the program should be:
SHUHANI SITA SHIV SHYAM
SOL
def push(s,l):
s.append(l)
def pop(s):
if s==[]:
print("underflow, stack is empty")
else:
print(s.pop(),end=" ")
stk=[]
for k in N:
if k[0] == 'S':
push(stk,k)
while True:
pop_disp(stk)
if stk==[]:
break
4. Write a program, with separate user defined functions to perform the following operations:
add(bookname) and delete() methods in python to add bookname and remove bookname
considering them to act as push() and pop() operations in stack.
Ans.
MyStack=[]
def add(bname):
MyStack.append(bname)
def delete(MyStack):
if len(MyStack) > 0:
MyStack.pop()
else:
print("Stack is empty. There is no book name")
5. Write a program, with separate user defined functions to perform the following operations:
addclient(clientname) and remove() methods in python to add new client and delete existing
client from a list "clientdetail", considering them to act as push and pop operations of the
stack.
Ans.
Clientdetail=[“avi”,”ravi”,”savi”,”kavi”]
def addclient(clientname):
s.append(clientname)
def remove():
if s==[]:
print("underflow, stack is empty")
else:
print(s.pop(),end=" ")
s=[]
for i in Clientdetail:
addclient(i)
while True:
remove()
if s==[]:
break
6. Write a program, with separate user defined functions to perform the following operations:
MakePush(Package) and MakePop(Package) to add a new Package and delete a Package
from a List of Package. Description, considering them to act as push and pop operations of
the Stack data structure.
SOL
P=[“BOMBAY_DELHI”,”LKO_GOA”,”DELHI_PUNE”]
def MakePush (package):
s.append(package)
def MakePop():
if s==[]:
print("underflow, stack is empty")
else:
print(s.pop(),end=" ")
s=[]
for i in P:
MakePush(i)
while True:
MakePop()
if s==[]:
break
7. Write a program in python to implement a stack for book details [book no,book
name,cost] ,write push() and pop() definitions and their calling statements.
SOL
def Push():
empcode=int(input("enter employee code"))
empname=input("enter employee name")
salary=int(input("enter salary"))
B.append([empcode,empname,salary])
def Pop():
if B==[]:
print("underflow, stack is empty")
else:
print(B.pop(),end=" ")
B=[]
while True:
ch=int(input("enter choice"))
if ch==1:
Push()
elif ch==2:
Pop()
else:
break
9. A list containing item details as [ [itemno, cost], [itemno, cost], [itemno, cost]…..of 5 items.
Write a program, with separate user defined functions to perform the following operations:
● Push the item no of the list into a stack, which cost is greater than 500.
● Pop and display the content of the stack.
For example: If the sample content of the dictionary is as follows: item=[[1001,400],[1002,600],
[1003,900],[1004,450],[1005,800]]
The output from the program should be:
Item no
1005
1003
1002
SOL
item=[[1001,400],[1002,600],[1003,900],[1004,450],[1005,800]]
def PUSH(S,n):
S.append(n)
def POP(S):
if S==[]:
print("underflow, stack is empty")
else:
print(S.pop())
ST=[]
for k in item:
if k[1]>500:
PUSH(ST,k[0])
while True:
POP(ST)
if ST==[]:
break
10. Write a function in Python PUSH(Arr), where Arr is a list of numbers. From this list push all
numbers divisible by 5 into a stack implemented by using a list. Display the stack if it has at least
one element, otherwise display appropriate error message.
Sol
def PUSH(Arr):
s=[]
for x in range(0,len(Arr)):
if Arr[x]%5==0:
s.append(Arr[x])
if len(s)==0:
print("Empty Stack")
else:
print(s)
11. Write a program, with separate user defined functions to perform the following operations:
A function in python named push_even(stk) where stk is a list of integers. From this
list, push all the elements that are even into a stack named evenstk implemented by
using a list.
A function in Python POP(evenstk), where evenstk is a stack implemented by a list
of numbers. The function delete and display element from the stack one by one.
Sol
stk=[23, 21, 45, 44, 28, 35, 53, 42, 63]
def push_even(stk):
for k in stk:
if k%2==0:
evenstk.append(k)
def POP(evenstk):
if evenstk==[]:
print("underflow, stack is empty")
else:
print(evenstk.pop(),end=" ")
evenstk=[]
push_even(stk)
while True:
POP(evenstk)
if evenstk==[]:
break
12. Write a program, with separate user defined functions to perform the following operations:
A is a list of numbers.
i) From this list push all numbers which is greater than 50 and even into a stack
implemented by using a list.
ii) Pop and Display the numbers from stack if it has at least one element, otherwise
display appropriate error message.
Sol
A=[23, 122, 45, 144, 28, 35, 53, 142, 63]
def push(A):
for k in A:
if k%2==0 and k>50:
stk.append(k)
def POP(stk):
if stk==[]:
print("underflow, stack is empty")
else:
print(stk.pop(),end=" ")
stk=[]
push(A)
while True:
POP(stk)
if stk==[]:
break
13. Write the menu driven program in python to implement following functions-
Push(student)- to insert details [srno,sname,stream]in to stack student if stream is ‘computer
science’.
Pop(student)-to remove and display students from student stack.
Display(student)-to display student details from student stack.
Sol
def Push():
srno=int(input("enter srno"))
sname=input("enter student name")
stream=input("enter stream")
if stream=="computer science":
B.append([srno,sname,stream])
def Pop():
if B==[]:
print("underflow, stack is empty")
else:
print(B.pop(),end=" ")
def Display():
if B==[]:
print("underflow, stack is empty")
else:
for i in range(len(B)-1,-1,-1):
print(B[i],end=" ")
B=[]
while True:
ch=int(input("enter choice"))
if ch==1:
Push()
elif ch==2:
Pop()
elif ch==3:
Display()
else:
break
14. Mohan 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 odd numbers which divisible by 9 into a stack.
● Pop and display the content of the stack.
For Example:
If the sample Content of the list is as follows: N=[120, 135, 342, 561, 210, 792, 981, 220, 45, 309]
Sample Output of the code should be: 45 981 792 342 135
Sol
N=[120, 135, 342, 561, 210, 792, 981, 220, 45, 309]
def push(N):
for k in N:
if k%2!=0 and k%9==0:
stk.append(k)
def POP(stk):
if stk==[]:
print("underflow, stack is empty")
else:
print(stk.pop(),end=" ")
stk=[]
push(N)
while True:
POP(stk)
if stk==[]:
break
15. Shyam has a list containing 10 characters. 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 only the vowel characters into a stack.
● Pop and display the content of the stack.
For Example:
If the sample Content of the list is as follows: N=[‘O’,’R’,’E’,’H’,’I’,’A’,’O’,’A’,’K’,’U’,]
Sample Output of the code should be: U A O A I E O
SOL
N=['O','R','E','H','I','A','O','A','K','U']
def push(N):
for k in N:
if k in ['A','E','I','O','U']:
stk.append(k)
def POP(stk):
if stk==[]:
print("underflow, stack is empty")
else:
print(stk.pop(),end=" ")
stk=[]
push(N)
while True:
POP(stk)
if stk==[]:
break