Skip to content
  • Linkedin
  • Youtube
  • Pinterest
  • Home
  • Privacy Policy
  • About
  • Contact
Programmingoneonone

Programmingoneonone

Programmingoneonone is a website that publishes daily tutorials, methods, guides, and articles on IT, Education, and technology.

  • Home
  • Human Values
  • DSA
  • IoT Tutorials
  • Interview Questions and Answers
  • Toggle search form
queue in data structure and algorithms

Queue in Data Structure

Posted on 27 May 202023 April 2023 By YASH PAL No Comments on Queue in Data Structure

A queue is an abstract data structure in which the insertion is performed on one end and deletion is performed on the other end.

Queue in Data structures and algorithms

Example of Queue in Data Structures

In the above-given image, you see that the customers are standing in a line. and the customer who entered the line will go first means First in First out. and we can add the new customers in line at the end of the line and will go out from the beginning of the line.

so the queue follows the FIFO ( First in First out ) process. and we can insert a new value from one end and delete the first inserted value from the other end.

Enqueue operation

To insert a new value in the queue is called the Enqueue operation in the queue

Dequeue operation

To delete the first entered value in the queue is called the Dequeue operation in the queue.

Front end 

the end from which we delete the value in the queue is called the Front end of the queue.

Rear-end

the end from which we insert a new value in the queue is called the rear end of the queue.

Queue in Data structures and algorithms

if we want to enter a new value then we use the rear end and if we want to delete the value then we use the front end.

Queue in Data structures and algorithms

Implementation of the queue using the array.

Queue in Data structures and algorithms

To implement a queue using an array first we need to choose the position of the front end and rear end. we use the first index of the array as the front end and the last index as the rear end.

Queue in Data structures and algorithms

In the above example first, we take an empty array and then we enqueue the values 20, 30, 15, and 40 after that, we dequeue the values 20, and 30 from the front end, and then we again enqueue the value 56, and then dequeue the value 15.

Program to implement queue using array in Python.

class EmptyQueueError(Exception):
    pass
class Queue:

    def __init__(self):
        self.items = []

    def is_empty(self):
        return self.items == []

    def size(self):
        return len(self.items)

    def enqueue(self,item):
        self.items.append(item)

    def dequeue(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is Empty")
        return self.items.pop(0)

    def peek(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is Empty")
        return self.items[0]

    def display(self):
        print(self.items)

##############


if __name__ == '__main__':
    qu = Queue()

    while True:
        print("1. Enqueue")
        print("2. Dequeue")
        print("3. Peek")
        print("4. Size")
        print("5. Display")
        print("6. Quit")

        choice = int(input("Enter your choice : "))

        if choice == 1:
            x = int(input("Enter the element : "))
            qu.enqueue(x)
        elif choice == 2:
            x=qu.dequeue()
            print("Element deleted fro the queue is : ", x)
        elif choice == 3:
            print("Element at the front end is ", qu.peek())
        elif choice == 4:
            print("Size of queue ", qu.size())
        elif choice == 5:
            qu.display()
        elif choice == 6:
            break        else:
            print("Wrong choice ")
        print()

Implementation of the queue using the linked list

Queue in Data structures and algorithms

To implement a queue using the linked list we first store the first node and last node’s reference into the front-end rear variables. because using the front variable we perform deletion in a queue and using the rear variable we perform insertion in a queue.

so the beginning of the list is called the front end of the queue and the end of the list is called the rear end of the queue.

Dequeue operation

we already know how to delete the first node of the linked list and delete the node in the only linked list. and if you don’t know then please go through this tutorial
Deletion in the linked list

Queue in Data structures and algorithms

Enqueue operation

we already know how to insert a new node at the end of the linked list. and if you don’t know then please go through this tutorial
Insertion in the linked list

Queue in Data structures and algorithms

Program to implement Queue using linked list in Python

class EmptyQueueError(Exception):
    pass

class Node:

    def __init__(self, value):
        self.info = value
        self.link = None

class Queue:

    def __init__(self):
        self.rear = None
    def is_empty(self):
        return self.rear is None
    def size(self):
        if self.is_empty():
            return 0        n = 0        p = self.rear.link
        while True:
            n += 1            p = p.link
            if p == self.rear.link:
                break        return n

    def enqueue(self, data):
        temp = Node(data)

        if self.is_empty():
            self.rear = temp
            self.rear.link = self.rear
        else:
            temp.link = self.rear.link
            self.rear.link = temp
            self.rear = temp

    def dequeue(self):

        if self.is_empty():
            raise EmptyQueueError("Queue is Empty ")

        if self.rear.link == self.rear:
            temp = self.rear
            self.rear = None        else:
            temp = self.rear.link
            self.rear.link = self.rear.link.link
        return temp.info

    def peek(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is Empty")
        return self.rear.link.info

    def display(self):
        if self.is_empty():
            print("List is empty")
            return
        p = self.rear.link
        while True:
            print(p.info, " ", end='')
            p = p.link
            if p == self.rear.link:
                break        print()


#####################

if __name__ == '__main__':
    qu = Queue()

    while True:
        print("1. Enqueue")
        print("2. Dequeue")
        print("3. Peek")
        print("4. Size")
        print("5. Display")
        print("6. Quit")

        choice = int(input("Enter your choice : "))

        if choice == 1:
            x = int(input("Enter the element : "))
            qu.enqueue(x)
        elif choice == 2:
            x = qu.dequeue()
            print("Element deleted from the queue is : ", x)
        elif choice == 3:
            print("Element at the front end is ", qu.peek())
        elif choice == 4:
            print("Size of queue ", qu.size())
        elif choice == 5:
            qu.display()
        elif choice == 6:
            break        else:
            print("Wrong choice")
        print()
Computer Science Tutorials, Data Structures Tutorials Tags:computer science, Data Structure

Post navigation

Previous Post: Stack in Data structure
Next Post: Circular queue in Data structure

Related Tutorials

python libraries for text transformation REPHRASING REVOLUTION: HOW PYTHON LIBRARIES FACILITATE TEXT TRANSFORMATION Computer Science Tutorials
Introduction to Statistics for Data Science: Building a Solid Foundation Computer Science Tutorials
programming languages for machine learning and data science Top Programming Languages For Machine Learning Computer Science Tutorials
How to Become a Successful Data Engineer in the Data Science Field – Complete Guide Computer Science Tutorials
Is Python a good language for Machine Learning/AI? Computer Science Tutorials
basics of boolean algebra Its Operators, Laws, and Examples Basics of Boolean Algebra: Its Operators, Laws, and Examples Boolean Algebra

Leave a Reply

You must be logged in to post a comment.

  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

  • Algorithms Tutorials
  • Basic Programming
  • Boolean Algebra
  • C Programming Tutorials
  • C++ Tutorials
  • Compiler Design Tutorials
  • Computer Networks Tutorials
  • Computer Organization Tutorials
  • Computer Science Tutorials
  • Data Structures Tutorials
  • DBMS Tutorials
  • Developer Guide
  • Digital Communication
  • Digital Logic Tutorials
  • Internet of Things Tutorials
  • Internet Tutorials
  • Interview questions answers
  • Java Tutorials
  • Javascript Tutorials
  • Linux
  • Machine Learning Tutorials
  • Operating Systems Tutorials
  • Programming Tutorials
  • Projects
  • Python Tutorials
  • Tips&Tricks
  • Tools
  • VBScript Tutorials
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

Copyright © 2023 Programmingoneonone.

Powered by PressBook Blog WordPress theme