Table of Contents
Priority queue insertion operation algorithm
Algorithm of insertion operation in priority queue
Home Backend Development C++ Introduction to priority queue in C/C++

Introduction to priority queue in C/C++

Sep 13, 2023 pm 05:21 PM
programming c language priority queue

A priority queue is a queue in which elements are inserted or removed according to the priority assigned to them, where priority is an integer value in the range 0-10, where 0 represents the element with the highest priority and 10 Represents the element with the highest priority and the element with the lowest priority. Implementing a priority queue follows two rules:

  • The data or element with the highest priority will be executed before the data or element with the lowest priority.
  • If two elements have the same priority, they will be executed in the order they were added to the list.

There are a variety of available data structures that can be used to implement priority queues such as stacks, queues, and linked lists. In this article, we will explain queue data structure. There are two methods that can be used to implement a priority queue, for example-

  • Maintaining multiple priority queues in a single array

    One method to implement a priority queue Just maintain a queue for each priority. We can store these multiple queues in an array where each queue has two pointers, Front and Rear. In the queue, the Front pointer is used to insert elements into the queue, and it is incremented by 1 every time an element is inserted; the other pointer is the rear pointer, used to delete or remove elements from the queue, and it is decremented every time an element is inserted. 1 is removed from the queue. Finally, from the positions of the two pointers we can also determine the number of elements in the queue.

Introduction to priority queue in C/C++

Note- If each queue has the same size, then instead of creating multiple A one-dimensional array.

Priority queue insertion operation algorithm

