You are on page 1of 16

Practical Implementation of Stack using List

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.")

Q5. Write push(rollno) and pop() method in python:


push(rollno) --add roll number in Stack
pop() --- remove roll number from Stack.
MyStack=[]
def Push(rollno):
MyStack.append(rollno)
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

Q9. A stack is which of the following type of data structure?


a) Linear b) Dynamic c) Circular d) All of these
Q10.Stack data structure is following ____________ principle.LIFO
Q11.In stack data can be inserted or deleted from ____________ only.Top
Q12.The insert operation in stack is known as pop. (True/False)
Q13.You can replace any element position in stack. (True/False)
Q14.The peek operation refers to accessing/inspecting the top element in the stack. (True/False)
Q15.A condition raise due to the stack is full is known as ___________.
a) Underflow b) Overflow c) List is full d) Completely Filled
Q16.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
Q17.Stack overflow condition is raised in ____________ operation where as Stack underflow
condition is raised in _____________ operations. Push, Pop
Q18. 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.
For example: If the sample content of the dictionary is as follows:
R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}
The output from the program should be: TOM ANU BOB OM
R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}
def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[ ]:
return S.pop()
else:
return None
ST=[ ]
for k in R:
if R[k]>=75:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break
Q19. 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.
For Example: If the sample Content of the list is as follows:
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
Sample Output of the code should be: 38 22 98 56 34 12

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

2 .____________ is applied in stacks.


A. FIRST-IN-LAST-OUT B. LAST-IN –FIRST-OUT
C. both A&B D. None

3. Other names for the insertion and deletion operations in Stacks?


A. PUSH – insertion, POP –Deletion. B. PUSH – Deletion, POP – Insertion.
C. Both A and B are valid. D. Only A is true.

4. In stacks, the possibility to do insertion and deletion is ____


A. At one end only B. at both ends C. Both A & B. D. None

5. The representation of stack can be done in ____________.


A. One way B. Two ways C. Three D. none

6. Pick from the following which represents an element in a stack.


A. SIZE B. ITEM C. TOP D. BOTTOM

7. At which position in the stacks , the operations are being done.


A. TOP B. SIZE C. POP D. PUSH

8. It is impossible to do ___ operation on empty stack.


A. PUSH B. POP C. STATUS 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

11. Can we delete a node at front of a stack by using POP 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

13.To implement Quick sort we use the concept of ____.


A. Stack B. Set C. List D. Queue

14.The concept “Pile of Plates” is applicable in which one of the following.


A. Stack B. Queue C. Linked list D. Tree
15. From the below listed options, which will you use to convert infix to
postfix/prefix operators.
A. Queue B. Stack C. Linked List D. Tree

16) Before inserting into stack one must check the condition ………
A. Overflow B. Underflow C. Maximum elements D. Existing elements

17) Before deletion condition into stack …… has to be checked.


A. Overflow B. Underflow C. Maximum elements D. Existing elements

18) When does Top value of stack change in insertion process?


A. Before insertion B. After insertion
C. At the time of insertion D. While checking overflow

19) Deletion in the linked stack takes place by deleting ……..


A. Node pointed by the start process. B. End of the list
C. Beginning of the list D. Middle of the list

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

21) The deletion operation in stack is called ……


A. insert B. push C. pop D. top

22) The pointer associated with the stack is ………..


A. front B. rear C. top D. link

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

26) Which of the following is an application of stack?


A. finding factorial B. tower of Hanoi
C. infix to postfix D. all of the above

27) The retrieval of items in a stack is ……….. operation.


A. push B. pop C. retrieval D. access
28) The term push and pop is related to
A. Array B. Lists C. Stacks D. Trees

29) The elements are removal from a stack in ………. order.


A. Reverse B. Hierarchical C. Alternative D. Sequential

30) Write the O/P of the following code


push(5)
push(8)
pop
push(2)
push(5)
pop
pop
pop
push(1)
pop
a) 85251 b)85521 c)82551 d)81255

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

Objective type questions contains MCQs, One word answer questions,


Fill in the blanks or true/false.
[1] A ____________ is a way to store, organize, or manage data in efficient and
productive manner. Data Structure

[2] A stack is which of the following type of data structure?


a) Linear b) Dynamic c) Circular d) All of these

[3] Stack data structure is following ____________ principle. LIFO

[4] In stack data can be inserted or deleted from ____________ only. Top

[5] The insert operation in stack is known as pop. (True/False)

[6] You can replace any element position in stack. (True/False)

[7] The peek operation refers to accessing/inspecting the top element in the stack.
(True/False)

[8] A condition raise due to the stack is full is known as ___________.


a) Underflow b) Overflow c) List is full d) Completely Filled

