Very Short Answer Type Questions [1 Mark]
I. Define data strueture.
Ans. It is a way to store a collection of related data together. The data structure can bek implemented via the
list, dictionary, tuple,set etc.
2.What is LIFO?
Ans. Last In First Out.
3.What is traversal?
Ans. Accessing each element is known as traversal.
4. Name some applications of Stack.
Ans.Application of Stack are:
(a) Reverse of a String or Line.
(b) Function call routines.
(c) Backtracking
5. A list contains LST=(10,7,9,15,12]. 8 is added to the list. What will be the output? If
(a) LST as Stack
Ans. (a) LST as Stack
LST = [8,10,7,9,15,12]
6. Python program to check whether the list is empty or not.
Ans. A = [ ]
if not A:
print("List is empty")
7. Write a program to implement the stack using list.(4 Marks)
Ans.s= [ ]
c= "y"
while (c=="y"):
print ("1. PUSH\2. POP \3.Display")
choice=int(input ("Enter your choice: "))
if (choice==1):
a=input ("Enter any number :")
s. append (a)
elif (choice==2):
if (s==[]):
print ("Stack Empty")
else:
print ("Deleted elementis : ",s.pop())
elif (choice==3):
l=len(s)
for 1 in range (l-1,-1,-1):
print (s[i])
else:
print ("Wrong Input"")
c=input ("Do you want to continue or not? ")
8. Write Push(Book) and Pop(Book) methods in Python to Add a new Book and Remove a Book from a
List of Books, considering them to act as PUSH and POP operations of the data structure stack.
def push(Book):
a=input("enter any number:")
Book.append(a)
def pop(Book):
if(Book==[]):
print("Stack Empty)
else:
print("Deleted element is :",Book.pop()_)
9.Write a menu driven python program using function Push( ). Pop( ) and Display( ) to implement the
stack. The program will store the name of the books.
def push(Book):
name=input("Entr the book name")
Book.append(name)
def pop(Book):
if(Book==[]):
print("Under flow")
else:
print("Deleted element is :",Book.pop())
def display(Book):
if Book==[]:
print("Underflow ! ! !")
else:
print("Book is Stack")
l=len(Book)
for i in range(l-1,-1,-1):
print(Book[i],"<=",end="")
#main
Book=[]
while True:
print("\n1.push\n2.pop\n3.display\n4.exit")
ch=int(input("enter your choice"))
if ch==1:
push(Book)
elif ch==2:
pop(Book)
elif ch==3:
display(Book)
else:
break
10. Write a menu driven python program using function Push ( ), Pop ( ) and Display( ) to implement the
stack. The program will store the Employee details I.e. Employee number, Employee name and Salary,
def push(Emp):
eno=int(input(“Enter the Employee Number”))
ename=input(“Enter the employee Name”)
sal=input(“Enter the salary)
Emp.append([eno,ename,sal])
def pop(Emp):
if Emp==[]:
print("Under Flow")
else:
print("Employee Record Deleted",Emp.pop())
def display(Emp):
if Emp==[]:
print("Underflow ! ! !")
else:
print("Employee record in Stack\n")
l=len(Emp)
for i in range(l-1,-1,-1):
print(Emp[i])
#Main
Emp=[]
while True:
print("\n1.push\n2.pop\n3.Display\n4.exit")
ch=int(input("Enter your choice"))
if ch==1:
push(Emp)
elif ch==2:
pop(Emp)
elif ch==3:
display(Emp)
else:
break
11.Read the following code carefully which is implementing the concept of STACK.
def isEmpty(STACK):
if len(STACK)==0:
return True
else:
return False
def pop(STACK):
if isEmpty(Stack):
return"Underflow"
else:
element=STACK.pop()
if len(STACK)==0:
top=None
else:
top=len(STACK)-1
return element
(a) How are the above two functions inter-related?
Ans:The function isEmpty(STACK) will be called when pop(STACK) is called
(b) What is the difference in between STACK.pop() and pop(STACK)
Ans:STACK.pop() is pre defined and pop(STACK) is user defined
(c) What is the return type of the function defisEmpty (STACK)
Ans: Boolean
(d) What will the pop(STACK) function return if the stack already consist of ['A','B'] at the
position 0 and I respectively
Ans: B
(e) if len(STACK) == 0: What is the purpose of this line in the above code:
Ans.It checks if the stack is already empty.
PRACTICE QUESTIONS