What is a Stack ?

Stack is a structure in which items are stored and collected in LIFO order. LIFO means Last In First Out. We can see several stacks in our day to day life. A simple example of stack using paper is shown below. In this arrangement, the paper is stacked from bottom to top order and it will be taken back from top to bottom order.

stack

 

The insert and delete operations are often called push and pop. The schematic diagram of a STACK is given below. Here you can see how the items are pushed and taken out from the STACK.

 

stack01

In Python world, Stack can be implemented in the following methods.

  • list
  • queue.LifoQueue
  • collection.deque

 

Stack Implementation using LIST in Python

The native data structure list can be used as a stack. A simple list is given below.

[1,2,3,4,5,6,7,8]

The push operation can be performed by using the append() function in the list and the pop operation can be performed using pop() function. This usage of append() and pop() function will create a LIFO behavior and this can be used as a simple implementation of stack. The performance of the stack created using list will reduce with larger data. This is ideal for handling small amount of data.

The following program shows a simple implementation of stack using python list


my_first_stack = []
# Push elements to stack using append()
my_first_stack.append('edward')
my_first_stack.append('sabitha')
print('My First Stack')
print(my_first_stack)
#Retrieve elements from the stack
print('\nElements retrieved from the stack:')
print(my_first_stack.pop())
print(my_first_stack.pop())
#Now print the stack and see the remaining elements
print(my_first_stack)

 

Stack Implementation using LifoQueue (Queue) in Python

Stack can be implemented using the LifoQueue function in the Python Queue module. A simple implementation is given below. The program is self explanatory.


from queue import LifoQueue as lq
# Initializing a stack with max size as 2
sample_stack = lq(maxsize=2)
# The qsize() function shows the size of the stack
print(sample_stack.qsize())
# Now lets push some elements to stack
# put() will push elements to stack
sample_stack.put('edward')
sample_stack.put('sabitha')
# Retrieve the elements from the stack
print('\nRetrieved elements from Stack in LIFO order')
print(sample_stack.get())
print(sample_stack.get())
print("\nIs the stack empty ? : ", sample_stack.empty())

Stack Implementation using Deque in Python Collections module.

This approach is similar to that of the implementation using LIST. This will be more efficient than the implementation using the list. The sample program is given below. The program is self explanatory.


from collections import deque as dq
sample_stack = dq()
# Push elements to stack using append() function
# This is similar to the way we push elements to list
sample_stack.append('Sabitha')
sample_stack.append('Edward')
# Print all the elements in the stack
print('All the elements in the stack')
print(sample_stack)
# Retrieve the elements in LIFO order
print('\nRetrieve the elements from stack:')
print(sample_stack.pop())
print(sample_stack.pop())

Advertisement