Stack Class XII
Stack Class XII
Q1. Write a function push(student) and pop(student) to add a new student name and remove
a student name from a list student, considering them to act as PUSH and POP operations of
stack Data Structure in Python.
st=[ ]
def push(st):
sn=input("Enter name of student")
st.append(sn)
def pop(st):
if(st==[]):
print("Stack is empty")
else:
print("Deleted student name :",st.pop())
Q2. Write a function push(number) and pop(number) to add a number (Accepted from the
user) and remove a number from a list of numbers, considering them act as PUSH and POP
operations of Data Structure in Python.
st=[ ]
def push(st):
sn=input("Enter any Number")
st.append(sn)
def pop(st):
if(st==[]):
print("Stack is empty")
else:
print("Deleted Number is :",st.pop())
Q3. Write a menu based program to add, delete and display the record of hostel using list as
stack data structure in python. Record of hostel contains the fields : Hostel number, Total
Students and Total Rooms
host=[ ]
ch='y'
def push(host):
hn=int(input("Enter hostel number"))
ts=int(input("Enter Total students"))
tr=int(input("Enter total rooms"))
temp=[hn,ts,tr]
host.append(temp)
def pop(host):
if(host==[]):
print("No Record")
else:
print("Deleted Record is :",host.pop())
def display(host):
l=len(host)
print("Hostel Number \tTotal Students\tTotal Rooms")
for i in range(l-1,-1,-1):
print(host[i][0],"\t\t",host[i][1],"\t\t",host[i][2])
while(ch=='y' or ch=='Y'):
print("1. Add Record\n")
print("2. Delete Record\n")
print("3. Display Record\n")
print("4. Exit")
op=int(input("Enter the Choice"))
if(op==1):
push(host)
elif(op==2):
pop(host)
elif(op==3):
display(host)
elif(op==4):
break
ch=input("Do you want to enter more(Y/N)")
Q1. Write a function Push() which takes number as argument and add in a stack "MyValue"
MyValue=[]
def Push(value):
MyValue.append(value)
Q2. Write a function Push that takes "name" as argument and add in a stack named "MyStack"
Mynames=[]
def Push(Value):
Mynames.append(Value)
Push("Amit")
Q3. Write a function Push() which takes "name" as argument and add in a stack named
"MyStack". After calling push() three times, a message should be displayed "Stack is
Full"
MyStack=[]
StackSize=3
def Push(Value):
if len(MyStack) < StackSize:
MyStack.append(Value)
else:
print("Stack is full!")
Q4. Write a function pop() which remove name from stack named "MyStack".
def Pop(MyStack):
if len(MyStack) > 0:
MyStack.pop()
else:
print("Stack is empty.")
Q6. Write add(bookname) and delete() method in python to add bookname and remove
bookname considering them to act as push() and pop() operations in stack.
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")
Q7. Write 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.
clientdetail=[]
def addclient(cn):
clientdetail.append(cn)
def remove():
if len(clientdetail)>0:
clientdetail.pop()
else:
print("Stack is empty")
Q8.A ____________ is a way to store, organize, or manage data in efficient and productive manner.
Data Structure
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[ ]:
return S.pop()
else:
return None
ST=[ ]
for k in N:
if k%2==0:
PUSH(ST,k)
while True:
if ST!=[ ]:
print(POP(ST),end=" ")
else:
break
MORE QUESTIONS ON STACK
1. What type of data structure does a Stack is?
A. Linear B. non-linear C. both A&B D. none
9. From the following which are static and dynamic representations of stacks?
A. Arrays – Static, Linked lists – Dynamic. B. Linked lists – Static, Arrays – Dynamic.
C. Queues –Dynamic ,Arrays – Static D. None of the mentioned.
10.Is there possibility to add a node at front of the stacks without PUSH operation.
A. YES B. NO
12. Pick out the odd one which is not related to stacks.
A. LIFO list B. FIFO list C. Push down lists. D. Piles
16) Before inserting into stack one must check the condition ………
A. Overflow B. Underflow C. Maximum elements D. Existing elements
20) The various operations that can be performed on stacks is/are …..
A. Insert an item into the stack B. Delete an item from the stack
C. Display the contents of the stack D. All of the above
23) In liked representation of stack ……. holds the elements of the stack.
A. INFO fields B. TOP fields C. LINK fields D. NULL fields
24) …….. form of access is used to add /remove nodes from a stack.
A. LIFO B. FIFO C. Both A and B D. None of these
25) In linked representation of stack the null pointer of the last node in the list signals
… A. Beginning of the stack B. Bottom of the stack
C. Middle of the stack D. In between some value
31) In the stack, If user try to remove element from the empty stack then it called as
_____ a) Empty Collection b) Overflow of stack
c) Garbage Collection d) Underflow of Stack
32) A peek can be simulated by accessing the element with which index
a) 0 b) 1 c) -1 d) Rear
33. Pushing an element into stack already having five elements and stack size of 5 ,
then stack becomes a) Overflow b) Crash c) Underflow d) User flow
34. Entries in a stack are “ordered”. What is the meaning of this statement?
a) A collection of stacks is sortable
b) Stack entries may be compared with the ‘<‘ operation
c) The entries are stored in a linked list
d) There is a Sequential entry that is one by one
[4] In stack data can be inserted or deleted from ____________ only. Top
[7] The peek operation refers to accessing/inspecting the top element in the stack.
(True/False)
[9] While popping the element from the stack, a condition will be raised, this
condition is known as ____________.
a) Underflow b) Overflow c) List is Empty d) Blank List
Q8. Name the methods used for inserting and deleting elements from a list.
Various methods for inserting elements in a list are - insert(), append(), extend() and
methods used for deleting items from a list are – pop() , remove(), clear()
def MakePush(Package):
a=int(input("enter package title : "))
Package.append(a)
def MakePop(Package):
if (Package==[]):
print( "Stack empty")
else:
print ("Deleted element:",Package.pop())
def Push(Stu):
rollno=int(input("enter package title : "))
Sname=int(input("enter package title : "))
Class=int(input("enter package title : "))
info=[rollno,Sname,Class]
Stu.append(info)
def Pop(Stu):
if (Stu==[]):
print( "Stack empty")
else:
print ("Deleted element:",Stu.pop())
def Push(Package):
Empid=int(input(“Enter Id of Employee: "))
Ename=input(“Enter Name of employee”)
Salary= int(input(“Enter Salary of an employee”))
T=(Empid, Ename ,Salary)
Package.append(T)
def Pop(Package):
if (Package==[]):
print( "Stack empty")
else:
print ("Deleted element:",Package.pop())
// function to push information of student include rollno and name in the form of
list, tuple, dictionary
stack=[ ]
def push (stack):
rollno=int(input(“Enter rollno of student”))
name =input(“Enter Name of student”)
item=(rollno,name) \\ [rollno,name] \\ {rollno : “name”} \\ as per the problem
stack.append(item)
Q17. Name any one linear data structure in python. Ans . List
Q19. “Lists are dynamic in nature” What do you mean by this statement?
Ans. This means that list can be grown or shrink in size means elements can be
deleted or added in list.
Q20. What do you mean by Stack?
Ans. A stack is a linear data structure where elements can be inserted or deleted at
one end.
Q21. Name the end where we add or remove element in stack.Ans. Top
Q32. In stack addition or removal of elements takes place at ___ (one/both) end of
the list.
Q33.In queue, addition of elements take place at one end and removal of elements
takes place at other end. (T/F)
Q34. If the elements “A”, “B”, “C” are added in the queue in the following order, first
A then B and in last C. In what order, it will come out of queue? Ans. A, B, C
Q36. Write a function Push() which takes number as argument and add in a stack
"MyValue"
MyValue=[ ]
def Push(value):
MyValue.append(value)
Q37. Write a function Push that takes "name" as argument and add in a stack named
"MyStack"
Mynames=[]
def Push(Value):
Mynames.append(Value)
Push("Amit")
Q38. Write a function Push() which takes "name" as argument and add in a stack
named
"MyStack". After calling push() three times, a message should be displayed "Stack is
Full"
Q39.Write a function pop() which remove name from stack named “MyStack”.
Q44.What are the main operations of a stack? Answer : PUSH, POP AND PEEK
Q50.If the elements “W”, “X”, “Y” and “Z” are placed in a stack and are deleted one
at a time, what is the order of removal? Answer : ZYXW
Aim : Consider a dictionary which contains 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.
Source code
d={"Ron":76,"Jai":45,"Bob":89,"Ali":65,"Anu":90,"Tom":82}
def push(stack,k):
stack.append(k)
def pop(stack):
if stack!=[]:
return stack.pop()
else:
return None
stack=[ ]
for k in d:
if d[k]>=75:
push(stack,k)
while True:
if stack!=[]:
print(pop(stack),end=" ")
else:
break
Aim :Consider a list which stores 10 numbers .Write 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 numbers into a stack which are
divisible by 5.
● Pop and display the content of the stack.
Source code
N=[2,5,10,13,20,23,45,56,60,78]
def push(stack,k):
stack.append(k)
def pop(stack):
if stack!=[]:
return stack.pop()
else:
return None
stack=[ ]
for k in N:
if k%5==0:
push(stack,k)
while True:
if stack!=[]:
print(pop(stack),end=" ")
else:
break