0% found this document useful (0 votes)
17 views5 pages

Stack Qa Edited

Uploaded by

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

Stack Qa Edited

Uploaded by

Bhavya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1. Differentiate between push and pop in stacks.

Push operation refers to inserting an element in the stack. Since there is only one position at which the new
element can be inserted — Top of the stack, the new element is inserted at the top of the stack.
Pop operation refers to the removal of an element from the Top. STACK follows LIFO (Last In First Out).
2. What is Stack? Write any one application of Stack?
Stack is linear data structure. Stack uses LIFO (Last In First Out) algorithm. All operations such as insertion
(push) and deletion (pop) of element takes place from one end only.
Application of Stack:- Infix to Post conversion and Evaluation of postfix expression
3. What is stack? Why it is called LIFO data structure?
A stack is a linear data structure that follows the principle of Last In First Out (LIFO).
This means the last element inserted inside the stack is removed first.
Stack is a linear data structure in which the elements can be added or removed only from one end known
as “Top” of the stack. The discipline of Stack is LIFO (Last In First Out) i.e. element inserted last will be
removed first.
4. Define stack. What is the significance of TOP in stack?
A stack is an abstract data type and a linear or user defined data structure based on the principle of Last In
First Out (LIFO). A stack is a list where insertion and deletion can take place only at one end called Top.
5. What do you mean by push and pop operations on stack?
Adding an item to a stack is called PUSH and removing an item from a stack is called POP. Since stack is
LIFO in nature, push and pop will be done at the end of the stack.
6. Rajiv has created a dictionary containing employee names and their salaries as key value pairs of 6
employees.
Write a program, with separate user defined functions to perform the following operations:
● Push the keys (employee name) of the dictionary into a stack, where the corresponding value (salary) is
less than 85000.
● Pop and display the content of the stack.
For example:
If the sample content of the dictionary is as follows:
Emp={"Ajay":76000, "Jyothi":150000, "David":89000, "Remya":65000, "Karthika":90000, "Vijay":82000}
The output from the program should be: Vijay Remya Ajay
Answer:
Emp={"Ajay":76000, "Jyothi":150000, "David":89000, "Remya":65000,"Karthika":90000, "Vijay":82000}
def push(E,k):
E.append(k)

def pop(E):
if E != []:
return E.pop()
else:
return None
E = []
for i in Emp:
if Emp[i]<85000:
push(E,i)
while True:
if E != []:
print(pop(E), end=" ")
else:
break
7. Nandu has created a dictionary containing countries and continent as key value pairs of 6 countries. Write
a program, with separate user defined functions to perform the following operations:
● Push the keys (name of the country) of the dictionary into a stack, where country belongs to continent
“ASIA”.
● Pop and display the content of the stack.
For example: If the sample content of the dictionary is as
follows:
R={"UK":”EUROPE, "INDIA":”ASIA”, "CHINA":”ASIA”, "EGYPT":”AFRICA”, "CUBA":”AMERICA”,
"JAPAN":”ASIA”}
The output from the program should be: JAPAN CHINA INDIA
Answer:

R={"UK":”EUROPE, "INDIA":”ASIA”, "CHINA":”ASIA”,"EGYPT":”AFRICA”, "CUBA":”AMERICA”, PUSH


"JAPAN":”ASIA”}

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]=”ASIA’:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break

8. Reva has created a dictionary containing Product names and prices as key value pairs
of 4 products. Write a user defined function for the following:
● PRODPUSH() which takes a list as stack and the above dictionary as the parameters.Push the keys
(Pname of the product) of the dictionary into a stack, where thecorresponding price of the products is less
than 6000. Also write the statement to call the above function.
For example: If Reva has created the dictionary is as follows:
Product={"TV":10000, "MOBILE":4500, "PC":12500, "FURNITURE":5500}
The output from the program should be: [ ‘FURNITURE’, ‘MOBILE’]
Answer:

Product={"TV":10000, "MOBILE":4500, "PC":12500, "FURNITURE":5500}


stack=[]
def PRODPUSH(stack, P):
for k in Product:
if P[k]<6000:
stack.append(k)

PRODPUSH(stack, Product)
print(stack)
9. Assume a dictionary named KVS having Region and Number of Schools as key-value pairs. Write a program
with separate user-defined functions to perform the following operations:
Push the keys (Name of Region) of the dictionary into a stack, where the corresponding value (Number
of Schools) is more than 50.
Pop and display the content of the stack.
For example
If the sample content of the dictionary is as follows:
KVS={"DELHI":55, “JAMMU”:27, "JAIPUR":76, "GURUGRAM":47, "AGRA":60}
The output from the program should be:
AGRA JAIPUR DELHI
Answer:
Push Function
def PUSH(S,N):
S.append(N)

