0% found this document useful (0 votes)
4 views2 pages

Stack Operations in Python Code

The document contains Python code snippets demonstrating basic stack operations such as push, peek, stack overflow/underflow checks, isEmpty, pop, and clear. Each function performs a specific operation on a stack, handling cases like empty stacks and overflow conditions. The code showcases the use of lists to implement stack behavior and includes error handling for various scenarios.

Uploaded by

ashvikadoss
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)
4 views2 pages

Stack Operations in Python Code

The document contains Python code snippets demonstrating basic stack operations such as push, peek, stack overflow/underflow checks, isEmpty, pop, and clear. Each function performs a specific operation on a stack, handling cases like empty stacks and overflow conditions. The code showcases the use of lists to implement stack behavior and includes error handling for various scenarios.

Uploaded by

ashvikadoss
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

1.

'''stack=[]
def push():
element = input("Enter element to push")
[Link](element)
print(stack,"element has been pushed")
push()

2.stack2=[1,2,3,4,5]
def peek():
print("'\n\nPeek operation allows the user to see the topmost of the last or latest element
which has been \nappended or added to the stack which is basically a list'")
print("\n\n",stack2[-1])
peek()

[Link]=[1,2,3,4]
stackunderflow=[]
def stackflow():
m=4
if len(stackoverflow)==m:
print("Stack Overflow!")
if len(stackunderflow)==0:
print("Stackunderflow!")
stackflow()

[Link]=[1,]
def isEmpty():
if len(stack)==0:
print("stack is empty")
else:
print("Stack is not empty")
isEmpty()'''

5.'''stack=[1,2,3]
def pop():
if len(stack)==0:
print("Stack underflow!")
else:
print("Element Popped",[Link]())
pop()'''
[Link]=[1,2,3]
def clear():
If len(stack)==0:
print(“the sack is empty”)
else:
[Link]()
print(“Stack has been cleared successfully”)
print(stack)
clear()”

You might also like