Queue Data Structure

This is a type of data structure similar to stack which follows the FIFO principle it stands for first in last out. Any data that is entered into a queue first will get out at the first in queue. And any data that is entered at the last will be the last to get out of the queue. It is like the queue in real life like a queue in McDonald’s, the first person standing in line is the one who gets the meal first. Queue’s are used in I/O (input/output) buffer, like printer or keyboard, where the first instruction to be sent is the first one to be executed. It is also used in task scheduling, networking, customer service, operating systems and in Tree and Graph Traversal. I will write another graph telling how trees and graphs work.

Queues use similar functions such as push, pop and display as stack but the order is different.

Here is a program showing the functions used in queues.

#include<stdio.h>
#include <stdlib.h>
#define n 5
int s1[n],s2[n];
int top1=-1,top2=-1;
int count=0;
int pop1(){ // pops element from stack 1
    return s1[top1--];
}
int pop2(){ // pops element from stack 1
    return s2[top2--];
}
void push1(int x){ //pushes element into stack 1
    if(top1==n-1){
        printf("Full");
    }
    else{
        top1++;
        s1[top1]=x;
    }
}
void push2(int x){ //pushes element into stack 2
    if(top2==n-1){
        printf("Full");
    }
    else{
        top2++;
        s2[top2]=x;
    }
}
void enqeue(int x){//Main queue stack function
    push1(x);
    count++;
}
void dequeue(){//Deletes from squeue by transferring the elements from the stack
//one to stack 2 and then popping element from stack 2 count is really important.
    int i,a,b;
    if(top1==-1 && top2==-1){
        printf("Queue is empty");
    }
    else{
        for(int i=0;i<count;i++){
            a=pop1();
            push2(a);
        }
        b=pop1();
        count--;
        printf("%d",b);
        for(i=0;i<count;i++){
            a=pop2();
            push(a);
        }
    }
}
void display(){
    for(int i=0;i<=top1;i++){
        printf("%d",s1[i]);
    }
}
int main(){

    return 0;
}

Types of Queues

Simple Queue- It is a simple queue which follow’s the FIFO principle.

Circular Queue-Last position connects to the first, forming a circle. It prevents wasted space in a normal queue by reusing freed positions. It follows FIFO and is used in CPU scheduling, buffering, and memory management.

Priority Queue-A Priority Queue is a data structure where elements are dequeued based on priority, not order. Higher-priority items are processed first.

Double-Ended Queue (Deque)-In this queue it allows insertion and deletion from both ends.

Q1. Program to show circular queue using stack.

#include <stdio.h>
#define MAX_SIZE 3
struct CircularQueue
{
int data; // data to be stored in the queue
};
// Array of structures
struct CircularQueue queue[MAX_SIZE];
int front = -1, rear = -1;
void insert(int value);
int delete();
void display();
int main()
{
int choice, value;
while (1)
{
printf("1. Insert element in the queue\n");
printf("2. Delete element from the queue\n");
printf("3. Display elements in the queue\n");
printf("4. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &value);
insert(value);
break;
case 2:
value = delete();
if (value != -1)
{
printf("The deleted element is: %d\n", value);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
void insert(int value)
{
if ((front == 0 && rear == MAX_SIZE - 1) || (rear + 1 == front))
{
printf("Circular Queue is full\n");
return;
} if (
front == -
1)
{
front = rear = 0;
}
else if (rear == MAX_SIZE - 1)
{
rear = 0;
}
else
{
rear++;
}
queue[rear].data = value;
}
// Function to remove an element from the queue
int delete()
{
int value;
if (front == -1) // queue is empty
{
printf("QUEUE UNDERFLOW\n");
return -1;
}
value = queue[front].data;
if (front == rear) // only one element in the queue
{
front = rear = -1;
}
else if (front == MAX_SIZE - 1) // front point to last of array
{
front = 0;
}
else // more than one element in the queue
{
front++;
}
return value;
}
void display()
{
int i;
if(front == -1) printf(" \n Empty Queue\n");
else
{
printf("\n Front -> %d ",front);
printf("\n Items -> ");
for( i = front; i!=rear; i=(i+1)%MAX_SIZE) {
printf("%d ",queue[i].data);
}
printf("%d ",queue[i].data); // Print last element of queue where front = rear
printf("\n Rear -> %d \n",rear);
}
}

Hope you learnt something new today

Happy Coding…