NOTES DataStructure Stacks 2022 23
NOTES DataStructure Stacks 2022 23
Data Structure: Data-structures: Lists as covered in Class XI, Stacks – Push, Pop
using a list.
WHAT IS DATA STRUCTURE
The way in which data is stored in memory of computer for efficient search and
retrieval. Different data structures are suited for different problems.
Allow to insert and delete items from one endonly called TOP.
Stack works on LIFO principle. It means items added
last is removed first.
Real life Example: Stack of Plates,
Books,Bangles
Computer based examples: Undo Operation,
Function Call etc.
def push(L):
x= int(input("Enter data to be inserted to stack "))
L.append(x)
def pop(L):
if len(L)==0:
print("Stack is empty- cannot delete")
else:
x= L.pop()
print(x, " Has been deleted")
def display(L):
for i in range(len(L)-1,-1,-1):
print(L[i])
def menu():
print(" Choose 1 for Push, 2 for Pop 3 for Display elements 4
to Exit ")
mylist=[]
while True:
menu()
ch= int(input("Enter your choice 1/2/3/4 "))
if ch==1:
push(mylist)
print("Now Stack is : ")
display(mylist)
elif ch==2:
pop(mylist)
print("Now Stack is : ")
display(mylist)
elif ch==3:
display(mylist)
elif ch==4:
break
else:
print("Wrong input”)
Output:
Important Questions
Q2.
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}
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
Output :
TOM ANU BOB OM
Q3. 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]
Output:
38 22 98 56 34 12
Q4) “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.
Q5) What is the order of inserting elements in the following stack?
Ans. Order is: A, B, C, D
Q6) Insertion into stack is called ______ (push/pop)
Q7) Write a function Push() which takes number as argument and add in a
stack "MyValue"
Ans.
MyValue=[]
def Push(value):
MyValue.append(value)
Q8) Write a function Push that takes "name" as argument and add in a stack
named "MyStack"
Ans
Mynames=[]
def Push(Value):
Mynames.append(Value)
Push("Amit")
Q9. Write a function pop() which remove name from stack named "MyStack".
Ans.
def Pop(MyStack):
if len(MyStack) > 0:
MyStack.pop()
else:
print("Stack is empty.")
Ans.
MyStack=[]
def Push(rollno):
MyStack.append(rollno)
def Pop(MyStack):
if len(MyStack) > 0:
MyStack.pop()
else:
print("Stack is empty.")