How To Use Lists As Stacks And Queues In Python
Last Updated :
06 Mar, 2024
Python, with its simplicity and versatility, provides various data structures that empower developers to efficiently handle and manipulate data. Two commonly used abstract data types are stacks and queues, which can be easily implemented using Python lists. In this article, we will explore how to leverage lists to create stacks and queues, along with the fundamental operations associated with each.
Using List as Stack in Python
A stack is a fundamental data structure in computer science that follows the Last In, First Out (LIFO) principle. In simpler terms, the last element added to the stack is the first one to be removed. Think of it like a stack of plates – you can only take the top plate off the stack, and to add a new plate, you place it on top.
The basic operations associated with a stack include:
- Push: Adding an element to the top of the stack.
- Pop: Removing the element from the top of the stack.
- Peek (or Top): Viewing the element at the top of the stack without removing it.
- IsEmpty: Checking if the stack is empty.
Below are some of the examples by which we can understand using list as a stack in Python:
Push Operation Using List in Python
In this example, a stack is initialized as an empty list, and elements (1, 100, and 23493) are successively added to the top of the stack using the append
method. The print(stack)
statement displays the current state of the stack.
Python3
stack = []
stack.append( 1 )
stack.append( 100 )
stack.append( 23493 )
print (stack)
|
Pop Operation Using List in Python
In this example, a stack represented by the list [1, 2, 4, 4, 100, 212]
is manipulated using the pop
method, which removes and returns the last element. The popped element (212) is displayed, and the updated stack without the popped element is printed using print(stack)
.
Python3
stack = [ 1 , 2 , 4 , 4 , 100 , 212 ]
popped_element = stack.pop()
print ( "Popped Element : " , popped_element)
print (stack)
|
Output
Popped Element : 212
[1, 2, 4, 4, 100]
Top Operation Using List in Python
In this example, a stack is represented by the list [1, 2, 4, 54, 3, 3532, 23]
, and the top element of the stack is accessed using indexing (stack[-1]
). The program then prints the top element, revealing the value at the peak of the stack.
Python3
stack = [ 1 , 2 , 4 , 54 , 3 , 3532 , 23 ]
top_element = stack[ - 1 ]
print ( "Top of Stack: " , top_element)
|
Implementing Stack Using List in Python
In this example, an empty stack is created using a list (stack
). Elements (1, 2, and 3) are then successively pushed onto the stack using the append
method. The program displays the top element of the stack using indexing (stack[-1]
).
Python3
stack = []
stack.append( 1 )
stack.append( 2 )
stack.append( 3 )
print ( "Top of Stack:" , stack[ - 1 ])
stack.append( 4 )
print ( "Top of Stack after inserting an element:" , stack[ - 1 ])
print ( "Stack:" , stack)
popped_element = stack.pop()
print ( "Popped Element:" , popped_element)
print ( "Updated Stack:" , stack)
|
Output
Top of Stack: 3
Top of Stack after inserting an element: 4
Stack: [1, 2, 3, 4]
Popped Element: 4
Updated Stack: [1, 2, 3]
Using List as Queues in Python
A queue is another essential data structure that follows the First In, First Out (FIFO) principle. In a queue, the first element added is the first one to be removed. Queues are commonly used in scenarios where tasks or processes are executed in the order they are received. Some common operations associated with queues include:
- Enqueue: Adding an element to the rear (end) of the queue.
- Dequeue: Removing the element from the front (head) of the queue.
- Front: Viewing the element at the front without removing it.
- Rear: Viewing the element at the rear without removing it.
- IsEmpty: Checking if the queue is empty.
Below are some of the examples by which we can understand about using list as queues in Python:
Enqueue Operation Using List in Python
In this example, a simple Queue
class is defined with an __init__
method to initialize an empty list (self.items
). The class includes an enqueue
method, which appends elements to the end of the list, simulating the enqueue operation in a queue.
Python3
class Queue:
def __init__( self ):
self .items = []
def enqueue( self , item):
self .items.append(item)
queue = Queue()
queue.enqueue( 1 )
queue.enqueue( 2 )
queue.enqueue( 3 )
print (queue.items)
|
Dequeue Operation Using List in Python
In this example, a Queue
class is defined with an __init__
method to initialize an empty list (self.items
). The class includes enqueue
and dequeue
methods. The enqueue
method appends elements to the end of the list, simulating the enqueue operation in a queue. The dequeue
method removes and returns the first element from the list, simulating the dequeue operation.
Python3
class Queue:
def __init__( self ):
self .items = []
def enqueue( self , item):
self .items.append(item)
def dequeue( self ):
if self .items:
return self .items.pop( 0 )
else :
raise IndexError( "Queue is empty. Cannot dequeue." )
queue = Queue()
queue.enqueue( 1 )
queue.enqueue( 2 )
print ( "Before Deque:" ,queue.items)
dequeued_element = queue.dequeue()
print ( "After Deque:" ,queue.items)
|
Output
Before Deque: [1, 2]
After Deque: [2]
Front Operation Using List in Python
In this example, a Queue
class is defined with an __init__
method to initialize an empty list (self.items
). The class includes an enqueue
method to append elements to the end of the list, simulating the enqueue operation in a queue. Additionally, a front
method is implemented to retrieve the element at the front of the queue without removing it.
Python3
class Queue:
def __init__( self ):
self .items = []
def enqueue( self , item):
self .items.append(item)
def front( self ):
if self .items:
return self .items[ 0 ]
else :
raise IndexError( "Queue is empty. No front element." )
queue = Queue()
queue.enqueue( 1 )
queue.enqueue( 2 )
front_element = queue.front()
print (front_element)
|
Share your thoughts in the comments
Please Login to comment...