0% found this document useful (0 votes)
20 views

Data Structure

Uploaded by

Vishal .H
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Data Structure

Uploaded by

Vishal .H
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Structure

Govt. Model Sanskriti Senior Secondary school ,Bilaspur(Haryana)


Data Structure: • Overflow : It refer to as situation ,when one tries
to push an element in stack that is full.
• It represents how Data is stored /Organized in
Computer’s Memory. A Data structure defines a • Underflow: It refer to a situation when one tries
mechanism to store ,organise and access data to pop/delete from empty stack.
along with operations.
Implementation of stack by using List.
Implementation of Data Structure can be done in two
• Push-stack.append(item)
ways:
len(stack)=5
Simple Data Structure: Built from Primitive Data Types
• Pop-stack.pop()
(Integers,real,character,Boolean )
• Display- for(len(stack)-1,-1,-1)
Compound Data Structure: Simple Data structure are used
to form more complex data Structure.They are classified • append () it is used to add the item at the end of
into two Types. stack
• 1.Linear Data structure: Means elements are • pop() this function remove the last item from the
stored in sequential order example: Stack, Queue list
,Linke list.
• Peek- len(stack)-1
• 2.Non-Linear Data structure : Data can be stored stack=[1,2,3,4,5]
in multilevel structure . Example: Trees ,Graphs
5
4
3
2
1
def push(item):
stack.append(item)
def pop():
stack.pop()
def peek():
print(len(stack)-1)
def display():
Stack is a linear Data Structure for I in range(len(stack)-1,-1,-1):
print(stack[i])
Stack is a list of elements in which an element may be stack=[]
inserted or deleted only at one end, called the TOP of the while True:
stack. print(“1.Push”,end=“ ”)
It follows the principle Last in First out( LIFO) print(“2.Pop”,end=“ ”)
print(“3.Peek”,end=“ ”)
LIFO means the element inserted last would be the first to print(“4.Diplay”,end=“ ”)
be deleted. print(“5.Exit”)
ch=int(input(“enter Choice”))
Operation On stacks:
if ch==1:
There are mainly two types of operation that can be done item=int(input(“enter item”))
with stack. push(item)
elif ch==2:
PUSH: Insertion of an element on top od the stack is called
if (len(stack)==0):
PUSH.
print(“Underflow”)
POP: Removal an element from the top of stack is called break
POP. else:
stack.pop()
Peek: Inspecting the value at the Top. elif ch==3:
Data Structure
Govt. Model Sanskriti Senior Secondary school ,Bilaspur(Haryana)
if(len(stack)==0): Q2.Write a function in python Push(vehicle) where Vehicle is
print(“stack is empty”) dictionary containing details of vehicle {Car_name:Maker} The
else: function should push the name of car manufactured by ‘TATA’
peek() including all the possible cases like Tata,TaTa, etc)
For Example:
elif ch==4:
If the dictionary contains the following Data:
if(len(stack)==0): Vehicle={“Santro”:”Hyundai” , “Nexon”:”TATA” ,”Safari”:”Tata”}
print(“stack is empty”) The stack Contain
else: Safari
display() Nexon

