0% found this document useful (0 votes)
3 views8 pages

Stack Implementation in Python

Uploaded by

nibhadevi3652
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)
3 views8 pages

Stack Implementation in Python

Uploaded by

nibhadevi3652
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: A

stack is a linear structures implemented in LIFO (Last In First Out)


manner where insertions and deletions are restricted to occur only at one end
– stack’ s top.

Example of overflow:
Underflow
Implementation of stack
1.​ Using List
Example:
stack = []
[Link]('a')
[Link]('b')
[Link]('c')

print('Initial stack')
print(stack)

print('\nElements popped from stack:')


print([Link]())
print([Link]())
print([Link]())

print('\nStack after elements are popped:')


print(stack)

2.​ Using [Link]

from collections import deque

stack = deque()
[Link]('a')
[Link]('b')
[Link]('c')

print('Initial stack:')
print(stack)

print('\nElements popped from stack:')


print([Link]())
print([Link]())
print([Link]())

print('\nStack after elements are popped:')


print(stack)

3.​ Using [Link]


from queue import LifoQueue

stack = LifoQueue(maxsize=3)

print([Link]())

[Link]('a')
[Link]('b')
[Link]('c')
print("Full: ", [Link]())
print("Size: ", [Link]())

print('\nElements popped from the stack')


print([Link]())
print([Link]())
print([Link]())

print("\nEmpty: ", [Link]())

Another example of stack Implementation Using List:

You might also like