Define Queue. Write are different applications of queue? Explain queue operations with example.

This answer is restricted. Please login to view the answer of this question.

Login Now

A queue is an ordered collection of items from which items may be deleted at one end (called the front of the queue) and into which items may be inserted at the other end (the rear of the queue).

A stack works as LIFO (Last-in-First-out) technique but the queue work as FIFO technique (First-in-First-out).

Queue | FIFO Technique | HAMROCSIT

Application of Queue:

  1. Task Waiting for the printing
  2. Time sharing system for use of CPU
  3. For access to disk storage
  4. Task scheduling in operating system

Operations on Queue:

  1. MakeEmpty(q): To make q as an empty queue
  2. Enqueue(q, x): To insert an item x at the reat of the queue
  3. Dequeue(q): To delete an item from the first of the queue
  4. isFull(q): To check whether the queue is full
  5. isEmpty(q): To  check whether the queue q is empty
  6. traverse(q): To read entire queue that is to display the conrtext of the queue

Program to implement Queue Operations:

#include <stdio.h>

#define MAX_SIZE 100
int arr_queue[MAX_SIZE];
int rear = 0;
int front = 0;

int isFull(){
    return (rear == MAX_SIZE);
}

int isEmpty(){
    return (front == rear);
}

void dequeue(){
    if( isEmpty() ){
        printf("Queue is Empty.\n");
    }else{
        printf("Dequeued element = %d\n", arr_queue[front]);
        front++;
    }
}

void enqueue(int val){
    if( isFull() ){
        printf("Queue is Full.\n");
    }else{
        arr_queue[rear] = val;
        rear++;
    }
}

void traverse(){
    if( isEmpty() ){
        printf("Queue is Empty.\n");
    }else{
        int i;
        printf("\n\nQueue Data are:\n");
        for (i = front; i < rear; i++)
            printf("Position : %d, Value  : %d\n", i, arr_queue[i]);
    }
}


int main(){

    int item, choice, i;
    int exit = 1;

    printf("Queue Main Menu");
    printf("\n1.Insert \n2.Remove \n3.Display \nOthers to exit");
    do{
        printf("\nEnter Your Choice : ");
        scanf("%d", &choice);

        switch (choice){
            case 1:
                printf("Enter Element an item to insert: ");
                scanf("%d", &item);
                enqueue(item);
                break;
            case 2:
                dequeue();
                break;
            case 3:
                traverse();
                break;
            default:
                exit = 0;
                break;
        }
    } while (exit);

    return 0;
}

The output of above program is

Queue Main Menu
1.Insert 
2.Remove 
3.Display 
Others to exit
Enter Your Choice : 1
Enter Element an item to insert: 12

Enter Your Choice : 1
Enter Element an item to insert: 15

Enter Your Choice : 1
Enter Element an item to insert: 17

Enter Your Choice : 3


Queue Data are:
Position : 0, Value  : 12
Position : 1, Value  : 15
Position : 2, Value  : 17

Enter Your Choice : 2
Dequeued element = 12

Enter Your Choice : 2
Dequeued element = 15

Enter Your Choice : 2
Dequeued element = 17

Enter Your Choice : 2
Queue is Empty.

Enter Your Choice : 12
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .