0% found this document useful (0 votes)
5 views4 pages

Stack Operations in Python

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)
5 views4 pages

Stack Operations in Python

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

CHAPTER - 2

STACK

GLASS STACK :
glassStack = list()

TO DEFINE THE STACK IS EMPTY :

def isEmpty (glassStack):


if len(glassStack)==0:
return True
else:
return False

TO ADD AN ELEMENT TO THE STACK :

def opPush (glassStack, element):


[Link](element)

TO CHECK THE SIZE OF THE STACK :

def size (glassStack):


return len (glassStack)

TO READ THE MOST ELEMENT :

def top (glassStack):


if isEmpty (glassStack);
print('Stack is empty')
return None
else:
x -len (glassStack)
element-glassStack [x-1]
return element

TO DELETE AN ELEMENT FROM STACK :

def opPop (glassStack):


if isEmpty (glassStack):
print('underflow")
return None
else:
return ([Link]())
TO DISPLAY THE STACK :

def display (glassStack):


x-len (glassStack)
print ("Current elements in the stack are: ")
for i in range (x-1,-1,-1):
print (glassStack[i])

glassStack = list()
element-'glass1'
print("Pushing element ",element)
opPush (glassStack, element)
element-'glass2'
print("Pushing element ",element)
opPush (glassStack, element)
print("Current number of elements is", size (glassStack))
element-opPop (glassStack)
print("Popped element is", element)
element='glass3'
print("Pushing element ",element)
opPush (glassStack, element)
print("top element is", top (glassStack))
display (glassStack)
while True:
item-opPop (glassStack)
if item == None:
print("Stack is empty now")
break
else:
print("Popped element is",item)

( Infix notation is a way of writing mathematical and logical expressions where the
operators are placed between the operands. It is the standard notation used in arithmetic
and algebra. )

( Prefix notation is a way of writing expressions where the operator appears before the
operands. It eliminates the need for parentheses because the order of operations is
inherently clear. )

( Postfix notation is a way of writing expressions where the operator appears after the
operands. It eliminates the need for parentheses because the order of operations is
inherently clear. )
Algorithm to convert a given infix expression (x + y)/(z*8) into equivalent postfix expression
using a stack.

You might also like