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
priority queue in data structures and algorithms

Priority queue in Data structure

Posted on 28 May 202023 April 2023 By YASH PAL No Comments on Priority queue in Data structure

In Data Structure a priority queue is a queue in that each element has some priority and all elements are processed based on their priorities. and the element with high priority is processed before the element with low priority.

if the two elements have the same priority then we follow the FIFO rule means the element that comes first will place first and deleted first. and the element that has the highest priority is always at the front of the queue. and has been inserted first and also deleted first.

Priority queue | Data structures and algorithms

Here in the above example, we implement a priority queue using a linked list. in each node, we have three parts. in the first part, we store the priority of the element and in the second part we store the value of the element in the third part, we store the linked part of the next element.

if we want to insert a new element that has priority 3 then we need to insert the element after node three.

Python program to Implement Priority Queue.

class EmptyQueueError(Exception):
    pass

class Node:

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

class PriorityQueue:

    def __init__(self):
        self.front = None
    def enqueue(self, data, data_priority):

        temp = Node(data, data_priority)

        if self.is_empty() or data_priority < self.front.priority:
            temp.link = self.front
            self.front = temp
        else:
            p = self.front
            while p.link != None and p.link.priority <= data_priority:
                p = p.link
            temp.link = p.link
            p.link = temp

    def dequeue(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is empty")
        x = self.front.info
        self.front = self.front.link
        return x

    def is_empty(self):
        return self.front == None
    def display(self):
        if self.is_empty():
            print("Queue is empty")
            return
        print("Queue is : ")
        p = self.front
        while p is not None:
            print(p.info, " ", p.priority)
            p = p.link
        print()

    def size(self):
        n = 0        p = self.front
        while p is not None:
            n += 1            p = p.link
        return n


############################
if __name__ == '__main__':
    qu = PriorityQueue()

    while True:
        print("1. Enqueue")
        print("2. Dequeue")
        print("3. Display all queue elements")
        print("4. Display size of the queue")
        print("5. Quit")

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

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

Post navigation

Previous Post: Dequeue in Data structure
Next Post: Polish Notation in Data Structure

Related Tutorials

vi editing commands VI Editing Commands For Linux Computer Science Tutorials
modes of vi editor VI Editor in Linux Computer Science Tutorials
environment and path setting in linux Environment and Path Setting in Linux Computer Science Tutorials
hard and synbolic links in linux Hard links and Symbolic links Computer Science Tutorials
changing file access permissions Changing File Access Permissions in Linux Computer Science Tutorials
access permissions in linux Access permissions in Linux Computer Science Tutorials

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick your Subject

  • Internet of Things
  • Data Structures/Algorithms
  • Interview Preparation
  • Human Values
  • 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
  • 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
  • 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