insert(queue, data, priority)
   If(queue->Rear[priority] = MAX-1 AND queue->Front[priority] = 0) OR (queue->Rear[priority] +1 =queue->Front[priority])
      Print Overflow
   End
   IF queue->Rear[priority - 1] = MAX-1
      Set queue->Rear[priority - 1] = 0
   Else
      Set queue->Rear[priority] = queue->Rear[priority - 1] +1
   End
      Set queue->CQueue[priority - 1] [queue->Rear[priority - 1] = data
   IF queue->Front[priority - 1] = -1
      Set queue->Front[priority - 1] = 0
End
Copy after login

Algorithm of insertion operation in priority queue

delete(queue)
   Set flag = 0, priority = 0
      While priority <= MAX-1
         IF NOT queue->Front[priority] = -1
            Set flag = 1
            Set value = queue->CQueue[priority][queue->Front[priority]]
            IF queue->Front[priority] = queue->Rear[priority]
               Set queue->Front[priority] = queue->Rear[priority] = -1
            Else
            IF queue->Front[priority] = MAX-1
               Set queue->Front[priority] = 0
            Else
               Set queue->Front[priority] = queue->Front[priority] + 1
            End
         End
      Break
   End
   Set priority = priority +
End
If flag = 0
   Print underflow
Else
   Return value
End
Copy after login

The above is the detailed content of Introduction to priority queue in C/C++. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Designated initializer in C language Designated initializer in C language Sep 01, 2023 am 08:49 AM

In the C90 standard, we have to initialize the array in a fixed order, such as initializing the index at position 0, 1, 2, etc. Starting with the C99 standard, they introduced designated initialization functionality in C. Here we can initialize elements in random order. Initialization can be done using array indexes or structure members. This extension is not implemented in GNUC++. If we specify some indexes and enter some values, then it will look like this - intarr[6]={[3]=20,[5]=40};orintarr[6]={[3]20,[5 ]40};This is equivalent to: intarr[6]={0,0,0,20,0,40};We can also put some range of elements using the following syntax

Introduction to priority queue in C/C++ Introduction to priority queue in C/C++ Sep 13, 2023 pm 05:21 PM

A priority queue is a queue in which elements are inserted or removed based on the priority assigned to them, where priority is an integer value in the range 0-10, where 0 represents the element with the highest priority and 10 represents the element with the highest Priority element The element with the lowest priority. Implementing a priority queue follows two rules: data or elements with the highest priority will be executed before data or elements with the lowest priority. If two elements have the same priority, they will be executed in the order they were added to the list. There are various data structures available that can be used to implement priority queues such as stacks, queues, and linked lists. In this article, we will explain queue data structure. There are two ways to implement priority queue eg - maintain multiple in a single array

In C language, variable length parameters of macros In C language, variable length parameters of macros Aug 27, 2023 pm 10:49 PM

We know that functions can be defined using variable length parameters in C language. For this we need to use ellipses (…). Similarly, in macros, we can also use variable length parameters. Here, too, we need to include the ellipses. ‘__VA_ARGS__’ is used to handle variable length arguments. The concatenation operator ‘##’ is used to concatenate variadic parameters. In this example, the macro accepts variable-length arguments, just like the printf() or scanf() functions. In this macro, we will print the file name, line number, and error message. The first parameter is pr. It is used to confirm

Robin Li mentioned the 'busy traffic' again, saying that AI will create more opportunities for human beings Robin Li mentioned the 'busy traffic' again, saying that AI will create more opportunities for human beings May 25, 2023 pm 10:05 PM

Two months ago, the painting "Car, Water, Horse, Dragon" created by Baidu Wenxinyiyan when it was tested by the first batch of users became popular on the Internet overnight. "Car", "Water", "Horse", The picture of "Dragon", four unrelated things stacked together, is indeed a bit innocent. Then almost overnight, Wen Xinyiyan completed the iteration and successfully interpreted the profound Chinese idiom "traffic and traffic" with pictures. Two months later, on May 18, when Robin Li, founder, chairman and CEO of Baidu, explained to the audience what "generative AI" was, he once again mentioned the "busy traffic", and what emerged from the joke was something more. Much confidence and calmness. Yes, in the past two months, Wen Xinyiyan's academic "scores" have increased almost linearly. "Baiduwen

In C language, what is an inline function? In C language, what is an inline function? Sep 08, 2023 am 11:21 AM

Inline functions can be replaced where the function call occurs. Function substitution is always the compiler's choice. In an inline function, the function call is replaced by the actual program code. Most inline functions are used for small calculations. They are not suitable for large calculations. Inline functions are similar to ordinary functions. The only difference is that we put a keyword inline before the function name. Inline functions are created using the following syntax -inlinefunction_name(){ //functiondefinition}ExampleThe following is an inline function for a C program: #include<stdio.h>inlineint

In C language, health macro In C language, health macro Sep 03, 2023 pm 06:09 PM

Here we will see hygiene macros in C. We know the usage of macros in C. But sometimes, it doesn't return the expected result due to an unexpected capture of the identifier. If we see the code below, we can see that it is not working. Example#include<stdio.h>#defineINCREMENT(i)do{inta=0;++i;}while(0)main(void){ inta=10,b=20; //Callthemacrostwotimesforaandb &a

Arrays in C/C++? Arrays in C/C++? Sep 20, 2023 pm 08:25 PM

An array is a sequential collection of elements of the same type. Arrays are used to store collections of data, but it is often more useful to think of arrays as collections of variables of the same type. Instead of declaring a single variable such as number0, number1, ... and number99, you can declare an array variable (e.g. number) and represent it using numbers[0], numbers[1] and ..., numbers[99] each variable. Specific elements in the array are accessed through indexing. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element. Declaring an ArrayDeclaring an array requires specifying the type of elements and the number of elements required. An array is as follows -ty

storm numbers storm numbers Aug 26, 2023 am 09:41 AM

ForNtobeastormernumber,thehighestprimefactoroftheexpressionN^2+1mustbegreaterthanorequalto2*Nanditshouldbeapositiveinteger.Forexample,4isastormernumber.Since4*4+1=17hasthegreatestprimefactor17itselfwhichisgreaterthan8i.e.2*4.But 3 is not a strong number because 3*3+1=10. 10

See all articles