Pop Function
def POP(S):
if S!=[]:
return S.pop()
else:
return None
ST=[]
KVS={"DELHI":55, “JAMMU”:27, "JAIPUR":76, "GURUGRAM":47, "AGRA":60} push() and pop()
for k in KVS:
if KVS[k]>50:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break

10. Vaishnav has a list containing names of 10 cities. 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 those names which are having more than 5 characters in it.
● Pop and display the content of the stack.
For Example: If the sample Content of the list is as follows:
N=[“Paris”, “England”, “Mumbai”, “Delhi”, “London”]
Sample Output of the code should be: London Mumbai England
Answer:

N=[“Paris”, “England”, “Mumbai”, “Delhi”, “London”,”Dehradun”,”Calcutta”,”Bareilly”,”Merrut”,”Raipur”]


def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[]:
return S.pop()
else:
return None
ST=[]

for k in N:
if len(k)>=5:
PUSH(ST,k)

while True:
If ST!=[]:
print(POP(ST),end=" ")
else:
break
11. Shaan has a list containing 14 integers. You need to help him create a program with separate user defined
function to perform the following operation based on this list.
● NUMUSH() which takes a list as stack and the above list of numbers as the parameters. push the
numbers of the list which are divisible by 5 into a stack. Also write the statement to call the function.
For Example: If the sample Content of the list is as follows:
M=[2, 10,13,17,25,32,38,44, 56, 60,21, 74, 35,15]
Sample Output of the code should be: [10, 25, 60, 35, 15]
Answer:

M=[2, 10,13,17,25,32,38,44, 56, 60,21, 74, 35,15]


stack=[]
def NUMPUSH(stack, P):
for k in M:
if k%5==0:
stack.append(k)
NUMPUSH(stack, M)
print(stack)
12.

Answer:

MARKS=[]

def push(record):
MARKS.append(record)
print("Stack contents")
print(MARKS)

choice = "y"
while choice in ["y","Y"]:
RollNo = int(input("enter roll no"))
StudentName = input("Enter Student name:")
Mark = int(input("Enter Marks"))
push([RollNo, StudentName, Mark])
choice = input("enter y to continue n to exit")
13.

For Example:
If the sample Content of the list is as follows:
N=[12, 15, 34, 50, 21, 70, 98, 22, 35, 38]
Sample Output of the code should be:
15 50 70 35
If the sample Content of the list is as follows:
N=[12, 18, 34, 56, 21, 73, 98, 22, 33, 38]
Stack Underflow as no numbers divisible by 5
Answer:
N_STACK=[]
L=[12, 1, 34, 53, 21, 72, 98, 22, 34, 38]

def PUSH(L):
for ele in L:
if ele%5==0:
N_STACK.append(ele)
if len(N_STACK)<=0:
print("Stack Underflow as no numbers divisible by 5")
else:
for i in range(len(N_STACK)):
print(N_STACK.pop(),end=" ")
PUSH(L)
14. A list NUM has some integer numbers. Write down a program to prepare a stack of even integers from the
given list NUM. The program should have user- defined functions which perform the following operations
based on this list.
Traverse the content of the list and push the EVEN numbers onto the stack.
Pop and display the content of the stack.
For Example:
If the Sample Content of the list is as follows:
N=[3,5,10,13,21,28,45,56,60,78]
Sample Output of the code should be:
10, 28,56, 60
Answer:

#Push Function
def PUSH(S,N):
S.append(N)
#Pop Function
def POP(S):
if S!=[]:
return S.pop()
else:
return None
ST=[]
Num=eval(input(“Enter list elements - ”))
for k in Num:
if k%2==0:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break
15. Write a function in Python PUSH_IN(L), where L is a list of numbers. From this list, push all even numbers
into a stack which is implemented by using another list.
16. Aruna has a list containing temperatures of 10 cities. You need to help her 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 negative temperatures into a stack.
● Pop and display the content of the stack.
For Example:
If the sample Content of the list is as follows:
T=[-9, 3, 31, -6, 12, 19, -2, 15, -5, 38]
Sample Output of the code should be:
-5 -2 -6 -9
17. Jiya has a list containing 8 integers. You need to help her create a program with two user defined functions
to perform the following operations based on this list.
● Traverse the content of the list and push those numbers
into a stack which are divisible by both 5 and 3.
● Pop and display the content of the stack.
For example:
If the sample Content of the list is as follows:
L=[5,15,21,30,45,50,60,75]
Sample Output of the code should be:
75 60 45 30 15

You might also like