C Program to implement Queue using Arrays

               Queue is a linear data structure which uses the principle FIFO(First In,First Out). Imagine a queue. The person who comes first, will leave first.

Let us implement the Queue in C language.

Queue implementation using Arrays:

              This program starts by including the built-in header file. It has following steps.

Steps:

  • ·       It defines the maximum size as 6.
  • ·       An array is created as integer with max size we defined already.
  • ·       Front and rear are the two variables to represent front end and rear end.
  • ·       First, we check the queue is full or not. Next, we check for empty or not.
  • ·       ‘Enqueue()’ is the operation to insert the values to the queue. It checks for queue is full or not. if it is not full, it checks the front value and increases the rear value in the queue array and assigns the value.
  • ·       ‘dequeue() is the operation to delete a value from the queue. It checks for queue emptiness. If queue has elements, it increases the front value in the queue and removes the value.
  • ·       ‘display()’ is the function to print the queue elements. It checks whether queue is empty or not. if queue is not empty, it prints the elements using a for loop.
  • ·       In the main() function, it calls the function.

C code:

#include <stdio.h>

#define MAX 6   //Let us assign Maximum size as 6.

// int array is declared as queue

int queue[MAX];

int q_front = -1, q_rear = -1;

// it check if queue is full or not

int isFull() {

    return (q_rear == MAX - 1);

}

// it check if queue is empty or not

int isEmpty() {

    return (q_front == -1 || q_front > q_rear);

}

// Enqueue operation (insert values)

void enqueue(int value) {

    if (isFull()) {

        printf("Queue Overflow! Cannot insert %d\n", value);

    } else {

        if (q_front == -1) q_front = 0;

        queue[++q_rear] = value;

        printf("%d inserted into queue\n", value);

    }

}

// Dequeue operation(delete values)

void dequeue() {

    if (isEmpty()) {

        printf("Queue Underflow! Nothing to delete\n");

    } else {

        printf("%d removed from queue\n", queue[q_front++]);

    }

}

// Display the queue elements

void display() {

    if (isEmpty()) {

        printf("Queue is empty\n");

    } else {

        printf("Queue elements: ");

        for (int i = q_front; i <= q_rear; i++) {

            printf("%d ", queue[i]);

        }

        printf("\n");

    }

}

int main() {

    enqueue(110);

    enqueue(22);

    enqueue(34);

    display();

    dequeue();

    display();

    enqueue(89);

    enqueue(76);

    enqueue(60);

    enqueue(90); //it overflows the queue

    display();

    return 0;

}

Output:

110 inserted into queue

22 inserted into queue

34 inserted into queue

Queue elements: 110 22 34

110 removed from queue

Queue elements: 22 34

89 inserted into queue

76 inserted into queue

60 inserted into queue

Queue Overflow! Cannot insert 90

Queue elements: 22 34 89 76 60

Yes. The C code to implement queue using array was implemented successfully. Hope, you understood the code. Keep coding!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

File handling in C++

Datatypes and Variables in java script