[go: up one dir, main page]

0% found this document useful (0 votes)
10 views7 pages

Stack Implementation

The document outlines a stack implementation in Python, featuring functions to push, pop, and display stack elements. It includes a user interface that allows for continuous interaction until the user decides to quit. The implementation handles basic stack operations and provides feedback for each action performed.

Uploaded by

Kasthuri L
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)
10 views7 pages

Stack Implementation

The document outlines a stack implementation in Python, featuring functions to push, pop, and display stack elements. It includes a user interface that allows for continuous interaction until the user decides to quit. The implementation handles basic stack operations and provides feedback for each action performed.

Uploaded by

Kasthuri L
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/ 7

STACK IMPLEMENTATION

def push(stk,item):

stk.append(item)

print("%d is succesfully added"%item)

def s_pop(stk):

if stk==[]:

print("stack is empty")

else:

i=stk.pop()

return i

def display(stk):

a=stk[::-1]

print(a)

stk=[]

print("\t\t\t STACK IMPLEMENTATION")

print("\t\t\t *************************")

while True:

print(" 1.Push\n 2.Pop\n 3.Display\n 4.Quit")

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

if ch==1:

no=int(input("Enter the number to be added:"))

push(stk,no)

elif ch==2:

item=s_pop(stk)

print("%d is popped"%item)
elif ch==3:

display(stk)

elif ch==4:

break

else:

print("Invalid operation!!!")
OUTPUT:

STACK IMPLEMENTATION

*************************

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:1

Enter the number to be added:4

4 is succesfully added

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:1

Enter the number to be added:7

7 is succesfully added

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:1

Enter the number to be added:2

2 is succesfully added
1.Push

2.Pop

3.Display

4.Quit

Enter your choice:1

Enter the number to be added:8

8 is succesfully added

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:1

Enter the number to be added:3

3 is succesfully added

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:3

[3, 8, 2, 7, 4]

1.Push

2.Pop

3.Display

4.Quit
Enter your choice:2

3 is popped

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:23

Invalid operation!!!

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:3

[8, 2, 7, 4]

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:2

8 is popped

1.Push

2.Pop

3.Display

4.Quit
Enter your choice:2

2 is popped

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:2

7 is popped

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:3

[4]

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:2

4 is popped

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:3


[]

1.Push

2.Pop

3.Display

4.Quit

Enter your choice:4

You might also like