[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

[10] Stack overflow condition is raised in ____________ operation whereas Stack


underflow condition is raised in _____________ operations.
Push, Pop

Data Structures : Linear Lists


Q.1 What do you mean by Data Structure?
Ans: Data Structure means organization of data. A data structure has well defined
operations or behavior.

Q2. How is Data Structure different from Data Type?


Data Structure provides information regarding organization of data where as Data
Type provides information regarding the domain of values and operations that can
be perform on data

Q3. Define - Stack, Queue, Tree.


Stack – A stack is a linear list also known as LIFO list with the special property that
items can be added or removed from only one end called the top.
Queue – A queue is a linear list also known as FIFO list with the special property that
items can be added added at one end and removed from the other.
Tree – A non-linear hierarchical organization of data as nodes and links between
them where the topmost node called root and bottom most nodes called leaves.

Q4. Name some linear and non-linear data structures


Linear Data Structures – Lists, Arrays, Stack, Queue
Non Linear Data Structures – Trees, Graphs

Q5. Name some operations commonly performed on data structures?


Ans: Traversal, Insertion, Deletion, Searching, Sorting, Merging etc.

Q6. What is a list?


Ans: A list is a mutable sequence of data elements indexed by their position. A list is
represented using [ ] . e.g L=[10,20,30]

Q7. What is traversing? Write python code to traverse a list.


Ans: Traversing means accessing or visiting or processing each element of any data
structure. #List traversal
L=[10,20,30,40,50]
for x in L :
print(x)

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()

Q9. What is data structure and why do we need it?


Ans: Data structure is a particular way of storing and organizing information in a
computer so that it can be retrieved and used most productively. Different kinds of
data structures are meant for different kinds of applications, and some are highly
specialized to specific tasks.

Q10.what is a stack ? what basic operation can be performed on them?


Ans: A stack is a collection of objects that supports fast last-in, first-out (LIFO)
semantics for inserts and deletes.
Basic operation are:
a) Push – insertion of element
b) Pop – deletion of an element
c) Peek- viewing topmost element without removing
d) Display- view all the element

Q11.Write a function in python, 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 (CBSE Sample
Paper-2019)

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())

Q12.Write a function in python, Push(Stu) and MakePop(Stu) to add a new studnet


and delete studnet from a List of Stu contain rollno, Sname and Class as list,
considering them to act as push and pop operations of the Stack data structure (CBSE
Sample Paper-2019)

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())

Q13.Write a function in python, Push(Package) and Pop(Package) to add details of


employee contain information (Empid, Ename and Salary) in the form of tuple in
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

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())

Q14.PUSH OPERATION ON STACK

// function to push an item of integer type into stack


stack=[ ]
def push (stack):
item=int(input(“Enter the values of item”))
stack.append(item)

// 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)

Q15.POP OPERATION ON STACK

// function to pop an item from stack


Stack=[ ]
def Pop (stack):
if(stack==[ ]):
print(“No element for pop”)
else:
ele=stack.pop()
print(“ Element to be poped”, ele)

Q16. What do you mean by data structure?


Ans . A data structure is a way of storing, organizing and retrieving data in a
computer.

Q17. Name any one linear data structure in python. Ans . List

Q18. Python list are ______________ in Nature (static/dynamic)

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

Q22.What is the full form of LIFO? Last In First Out

Q23. Give two example of stack from daily life.


Ans. A pile of book, a stack of carom coins etc.
Q24. Name two operations performed in stack. Ans. Push and Pop
Q25. What is the order of inserting elements in the following stack?

Ans. Order is: A, B, C, D

Q26. Organization of data in python means ______. Ans. Data Structure

Q27. Write the full form of the following: a. LIFO b. FIFO


Ans. a. LIFO : Last In First Out b. FIFO : First In First Out

Q28. Which data structure is represented as FIFO? Ans. Queue

Q29. Insertion into stack is called ______ (push/pop)

Q30. Giving printing command to printer is an example of _____ (stack/queue)

Q31. Reversing a number or a word/string is an example of ______(stack or queue)

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

Q35._________ function is used to add an element in stack. Ans. Push/ append()

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”.

Q40.Write push(rollno) and pop() method in python:


push(rollno) – add roll number in stack
pop() --- remove roll number from stack

Q40.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.
Q41.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.

Q42.A stack stores item in which order ?


Answer : A stack stores items in a last-in, first-out (LIFO) order.

Q43.What is the strength of a stack?


Answer : The strength of a stack is fast operations. All stack operation takes O(1)
time.

Q44.What are the main operations of a stack? Answer : PUSH, POP AND PEEK

Q45.What happens when we try to push an element in a full stack?


Answer : If the stack is full and does not contain enough space to accept an entity to
be pushed, the stack is then considered to be in an overflow state.

Q46.Which method is best suitable while coding for reversing a stack?


Answer : Recursion

Q47.How are the elements accessed in stack data structure?


Answer : In stack data structure, elements are accessed sequentially. Stack data
structure resembles the serial access memory.

Q48.Consider the following operation performed on a stack of size 5.


Push(1);
Pop();
Push(2);
Push(3);
Pop();
Push(4);
Pop();
Pop();
Push(5);
After the completion of all operation, what is the number of elements present in the
stack?
Answer: 1
Q49.Write any one inherent application of a stack?
Answer : Implementation of recursion.

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

Q51.Can Job Scheduling be performed using stacks? Answer : No

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

You might also like