Explain linear data structure queue in C language


Data structure is collection of data organized in a structured way. It is divided into two types as explained below −

  • Linear data structure − Data is organized in a linear fashion. For example, arrays, structures, stacks, queues, linked lists.

  • Nonlinear data structure − Data is organized in a hierarchical way. For example, Trees, graphs, sets, tables.

Queue

It is a linear data structure, where the insertion is done at rear end and the deletion is done at the front end.

The order of queue is FIFO – First In First Out

Operations

  • Insert – Inserting an element into a queue.
  • Delete – Deleting an element from the queue.

Conditions

  • Queue over flow − Trying to insert an element into a full queue.

  • Queue under flow − Trying to delete an element from an empty queue.

Algorithms

Given below is an algorithm for insertion ( ) −

  • Check for queue overflow.
if (r==n)
printf ("Queue overflow")
  • Otherwise, insert an element in to the queue.
q[r] = item
r++

Given below is an algorithm for deletion ( )

  • Check for queue under flow.
if (f==r)
printf ("Queue under flow")
  • Otherwise, delete an element from the queue.
item = q[f]
f++

Given below is an algorithm for display ( )

  • Check whether the queue is empty or not.
if (f==r)
printf("Queue is empty")
  • Otherwise, print all the elements from ‘f’ to ‘r’.
for(i=f; i<r; i++)
printf ("%d", q[i]);

Program

Following is the C program for implementation of queue by using arrays −

 Live Demo

#include<limits.h>
#include<stdio.h>
#include <stdlib.h>
struct Queue {
   int front, rear, size;
   unsigned capacity;
   int* array;
};
struct Queue* createQueue(unsigned capacity){
   struct Queue* queue = (struct Queue*)malloc(
   sizeof(struct Queue));
   queue->capacity = capacity;
   queue->front = queue->size = 0;
   queue->rear = capacity - 1;
   queue->array = (int*)malloc(
   queue->capacity * sizeof(int));
   return queue;
}
//if queue is full
int isFull(struct Queue* queue){
   return (queue->size == queue->capacity);
}
// Queue is empty
int isEmpty(struct Queue* queue){
   return (queue->size == 0);
}
void Equeue(struct Queue* queue, int item){
   if (isFull(queue))
      return;
   queue->rear = (queue->rear + 1)
   % queue->capacity;
   queue->array[queue->rear] = item;
   queue->size = queue->size + 1;
   printf("%d entered into queue
", item); } int Dqueue(struct Queue* queue){    if (isEmpty(queue))       return INT_MIN;    int item = queue->array[queue->front];    queue->front = (queue->front + 1)    % queue->capacity;    queue->size = queue->size - 1;    return item; } // Function to get front of queue int front(struct Queue* queue){    if (isEmpty(queue))       return INT_MIN;       return queue->array[queue->front];    }    // Function to get rear of queue    int rear(struct Queue* queue){       if (isEmpty(queue))          return INT_MIN;    return queue->array[queue->rear]; } int main(){    struct Queue* queue = createQueue(1000);    Equeue(queue, 100);    Equeue(queue, 200);    Equeue(queue, 300);    Equeue(queue, 400);    printf("%d is deleted element from queue

",    Dqueue(queue));    printf("1st item in queue is %d
", front(queue));    printf("last item in queue %d
", rear(queue));    return 0; }

Output

When the above program is executed, it produces the following result −

100 entered into queue
200 entered into queue
300 entered into queue
400 entered into queue
100 is deleted element from queue
1st item in queue is 200
last item in queue 400

Updated on: 24-Mar-2021

493 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements