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

Stack Notes

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

Stack Notes

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

A Stack is a linear data structure that holds a linear, ordered sequence of elements.

It is
an abstract data type. A Stack works on the LIFO process (Last In First Out), i.e., the
element that was inserted last will be removed first. To implement the Stack, it is
required to maintain a pointer to the top of the Stack, which is the last element to be
inserted because we can access the elements only on the top of the Stack.

def push(a,val):
a.append(val)
def pop(a):
item=a.pop()
print("popped item=",item)
def peek(a):
last=len(a)-1
print("peek element=",a[last])
def display(a):
for i in range(len(a)-1,-1,-1):
print(a[i])

a=[]
while True:
choice=int(input("enter choice"))
if choice==1:
val=int(input("Enter number to insert:"))
push(a,val)
print("Element pushed successfully")
elif choice==2:
if len(a)==0:
print("Stack underflow")
else:
pop(a)
elif choice==3:
if len(a)==0:
print("Stack underflow")
else:
peek(a)
elif choice==4:
if len(a)==0:
print("Stack underflow")
else:
display(a)
else:
break;
Output:
enter choice1
Enter number to insert:20
Element pushed successfully
enter choice1
Enter number to insert:15
Element pushed successfully
enter choice2
popped item= 15
enter choice3
peek element= 20
enter choice4
20
Example 2

A list, NList contains following record as list elements:


[City, Country, distance from Delhi]
Each of these records are nested together to form a nested list. Write the
following user defined functions in Python to perform the specified
operations on the stack named travel.
(i) Push_element(NList): It takes the nested list as an
argument and pushes a list object containing name of the city and
country, which are not in India and distance is less than 3500 km
from Delhi.
(ii) Pop_element(): It pops the objects from the stack and displays
them. Also, the function should display “Stack Empty” when there
are no elements in the stack.
For example: If the nested list contains the following data:
NList=[["New York", "U.S.A.", 11734],
["Naypyidaw", "Myanmar", 3219],
["Dubai", "UAE", 2194],
["London", "England", 6693],
["Gangtok", "India", 1580],
["Columbo", "Sri Lanka", 3405]]
The stack should contain:
['Naypyidaw', 'Myanmar'],
['Dubai', 'UAE'],
['Columbo', 'Sri Lanka']
The output should be:
['Columbo', 'Sri Lanka']
['Dubai', 'UAE']
['Naypyidaw', 'Myanmar']
Stack Empty
Answer:-
Question 3
A list contains following record of a customer:
[Customer_name, Phone_number, City]
Write the following user defined functions to perform given operations
on the stack named ‘status’:
(i) Push_element() - To Push an object containing name and
Phone number of customers who live in Goa to the stack
(ii) Pop_element() - To Pop the objects from the stack and
display them. Also, display “Stack Empty” when there are no
elements in the stack.
For example:
If the lists of customer details are:
[“Gurdas”, “99999999999”,”Goa”]
[“Julee”, “8888888888”,”Mumbai”]
[“Murugan”,”77777777777”,”Cochin”]
[“Ashmit”, “1010101010”,”Goa”]
The stack should contain
[“Ashmit”,”1010101010”]
[“Gurdas”,”9999999999”]
The output should be:
[“Ashmit”,”1010101010”]
[“Gurdas”,”9999999999”]
Stack Empty
Ans:-

Question no 4
Write a function in Python, Push(SItem) where , SItem is a dictionary
containing the details of stationary items– {Sname:price}.
The function should push the names of those items in the stack who
have price greater than 75. Also display the count of elements pushed
into the stack.
For example:
If the dictionary contains the following data:
3
9
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
The stack should contain
Notebook
Pen
The output should be:
The count of elements in the stack is 2

Ans:-
Q- 5 Write a function to push student info
into stack

You might also like