Q1. A list contains following record of customer: (1/2 mark for defining correct function header
(1/2 mark for correct loop in function
[customer_name,Roomtype]
(1 mark for checking the condition)
Write the following function user defined function to perform (1 mark for appending)
given operation on the list name ‘Hotel’:
i) Push_cust()- To push customer name of those Ans2:
customers who are staying in ‘Delux’ Roomtype. Stack=[]
ii) Pop_cust()-To pop the names of the customer from def Push(Vehicle):
the stack and also display “underflow” when there
for veh_name in Vehicle:
are no customer in the stack.
if vehicle[veh_name] in (“TATA”, ”TaTa”, ”tata”,
For example:
“Tata”,”taTa”):
If the list with customer details follows:
[“Siddharth”, ”Delux”] stack.append(veh_name)
[“Rahul” , “Standard”]
[“Jerry” , “Delux”]
The stack should contain Q3.A list NList contains following record as list elements:
Jerry [City,Country,Distance from delhi]
Siddharth Each of these record are nested together to form a nested
The output should be: list.Write the following user defined function in python to
Jerry perform the specified.
Siddharth
Operation on the name stack travel.
Underflow
i)Push_element(NList): It takes the nested list as an
(1/2 mark for defining correct function header (Push_cust()) argument and pushes a list object containing name of the
(1/2 mark for correct loop in function Push_cust())
(1/2 mark for checking the condition and appending the data in Push_cust()) city and country which are not in india snd distance is less
(1/2 mark for defining correct function header (Pop_Cust())
(1/2 mark for correct loop in function (Pop_Cust())
than 3500 km from Delhi.
(1/2 mark for deleting and displaying data in (Pop_Cust()) ii)Pop_element():It pop the object from the stack and
display+ them.Also the function should display “Stack
Ans1: empty” if there are no element in the stack.
Hotel=[] For example: If the nested list contain the following data:
Customer=[[“Siddharth”, ”Delux”],[“Rahul” , Nlist=[[“newyork”,“USA”,”11735”],
“Standard”][“Jerry” , “Delux”]] [“Naypyidaw”,”Myanmar”,3219], [“Dubai”,”UAE”,2194],
def Push_cust(): [“London”,”England”,6693], [“Gangtok”,”India”,1580] ,
for i in customer: [“Columbo” ,”sri lanka”,3405]]
if i[1]==“Delux”: The Stack Should Contain:
Hotel.append(i[0]) [“Naypyidaw”,”Myanmar”]
def Pop_cust(): [“Dubai”,”UAE”]
while len(Hotel)>0: [“Columbo” ,”sri lanka”]
print(Hotel.pop()) Output should be:
else: [“Columbo” ,”sri lanka”]
print(“Underflow”) [“Dubai”,”UAE”]
[“Naypyidaw”,”Myanmar”]
Underflow
Data Structure
Govt. Model Sanskriti Senior Secondary school ,Bilaspur(Haryana)
ii)def PopBig():
Ans3: while len(BigNums)>0:
i.travel=[] print(BigNumsl.pop())
def Push_element(NList): else:
for i in NList: print(“underflow”)
if i[1]!=”india” and i[2]<3500:
travel.append([ i[0] , i[1] ])

ii. def Pop_element(): Q5. (A) You have a stack named BooksStack that contains
while len(travel)>0: records of books. Each book record is represented as a list
print(travel.pop()) containing book_title, author_name, and
else: publication_year.
print(“underflow”) Write the following user-defined functions in Python to
perform the specified operations on the stack BooksStack
Q4.Consider a list named Nums which contain random I. push_book (BooksStack, new_book): This function takes
integerWrite the following user defined functions in the stack BooksStack and a new book record new_book as
Python and perform the specified operations on a stack arguments and pushes the new book record onto the
named BigNums. stack.
(i) PushBig(): It checks every number from the list Nums II. pop_book (BooksStack): This function pops the topmost
and pushes all such numbers which have 5 or more digits book record from the stack and returns it. If the stack is
into the stack, BigNums. already empty, the function should display "Underflow".
(ii) PopBig (): It pops the numbers from the stack, III. peep (BookStack): This function displays the topmost
BigNums and displays them. The function should also element of the stack without deleting it. If the stack is
display "Stack Empty" when there are no more numbers empty, the function should display 'None'.
left in the stack.
For example: If the list Nums contains the following Ans:
data: I.def push_book (BooksStack, new_book):
Nums [213, 10025, 167, 254923, 14, 1297653, 31498, 386, Bookstack.append(new_book)
92765]
Then on execution of PushBig (), the stack BigNums II. def pop_book(BooksStack):
should store: while len(Booksstack)>0:
[10025, 254923, 1297653, 31498, 92765] print(BooksStack.pop())
And on execution of PopBig (), the following output else:
should be displayed: print(“underflow”)
92765
31498 III. def peep(BooksStack):
1297653 while len(BooksStack)>0:
254923 print(len(BooksStack)-1)
10025 else:
Stack Empty print(“None”)

Ans: Q6. Write the definition 'N' and pushes of a user-defined


i) BigNums=[] function push_even (N) which accepts al all those integers
def PushBig(): which are even from the list N into a list of integers in a
for i in Nums: parameter Stack named EvenNumbers.
s=str(i) Write function pop_even() to pop the topmost number
if len(s)>5: from the stack and returns it. If the stack is already empty
BigNums.append(i) then function should display "Empty".
Write function Disp_even() to display all element of the
stack without deleting them. If the stack is empty, the
function should display 'None'. For example:
Data Structure
Govt. Model Sanskriti Senior Secondary school ,Bilaspur(Haryana)
If the integers input into the list 'VALUES` are: [10, 5, 8, 3, Ans:
12] Status=[]
Then the stack EvenNumbers should store: def Push_element(cust):
[10,8,12] for i in cust:
if i[2]==”Goa”:
Ans: status.append([ i[0] ,i[1]])
EvenNumber=[]
def push_even(N):
for i in N: def Pop_element():
if i%2==0: while len(status)>0:
EvenNumber.append(i) print(status.pop())
else:
print(“Stack Empty”)
def pop_even():
while len(EevnNumber)>0: def peep(cust):
print(EvenNumber.pop()) while len(status)>0:
else: print(len(status)-1)
print(“Empty”) else:
print(“Empty Stack”)
def Disp_even():
while len(EvenNUmber)>0:
print(len(EvenNumber)-1)
else: Q8.Vishal has created a List Arr with some elements. Help
print(“None”) him to create user defined functions to perform the
following tasks: Create a Stack after checking the
Q7. A list contains following record of a customer: elements, if it is even multiply it by 2, and if the element
[Customer_name, Phone_number, City] is odd, then multiply it by 3. Pop and display the contents
Write the following user defined functions to perform of the stack.
given operations on the stack named 'status':
(i) Push_element(): To Push an object containing name Ans:
and Phone number of customers who live in Goa to the Stack=[]
stack. def push(Arr):
(ii) Pop_element(): To Pop the objects from the stack and for i in Arr:
display them. Also, display "Stack Empty" when there are if i%2==0:
no elements in the stack. For example: If the lists of stack.append(i*2)
customer details are: else:
["Gurdas", "9999999999", "Goa"]["Julee", "8888888888", stack.append(i*3)
"Mumbai"]["Murugan","7777777777","Cochin"] def pop():
["Ashmit", "1010101010", "Goa"] while len(stack)>0:
The stack should contain print(stack.pop())
["Ashmit", "1010101010"] else:
["Gurdas", "9999999999"] print(“under flow”)
The output should be:
["Ashmit", "1010101010"]
["Gurdas", "9999999999"]
Stack Empty
(iii) peep (cust): This function displays the topmost
element of the stack without deleting it. If the stack is
empty, the function should display 'Stack Empty.
Data Structure
Govt. Model Sanskriti Senior Secondary school ,Bilaspur(Haryana)
Q9. Write the definition of a user defined function Q1. Stack is based on the property.
QPushNV(N) which accepts a list of strings in the Ans: LIFO( Last in first out)
parameter N and pushes all strings which have no vowels Q2.In stack all operation takes place.
present in it, into a list named NoVowel. Ans: TOP
Write a program in Python to input 5 Words and push Q3.Insertion and Deletion operation of stack are known as
them one by one into a list named All. The program should ………………………….. respectively.
then use the function Ans: Push and POP
PushNV() to create a stack of words in the list NoVowel so Q4.When Stack is full and an element’s insertion is tried
that it stores only those words which do not have any from it,It is called an……………………
vowel present in it, from the list All. Ans: Overflow
Thereafter, pop each word from the list NoVowel and Q5.When a stack is empty and an element’s deletion is
display the popped word. When the stack is empty, display tried from the stack .it is called an ………………..case.
the message "EmptyStack". For example:If the Words Ans: Underflow
accepted and pushed into the list are Q6.Which of the following isa n application of stack.
['DRY', 'LIKE', 'RHYTHM', 'WORK', 'GYM'] Ans.
Then the stack NoVowel should store ['DRY', 'RHYTHM', 1. Finding the factorial of number
'GYM'] 2. Reversing of strig
And the output should be displayed as 3. Infix to postfix
GYM RHYTHM DRY EmptyStack 4. Postfix evaluation
5. All of these
Ans:
NoVowel=[] Q7. Consider the following code:
def PushNV(N): Push(5)
for i in N: Push(8)
for j in i: Pop
if j in ‘AEIOUaeiou’: Push(2)
break Push(6)
else: Pop
NoVowel.append(i) Pop
Pop
Push(1)
def PopNV(): Pop
while len(NoVowel)>0: The output of the following code is
print(NoVowel.pop()) Ans: 86251
else:
print(“Underflow”) Explanation
When we push 5 and then 8 ,top element of stack is 8,
Q10.Write a function in Python PUSH(Arr), where Arr is a when we pop 8 will out now only 5 is left in stack then push
list of numbers. From this list push all numbers divisible by 2 and 6 now three elements are in stack 526 the top
5 into a stack implemented by using a list. Display the stack element is 5 now pop three times elements pop in reverse
if it has at least one element, otherwise display order 6 then 2 then 5 now stack is empty then push 1 and
appropriate error message. now 1 is at top then pop 1 . so the order of elements is
86251
Ans:
S=[]
def PUSH(Arr):
for i in Arr:
if Arr[i]%5==0:
PUSH.append(Arr[i])

You might also like