DEV Community

Cover image for Queue and Stack Essentials: A Python Programmer's Guide
Augustine Alul
Augustine Alul

Posted on • Edited on

Queue and Stack Essentials: A Python Programmer's Guide

Introduction
Data structures in programming can be seen as a way of storing and organizing data to ensure that the data stored therein can be efficiently utilized in a program, thereby improving the functionality of the software it was applied.
Most data structures are language agnostic; in that, they can be implemented using any programming language of choice. Although some programming languages have their specific in-built data structures, still, the concept of data structures remains the same regardless; which is to store and organize data for programming applications.

In this article, we will explore queue and stack data structures using Python. For optimum understanding, it is requisite that you know basic programming preferably in Python programming language.

Queue and stack are an example of linear data structures, and by that, I mean that each constituent element is sequentially linked to another. Think of a chain, for instance, each link is connected to the next one.
Data stored in a linear data structure can be accessed one after another while traversing. Let’s delve into queue and stack individually for more clarity.

Queue Data Structure
Queues operate on the 'First-in-First-out' principle(FIFO), what this means is that retrieval and insertion of data in a queue data structure occur at the beginning and end respectively and the right term for this operation is dequeuing and enqueueing. In a real-world scenario, a queue can be likened a a system whereby people are lined up in a McDonald’s outlet to get pizza, and they are attended to according to the time they came, the persons who came earlier are always in the front of the the queue while those who came later queue behind them. Dequeuing happens at the front when someone takes his/her turn and eventually buys the pizza and leaves, enqueueing occurs at the end when a new person joins the queue.
The Python implementation of a queue is demonstrated in the code snippet below.

#creating an object of the dequeue class
my_queue = dequeue(['first_person', 'second_person', 'third_person', 'fourth_person'])

#enqueueing 

my_queue.append('fifth_person')
print(my_queue) # outputs ['first_person', 'second_person', 'third_person', 'fourth_person', 'fifth_person']

#dequeueing 
my_queue.leftpop()
print(my_queue)
# outputs ['second_person', 'third_person', 'fourth_person', 'fifth_person'] 
Enter fullscreen mode Exit fullscreen mode

From the code snippet above, we have been able to demonstrate the enqueueing and dequeueing operations of a queue data structure.

Stack Data Structure
Stack data structure follows the 'Last-in-First-out'(LIFO) principle, retrieving and addition of data occurs at the top of the stack, this implies that the last element that is added to the stack is the first element to be retrieved/removed.
In a real-life scenario, a stack can be likened to a pile of dishes to be washed, the first plate in the pile of dishes is positioned at the bottom and the last dish added is at the top of the stack, this simply implies that if we are to start picking out the dishes for washing, we naturally start picking from the top which happens to be the last dish added to the stack of dishes, hence we are unknowingly following the LIFO approach.
Below is a code snippet to demonstrate stack operation using Python.

#creating a stack object
dish_stack = dequeue(['first_dish', 'second_dish', 'third_dish'])

#adding data to the stack
dish_stack.append('fourth_dish')
print(dish_stack) #outputs ['first_dish', 'second_dish', 'third_dish', 'fourth_dish']

#removing data from the stack
dish_stack.pop()
print(dish_stack) #outputs ['first_dish', 'second_dish', 'third_dish']
Enter fullscreen mode Exit fullscreen mode

To reiterate, queue and stack are an example of linear data structures. The queue data structure operates on the FIFO principle meanwhile the stack operates on LIFO.
Although the dequeue class has some other methods for manipulating queue and stack objects that were not included in this article, the methods used in this article are sufficient to ensure a basic understanding of how queue and stack data structures primarily behave.

Thanks for reading.

Top comments (0)