Data structure is a way of organizing and storing data in a computer program so that it can be easily accessed and managed. We can perform various data operations on data effectively such as traveling, searching, sorting, insertion, and deletion of data.
Stacks in Data Structures is a linear type of data structure that follows the LIFO (Last-In-First-Out) principle and allows insertion and deletion operations from one end of the stack data structure, that is called top of the stack
Define push, pop and display function of stack . Push function to add numbers divisible by 5 only in to stack
l=[1,44,55,6,7,6,5,45,35,15]
s=[]
def push(l):
for i in l:
if i%5==0:
s.append(i)
def display(s):
if len(s)==0:
print("stack is empty")
return
else:
for i in s:
print(i,end=" ")
def pop(s):
if(len(s))==0:
print("stack is empty")
else:
print("Item deleted", s.pop())
push(l)
display(s)
pop(s)
display(s)
pop(s)
display(s)
# STACK OF BOOKS USING dictionary
s={}
def push(s):
n=int (input("Enter the numbers of book you want to enter"))
for i in range(0,n):
bno=input("Enter book number")
bname=input("Enter book name")
s[bno]=bname
def display(s):
if len(s)==0:
print("stack is empty")
return
else:
for i in s.items():
print(i,end=" ")
print()
def pop(s):
if len(s)==0:
print("stack is empty")
return
else:
s.popitem()
push(s)
display(s)
pop(s)
display(s)
# STACK OF STUDENTS(ROLLNO, NAME,MARKS) USING dictionary
s={}
def push(s):
n=int (input("Enter the numbers of students for you want to enter details"))
for i in range(0,n):
rno=input("Enter rollno")
name=input("Enter name")
marks=int(input("Enter marks"))
s[rno]=[name,marks]
def display(s):
if len(s)==0:
print("stack is empty")
return
else:
for i in s.items():
print(i,end=" ")
print()
def pop(s):
if len(s)==0:
print("stack is empty")
return
else:
s.popitem()
push(s)
display(s)
pop(s)
display(s)
print(type(s))
l=[1,44,55,6,7,6,5,45,35,15]
s=[]
def push(l):
for i in l:
if i%5==0:
s.append(i)
if len(s)==0:
print("stack is empty")
return
else:
for i in s:
print(i,end=" ")
push(l)
[“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
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:
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
Objective type questions of Data Structure stack
[1] A ____________ is a way to store, organize, or manage data in efficient and productive manner.
Data Structure
[2] A stack is one of the following types of data structure?
a) Linear b) Dynamic c) Circular d) All of these
[3] Stack data structure is following ____________ principle.
LIFO
[4] In stack data can be inserted or deleted from ____________ only.
Top
[5] The insert operation in the stack is known as pop. (True/False)
[6] You can replace any element position in the stack. (True/False)
[7] The peek operation refers to accessing/inspecting the top element in the stack. (True/False)
[8] A condition raise due to the stack is full is known as ___________.
a) Underflow b) Overflow c) List is full d) Completely Filled
[9] While popping the element from the stack, a condition will be raised, this condition is known as ____________.
a) Underflow b) Overflow c) List is Empty d) Blank List
[10] Stack overflow condition is raised in ____________ operation where as Stack underflow condition is raised in _____________ operations.
Push, Pop