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

Stack Using List

The document provides a Python implementation of a stack using a list, including functions to check if the stack is empty, push elements, pop elements, and display the stack contents. It also includes a main program that presents a menu for user interaction to perform these stack operations. The program continues to run until the user chooses to exit.

Uploaded by

rajdeepsachin145
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)
2 views4 pages

Stack Using List

The document provides a Python implementation of a stack using a list, including functions to check if the stack is empty, push elements, pop elements, and display the stack contents. It also includes a main program that presents a menu for user interaction to perform these stack operations. The program continues to run until the user chooses to exit.

Uploaded by

rajdeepsachin145
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

# Stack using List

stack = []

# Function to check if stack is empty

def isEmpty():

if len(stack) == 0:

return True

return False

# Function to push an element

def push(item):

[Link](item)

print(item, "pushed into stack")

# Function to pop an element

def pop():

if isEmpty():

print("Stack is Empty")

else:

print([Link](), "popped from stack")

# Function to display stack

def display():

if isEmpty():
print("Stack is Empty")

else:

print("Stack elements are:")

for i in range(len(stack) - 1, -1, -1):

print(stack[i])

# Main Program

while True:

print("\n----- STACK MENU -----")

print("1. Push")

print("2. Pop")

print("3. Check Empty")

print("4. Display")

print("5. Exit")

choice = int(input("Enter your choice: "))

if choice == 1:

item = int(input("Enter element to push: "))

push(item)

elif choice == 2:

pop()

elif choice == 3:

if isEmpty():

print("Stack is Empty")
else:

print("Stack is Not Empty")

elif choice == 4:

display()

elif choice == 5:

print("Program Ended")

break

else:

print("Invalid Choice")

----- STACK MENU -----

1. Push

2. Pop

3. Check Empty

4. Display

5. Exit

Enter your choice: 1

Enter element to push: 10

10 pushed into stack


Enter your choice: 1

Enter element to push: 20

20 pushed into stack

Enter your choice: 4

Stack elements are:

20

10

Enter your choice: 2

20 popped from stack

Enter your choice: 3

Stack is Not Empty

You might also like