QueueTreeGraph
QueueTreeGraph
Applications of Queue
waiting lists for a single shared resource like printer, disk, CPU.
asynchronous transfer of data
maintain the play list in media players in order
operating systems for handling interrupts
Enqueue
If rear == capacity-1, Overflow condition
Dequeue
#include <stdio.h>
#define MAX_SIZE 5
// Queue structure
typedef struct {
int data[MAX_SIZE];
int front;
int rear;
int size;
} Queue;
return q->size = 0;
if (isFull(q)) {
printf("Queue is full\n");
return;
q->data[q->rear] = value;
q->size++;
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
}
int value = q->data[q->front];
q->size--;
return value;
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
return q->data[q->front];
int i = q->front;
i = (i + 1) % MAX_SIZE;
printf("\n");
int main() {
Queue q;
createQueue(&q);
int choice, value;
while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("4. Is Full\n");
printf("5. Is Empty\n");
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
break;
case 3:
break;
case 4:
case 5:
break;
case 6:
printQueue(&q);
break;
case 7:
return 0;
default:
printf("Invalid choice\n");
return 0;
1. Initialize Queue q
2. Set q.front = 0
3. Set q.rear = 0
4. Set q.size = 0
Is Empty
1. If q.size == 0
2. Return True
3. Else
4. Return False
Is Full
1. If q.size == MAX_SIZE
2. Return True
3. Else
4. Return False
Enqueue
1. If Is Full(q)
3. Else
4. q.data[q.rear] = value
6. q.size++
Dequeue
1. If Is Empty(q)
3. Else
4. value = q.data[q.front]
6. q.size--
7. Return value
Print Queue
1. For i = q.front to q.size
2. Print q.data[i]
3. i = (i + 1) mod MAX_SIZE
Main
1. Create Queue q
2. Enqueue(q, 1)
3. Enqueue(q, 2)
4. Enqueue(q, 3)
5. Enqueue(q, 4)
6. Enqueue(q, 5)
7. Print Queue(q)
8. Dequeue(q)
9. Print Queue(q)
#include <stdio.h>
#include <stdlib.h>
// Node structure
int data;
} Node;
// Queue structure
typedef struct {
Node* front;
Node* rear;
} Queue;
newNode->data = data;
newNode->next = NULL;
return newNode;
void createQueue(Queue* q) {
int isEmpty(Queue* q) {
if (isEmpty(q)) {
} else {
q->rear->next = newNode;
q->rear = newNode;
int dequeue(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
free(temp);
return data;
int peek(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
return q->front->data;
}
// Function to print queue elements
void printQueue(Queue* q) {
temp = temp->next;
printf("\n");
int main() {
Queue q;
createQueue(&q);
while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &data);
enqueue(&q, data);
break;
case 2:
break;
case 3:
break;
case 4:
printQueue(&q);
break;
case 5:
return 0;
default:
printf("Invalid choice\n");
return 0;
Pseudo Code
Create Node
Create Queue
1. Initialize queue q
Is Empty
1. If q.front is NULL
2. Return True
3. Else
4. Return False
Enqueue
2. If q is empty
5. Else
Dequeue
1. If q is empty
3. Return -1
7. If q.front is NULL
9. Free temp
Peek
1. If q is empty
3. Return -1
4. Return q.front.data
Print Queue
3. Print temp.data
Main
1. Create queue q
2. While true
3. Print menu
5. Switch choice
Priority Queue
A priority queue is a special type of queue in which each element is associated with
a priority value
If there is no node,
create a newNode.
insert the newNode at the end (last node from left to right.)
remove noteToBeDeleted
return rootNode
Priority Queue Applications
Dijkstra's algorithm
1. malloc
int* ptr = (int*) malloc(5 * sizeof(int)); // Allocates memory for 5 integers
2. calloc
int* ptr = (int*) calloc(5, sizeof(int)); // Allocates memory for an array of 5 integers
3. realloc:
int* ptr = (int*) malloc(5 * sizeof(int)); // Allocates memory for 5 integers
ptr = (int*) realloc(ptr, 10 * sizeof(int)); // Reallocates memory for 10 integers