Stacks vs Queues in python

·

3 min read

Introduction: If you ever faced difficulties in differentiating stacks and queues in python, you are probably in the right place. At the end of the blog, I can assure you that you will have good knowledge of stacks and queues and their properties

Stacks:

A Stack is a linear data structure implemented in LIFO[Last In First Out] manner where insertion and deletion are restricted to occur at only one end - The stack's Top LIFO means the last element inserted would be the first to be deleted.

Data can be removed only from the top i.e the element at the top of the stack, The removal of an element from a stack is called POP operation

new data elements can only be added to the top of the stack (push). The insertion of an element in a stack is called a PUSH operation

Lifo_stack.svg.png

Other Stack Terms:

Peek: This refers to inspecting the value at the Stack's top without removing it. It is also sometimes referred to as Inspection

Overflow: It refers to a situation where one tries to add an element into the stack even after the stack is filled

Underflow: It refers to a situation where one tries to delete an element from an empty stack

Application of stack:

  1. One of the most important features of the stack is Backtracking. Backtracking is mostly used in a large number of puzzles like sudoku and optimization problems such as the Knapsack problem

  2. The compilers use stack to store the previous state of a program when it is called

QUEUE:

A queue is similar to the stacks but the most important thing is, Queue is implemented in a FIFO[First In First Out] manner, A queue has two ends which are termed as front-end and back-end or the rear-end of the queue. Items are always added to the rear end and are removed from the front end. EX: Queue of customers in the billing line of a supermarket

Data can be removed only from the front end, i.e the element at the front end of the queue, The removal of an element from a queue is called DEQUEUE operation

new data elements can only be added to the end of the queue. The insertion of an element in a queue is called an ENQUEUE operation

fifo.png

Other Queue Terms:

Peek: This refers to inspecting the value at the Queues front without removing it. It is also sometimes referred to as Inspection

Overflow: It refers to a situation where one tries to add an element into the stack even after the queue is full

Underflow: It refers to a situation where one tries to delete an element from an empty queue

Application of queues:

Airport authorities make use of ques in the situation of sharing a single runway of the airport for both landing and take-off flights

When you make calls to a call center, the call center phone systems will use a queue to hold people in line until a service representative is free