You are on page 1of 24

Q1.

Define stack(1mm/2mm)(slide 5)
Q2. Give applications of stack(1mm/2mm)(slide 6)
Q3. function def of push(3mm)
Q4. function def of pop(3mm)
Q5. function def of push as well as pop(3mm)

DATA STRUCTURE
Data-structures: Lists as covered in Class XI,
Stacks – Push, Pop using a list.
Data Structures

• A data structure is a group of data which can be processed as a single unit.


• This group of data may be of similar or dissimilar data types.
• Data Structures are very useful while programming because they allow
processing of the entire group of data as a single unit. This makes managing
of data simpler.
• The simplest form of data structure is an array which combines finite data
of same data type.
• Data structures are of two types: Linear and Non - Linear.
• In a linear data structure, the elements are stored in a sequential order.
• On the other hand, in a non linear data structure no sequential order is
followed.
• List is one of the simplest and most important data structures in Python.
STACK
• Consider the following examples of stacks:
• Ten glass plates placed one above another. (The plate that is kept last has
to be taken out first)
• The tennis balls in a container. (You cannot remove more than one ball at
a time)
• A pile of books
• A stack of coins

In the above picture coins are kept one above the other and if any additional
coin is to be added, it can be added only on the top. If we want to remove any
coin from the stack, the coin on the top of the stack has to be removed first.
That means, the coin that was kept last in the stack has to be taken out first.
STACK
• A stack is a data structure whose
elements are accessed according
to the Last-In First-Out (LIFO)
principle.
• This is because in a stack, insertion
and deletion of elements can only
take place at one end, called top
of the stack.
• Insertion in stack is also known as
a PUSH operation.
• Deletion from stack is also known
as POP operation in stack.
(Recursion)
The two operations performed on the stack are:
1. Push: Adding(inserting) new element on to the stack.
2. Pop: Removing (deleting) an element from the stack
Program to implement stack
def push(stack,n): s=[]
stack.append(n) c="y"
print("elemet pushed on to stack") while (c in "yY"):
def pop(stack): print("1. PUSH")
if (stack==[]): print("2. POP ")
print("Stack Empty-underflow") print("3. Display")
else: choice=int(input("Enter your choice: "))
print("Deleted element is : if (choice==1):
",stack.pop()) a=input("Enter any number :")
def display(stack): push(s,a)
if stack==[]: elif (choice==2):
print("Sorry!!! nothing to display") pop(s)
else: elif (choice==3):
l=len(s) display(s)
for i in range(l-1,-1,-1): else:
print(s[i]) print("Wrong Input")
c=input("Do you want to continue or
#driver code not?(y/n) ")
Write PUSH (Books) and POP (Books) methods in python to add Books and remove Books considering them to act as
Push and Pop operations of Stack.

def PUSH(Books):
a=input("enter book title : ")
Books.append(a)

def POP(Books):
if (Books==[]): #if len(Books)==0:
print ("Stack empty" )
else:
print ("Deleted element:",Books.pop())
Write PUSH (Names) and POP (Names) methods in python to add Names and Remove names considering them
to act as Push and Pop operations of Stack.

def push(Names):
nm=input(“Enter name”)
Names.append(nm)
print (‘Element:’,nm,'inserted successfully’)

def pop():
if Names == []:
print('Stack is empty!')
else:
print('Deleted element is’,Names.pop())
Write functions in Python for PushS(List) and for PopS(List) for performing Push and Pop operations with a stack of List
containing integers.

def PushS(List):
N=int(input("Enter integer"))
List.append(N)

def PopS(List):
if (List==[]):
print("Stack empty")
else:
print ("Deleted integer :",List.pop())
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.
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.

def MakePush(Package):
a=input("enter package description : ")
Package.append(a)

def MakePop(Package):
if (Package==[]):
print( "Stack empty")
else:
print ("Deleted element:",Package.pop())
Write a function in Python PUSH(Arr), where Arr is a list of numbers. From this list push all numbers
divisible by 5 into a stack implemented by using a list. Display the stack if it has at least one element,
otherwise display appropriate error message.
Write a function in Python PUSH(Arr), where Arr is a list of numbers. From this list push all numbers
divisible by 5 into a stack implemented by using a list. Display the stack if it has at least one element,
otherwise display appropriate error message.

def PUSH(Arr):
s=[]
for x in range(0,len(Arr)):
if Arr[x]%5==0:
s.append(Arr[x])
if len(s)==0:
print("Empty Stack")
else:
print(s)
Write a function in Python POP(st), where st is a stack implemented by a list of
numbers. The function returns the value deleted from the stack.

def popStack(st) :
# If stack is empty
if len(st)==0: #if st==[]:
print("Underflow")
else:
Ele=st.pop()
print(Ele)
return Ele
Write a program to perform push operation on a stack to push only words beginning with a vowel from a string entered
by a user

def pushOp(stack,s):
:
:#write the code
:
:

#driver code
stack=[]
s="hello how are you ele"
pushOp(stack,s)
print(stack)
Write a program to perform push operation on a stack to push only words beginning with a vowel from a string entered
by a user

def pushOp(stack,s):
for word in s.split():
if word[0] in "aeiouAEIOU":
stack.append(word)

#driver code
stack=[]
s="hello how are you ele"
pushOp(stack,s)
print(stack)
A linear stack called Mobiles contains the following information:
-Mobile Number ,name of the Mobile and cost of the Mobile
Write PUSH (Mobiles, N) method in python to add N Mobile details each.
A linear stack called Mobiles contains the following information:
-Mobile Number ,name of the Mobile and cost of the Mobile
Write PUSH (Mobiles, N) method in python to add N Mobile details each.

def PUSH (Mobiles,N):


for i in range(N):
Mobile_id=int(input("Enter the Mobile Id:"))
MobileName=input("Enter the Mobile name:")
MobileCost=float(input("Enter the cost:"))
b=[Mobile_id,MobileName,MobileCost]
Mobiles.append(b)
A linear stack called Mobiles contains the following information of N Mobiles:
-Mobile Number ,name of the Mobile and cost of the Mobile
Write a function in python DISPLAY(Mobiles, N) to display all Mobile details having price less than 50000.

def DISPLAY(Mobiles, N)
if Mobiles==[]: #len(Mobiles)==0
print("Underflow!!Stack is empty")
else:
print("\nMobile details (cost <50000)\n----------------------")
print("Mobile Id\tMobile Name\tMobile Cost")
for i in range(N-1,-1,-1):
Mobile_id,MobileName,MobileCost= Mobiles[i]
if MobileCost<50000:
print(Mobile_id,"\t",MobileName,"\t",MobileCost)
Write a program to perform push and pop operation on a stack to push all prime numbers from a list entered by a user
import math
def push_prime(stack,L):
for n in L:
n=abs(n)
count=0
factor=2
while factor<=n**0.5:
if (n%factor==0):
count+=1
factor+=1
if (count==0):
stack.append(n)
L=[]
while True:
L.append(int(input()))
ans=input("cont?(y/n)")
if ans in 'Nn':
break
print(L)
stack=[]
push_prime(stack,L)
print(stack)
QUEUE DATA STRUCTURE NOT IN SYLLABUS

=: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =: =:

You might also like