Queues-in-data-structure

Hello guys, welcome back to my blog. In this article, I will discuss queue in data structure and algorithm using c, code of queue in the data structure, types of queue, explanation with diagram, etc.

If you need an article on some other topics then comment us below in the comment box. You can also catch me @ Instagram – Chetan Shidling.

Also, read:

  1. Search In Data Structure, Linear, Binary, Explanation, Code, Algorithm.
  2. Linked List In Data Structure, Explanation, Algorithm, Code, Questions.
  3. Stack Operation In Data Structure, Definition, Code, Push, Pop, Full.

Queue In Data Structure

Queues-in-data-structure

Assume we require to add another element by value 5, then REAR would be incremented by 1 and that value would be saved at the position pointed by REAR. The queue after addition would be as shown in block 02. Here, FRONT = 0 and REAR = 8. Each time a different element has to be added, we replicate the same procedure. If we need to delete a component from the queue, later the value of FRONT will be incremented. Deletions are made from just this end of the queue. The queue after deletion will be as shown in block 03.

Program to perform queue operation?

In this code, we can add a new element, delete an element, and we can also check whether the queue is full or empty, we can also display all elements.

#include <stdio.h>
#include <stdlib.h>

struct queue{
    int size;
    int f;
    int r;
    int * arr;
};

int isEmpty(struct queue *q){
    if(q->r==q->f){
        return 1;
    }
    return 0;
}

int isFull(struct queue *q){
    if(q->r==q->size-1){
        return 1;
    }
    return 0;
}

void enqueue(struct queue *q, int val){
    if(isFull(q)){
        printf("This Queue is full\n");
    }
    else{
        q->r++;
        q->arr[q->r]=val;
    }
}

int dequeue(struct queue *q){
    int a = -1;
    if(isEmpty(q)){
        printf("This Queue is Empty\n");
    }
    else{
        q->f++;
        a = q->arr[q->f];
    }
    return a;
}


int main()
{
    int O, val;
    struct queue q;
    q.size = 100;
    q.f = q.r = -1;
    q.arr = (int*)malloc(q.size*sizeof(int));
    printf("1: For Enqueue\n");
    printf("2: For Dequeue\n");
    printf("3: To check empty\n");
    printf("4: To check full\n");
    printf("5: Display\n");
    printf("6: To exit\n");

    while(q.r != q.size-1){
        printf("Enter the operation you want to perform\n");
        scanf("%d", &O);
        switch(O)
        {
        case 1:
            printf("Enter the element you want to store\n");
            scanf("%d",&val);
            enqueue(&q, val);
            printf("Element is %d\n",q.arr[q.r]);
            break;
        case 2:
            printf("Dequeueing element %d\n", dequeue(&q));
            break;
        case 3:
            if(isEmpty(&q)){
            printf("Queue is Empty\n");
            }
            else
                printf("Not Empty\n");
            break;
        case 4:
            if(isFull(&q)){
            printf("Queue is Full\n");
            }
            else
                printf("Not full\n");
            break;
        case 5:
            printf("Display All Values Stored\n");
            while(q.f!=q.r)
            {
                q.f++;
                printf("Element are %d\n",q.arr[q.f]);
            }
            break;
        default:
            exit(0);
        }
    }

   return 0;
}

Output:

1: For Enqueue
2: For Dequeue
3: To check empty
4: To check full
5: Display
6: To exit
Enter the operation you want to perform
1
Enter the element you want to store
22
Element is 22
Enter the operation you want to perform
1
Enter the element you want to store
44
Element is 44
Enter the operation you want to perform
1
Enter the element you want to store
66
Element is 66
Enter the operation you want to perform
4
Not full
Enter the operation you want to perform
3
Not Empty
Enter the operation you want to perform
2
Dequeueing element 22
Enter the operation you want to perform
5
Display All Values Stored
Element are 44
Element are 66
Enter the operation you want to perform

Types of queues

  1. Circular Queue
  2. Deque
  3. Priority Queue
  4. Multiple Queue

Applications Of Queue

01. Queues are generally used as waiting lists for a single shared device like a printer, disk, CPU.

02. Queues are utilized to transfer data asynchronously (data not necessarily received at the same rate as sent) between two processes (IO buffers), e.g., pipes, file IO, sockets.

03. Queues are employed as buffers on MP3 players and portable CD players, iPod playlist.

04. Queues are used in Playlist for the jukebox to add songs to the end, play from the front of the list.

05. Queues are used in the operating system for handling interrupts. When programming a real-time system that can be interrupted, for example, by a mouse click, it is necessary to process the interrupts immediately, before proceeding with the current job. If the interrupts have to be handled in the order of arrival, then a FIFO queue is the appropriate data structure.

I hope this article may help you all a lot. Thank you for reading.

Also, read:

About The Author

Share Now