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

Stack Notes

stack

Uploaded by

9g19harishv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Stack Notes

stack

Uploaded by

9g19harishv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1)FROM DICTIONARY TO STACK

stack=[]
top=None
d=eval(input("Enter dictionary:"))
def Push(d):
global stack,top
for key in d:
if d[key]>49:
stack.append(key)
top=len(stack)-1
def Pop():
global top,stack
while True: #while top!=None:
if stack==[]:
print("Underflow")
top=None
break
else:
print(stack.pop())
top=len(stack)-1
Push(d)
Pop()

Note:If display the elements of the stack is


asked,the following code to be added

def display():
global top,stack
if stack==[]:
top=None
print(“Stack is empty”)
else:
top=len(stack)-1
for i in range(top,-1,-1):
print(stack[i])
2)FROM LIST TO STACK
stack=[]
set1=eval(input('Enter list of numbers:'))
top=None
def PUSH(stack,set1):
global top,stack
for i in range(len(set1)):
if set1[i]%10==8:
stack.append(set1[i])
top=len(stack)-1

def Pop():
global top,stack
while True:
if stack!=[]:
print(stack.pop())
top=len(stack)-1
else:
print('Underflow')
break

PUSH(stack,set1)
Pop()

3)FROM STRING TO STACK

WAP TO INPUT A STRING STR.


push(STR) - PUSH THE UPPERCASE LETTERS INTO THE
STACK R.
Pop() - POP AND DISPLAY THE ELEMENTS IN THE STACK

R=[]
t=None
STR=eval(input("Enter string:"))
def push(STR):
global t,R
for i in STR:
if i.isupper():
R.append(i)
t=len(R)-1
def Pop():
global t,R
while True:
if R==[]:
t=None
print("Stack is empty")
break
else:
R.pop()
t=len(R)-1
Push(d)
Pop()

4)FROM Array of list(Nested list) TO STACK


stack=[]
top=None
l=[['Gurudas','99999999','Goa'],['Julie','88888888','Mumbai'],
['Murugan','77777777','Cochin'],['Ashmit','101010101'
,'GOA']]
#l=eval(input("Enter a list of records:"))
def Push():
global stack,top
for i in l:
if i[2].lower()=='goa':
stack.append([i[0],i[2]])
top=len(stack)-1
def Pop():
global top,stack
while stack:
print(stack.pop())
top=len(stack)-1
else:
print("Stack Empty")
top=None
Push()
Pop()

5)PUSH PRIME NOS INTO A STACK


Write a menu driven program in Python to
implement
a Book Stack using a list data structure.
Each node should have
• Book no
• Book name
• Book price
Write a menu driven program in Python to implement
a Book Stack using a list data structure.
Each node should have
• Book no
• Book name
• Book price
Top=None
Book=[]
def push():
global Top.Book
bno=int(input('Enter book number :'))
bname=input('Enter book name :')
bprice=float(input('Enter price :'))
Book.append([bno,bname,bprice])
Top=len(Book)-1
def pop():
global Top,Book
if Book==[]:
print('Underflow')
Top=None
else:
print('Deleted book ',Book.pop())
Top=len(Book)-1

def display():
global Top,Book
if Book==[]:
print(“Stack is empty”)
else:
Top=len(Book)-1
for i in range(Top,-1,-1):
print(Book[i])
def Peek():
global Top,Book
if Book==[]:
print(“Stack is empty”)
else:
Top=len(Book)-1
print(Book[Top])
while True:
print('1 - Push')
print('2 - Pop')
print('3 - Display')
print('4 - Peek)
print('5 - Exit')
ch=int(input('Enter your choice '))
if ch==1:
push()
elif ch==2:
pop()
elif ch==3:
display()
elif ch==4:
Peek()
elif ch==5:
break
else:
print('Invalid choice')

THEORY QUESTIONS
 What is a stack?
A stack is a linear datastructure that follows the
LIFO method in which insertion and deletion takes
place at one end(from top)
 List the applications of stack
Applications of Stack
1.Reversal of a string,list or word.
2.Backtracking of web page in a website
3.Undo operation
4.Expression evaluation
5.Recursive function call
 Peek – Inspecting the topmost element
 Underflow – Trying to delete an element from an
empty stack
 Overflow – Trying to insert elements to a stack
which is full.This situation does not arise in Python
as Lists are dynamic in size

You might also like