Priority Queue Using Array
#include <bits/stdc++.h>
using namespace std;
// Structure for the elements in the priority queue
struct item {
int value;
int priority;
};
// Store the element of a priority queue
item pr[100000];
// Pointer to the last index (size of the queue)
int queueSize = -1; // Renamed variable to avoid conflict
// Function to insert a new element into priority queue
void enqueue(int value, int priority) {
// Increase the size
queueSize++;
// Insert the element
pr[queueSize].value = value;
pr[queueSize].priority = priority;
}
// Function to check the top element
int peek() {
int highestPriority = INT_MIN;
int ind = -1;
// Check for the element with highest priority
for (int i = 0; i <= queueSize; i++) {
// If priority is same, choose the element with the highest value
if (pr[i].priority > highestPriority ||
(pr[i].priority == highestPriority && pr[i].value > pr[ind].value)) {
highestPriority = pr[i].priority;
ind = i;
}
}
return ind; // Return the index of the highest priority element
}
// Function to remove the element with the highest priority
void dequeue() {
// Find the position of the element with highest priority
int ind = peek();
// If the queue is not empty
if (ind != -1) {
// Shift the elements to remove the highest priority element
for (int i = ind; i < queueSize; i++) {
pr[i] = pr[i + 1];
}
// Decrease the size of the priority queue by one
queueSize--;
} else {
cout << "Queue is empty!" << endl;
}
}
// Driver Code
int main() {
// Function Call to insert elements as per priority
enqueue(10, 2);
enqueue(14, 4);
enqueue(16, 4);
enqueue(12, 3);
// Stores the top element at the moment
int ind = peek();
cout << "Top Element: " << pr[ind].value << endl;
// Dequeue the top element
dequeue();
// Check the top element after dequeue
ind = peek();
cout << "Top Element after dequeue: " << pr[ind].value << endl;
// Dequeue the top element again
dequeue();
// Check the top element after second dequeue
ind = peek();
cout << "Top Element after second dequeue: " << pr[ind].value << endl;
return 0;
}
Queue As ADT
// Define the structure of Queue
Declare Queue as an array
Initialize FRONT = -1, REAR = -1, MAX_SIZE = size of the queue
// i) Create Queue
Procedure CreateQueue(size)
Set MAX_SIZE = size
Set FRONT = -1
Set REAR = -1
Print "Queue created with size:", size
End Procedure
// ii) Insert (Enqueue)
Procedure Enqueue(value)
If REAR == MAX_SIZE - 1
Print "Queue is full. Insertion not possible."
Exit
Else If FRONT == -1
FRONT = 0 // Queue is empty, set FRONT to 0
End If
REAR = REAR + 1
Queue[REAR] = value
Print "Inserted", value, "at REAR"
End Procedure
// iii) Delete (Dequeue)
Procedure Dequeue()
If FRONT == -1
Print "Queue is empty. Deletion not possible."
Exit
Else If FRONT == REAR
Print "Deleted", Queue[FRONT]
FRONT = -1
REAR = -1
Else
Print "Deleted", Queue[FRONT]
FRONT = FRONT + 1
End If
End Procedure
// iv) Display Queue
Procedure DisplayQueue()
If FRONT == -1
Print "Queue is empty."
Exit
End If
Set i = FRONT
Print "Queue elements:"
While i <= REAR
Print Queue[i]
i=i+1
End While
End Procedure
// v) Check if Queue is Empty
Procedure IsEmpty()
If FRONT == -1
Return true
Else
Return false
End If
End Procedure
// vi) Get Size of Queue
Procedure Size()
If FRONT == -1
Return 0
Else
Return REAR - FRONT + 1
End If
End Procedure
// Main program
CreateQueue(5)
Enqueue(10)
Enqueue(20)
Enqueue(30)
DisplayQueue() // Should display 10, 20, 30
Dequeue() // Removes 10
DisplayQueue() // Should display 20, 30
Enqueue(40)
Enqueue(50)
DisplayQueue() // Should display 20, 30, 40, 50
Size() // Should return 4
IsEmpty() // Should return false
Dequeue() // Removes 20
Dequeue() // Removes 30
Dequeue() // Removes 40
Dequeue() // Removes 50
DisplayQueue() // Should print "Queue is empty"
Queue using SLL
Initialization
pseudocode
size ← N // Maximum size of the queue
queue ← array of size N
front ← -1
rear ← -1
Enqueue (Insert Element)
pseudocode
function enqueue(element):
if rear == size - 1: // Check if the queue is full
print "Queue is Full"
return
if front == -1: // If the queue is empty
front ← 0
rear ← rear + 1
queue[rear] ← element
print "Element", element, "added to the queue"
Dequeue (Remove Element)
pseudocode
function dequeue():
if front == -1 or front > rear: // Check if the queue is empty
print "Queue is Empty"
return
element ← queue[front]
front ← front + 1
print "Element", element, "removed from the queue"
if front > rear: // Reset queue when empty
front ← -1
rear ← -1
Peek (View Front Element)
pseudocode
function peek():
if front == -1: // Check if the queue is empty
print "Queue is Empty"
return
print "Front Element:", queue[front]