0% found this document useful (0 votes)
22 views9 pages

ch9 Datastructure12

Uploaded by

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

ch9 Datastructure12

Uploaded by

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

Chapter 9

Data Structure - Stack


Initial value:
top=-1
stack=[]

Operation in stack:

I. Push()
stack.append(item)
top=top+1

II. Pop()
(i) Check stack exist with value
stack==[]:
print("Deletion Not possible. \n Stack empty")
(ii) otherwise delete the top position value
print("Deleted element is",stack[top])
top=top-1
stack.pop()

III. Display
to display value from top to bottom.
1. Write a program to implement stack using integer list.
top=-1
stack=[]
def cls():
print("\n"*100)
def push(stack,item):
global top
stack.append(item)
top=top+1
def pop(stack):
global top
if stack==[]:
print("Deletion Not possible. \n Stack empty")
else:
print("Deleted element is",stack[top])
top=top-1
stack.pop()
def display(stack):
if stack==[]:
print("Stack empty")
else:
for i in range(top,-1,-1):
print(stack[i])
ch='y'
while ch=='y':
cls()
print("Stack Operation")
print("1. Push")
print("2. Pop")
print("3. Display")
choice=int(input("Enter your choice"))
if choice==1:
item=int(input("Enter any value"))
push(stack,item)
elif choice==2:
pop(stack)
elif choice==3:
display(stack)
ch=input("Do you Want to cotinue or not[y/n]")
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 POP(Arr), where Arr is a stack implemented by a list of numbers.
The function returns the value deleted from the stack.

You might also like