C Program to Implement Queue Using Circular Array

Queue is one of the fundamental data structures used in computer science. As the very nature of a queue, whoever comes first will be served first. That’s why it is called First In First Out (FIFO) data structure. Event Queue or Message Queue are very common examples of this data structure. We’ll see here how to implement queue using array – even though it can be implement in various other ways.

We’ll implement the queue as a circular array. Otherwise, the number of the enqueue (described later) operations will be limited to the array size. The array indexes will come back to the front of the array if they exceed the array size.

Queue Operations

Here are the basic queue operations.

  1. Enqueue: Inserting a new element into the queue. This newly inserted element will be the last element to be served. But it will be served before any future element that will be enqueued later.
  2. Dequeue: Get the element from the queue which came first. The retrieved element will be removed from the queue.
  3. Peek: This is same as dequeue but the element will not be removed. That means that the consecutive peek operations will give the same element.

Queue Implementation

Here the queue is represented as a structure with the following elements.

  1. An array to hold the queue elements.
  2. A counter to hold the number of elements in the queue in a particular moment.
  3. The front index to represent the position in the array where the next enqueued element will be placed.
  4. The rear index that points the last inserted element in the array.

The queue operations are implemented as functions. The functions take a queue pointer as input. This will allow to use multiple queues in the program.

Enqueue Operation Logic

  1. If the number of elements is greater than the array size, return failure. The queue is already full – no room for new element.
  2. Place the new element where the front index is pointing to.
  3. Increment the front index. If the front index exceeds the array size, it will again start from the front of the array.

Dequeue Operation Logic

  1. If the element count is 0, return failure. The queue is empty.
  2. Return the element of the rear index.
#include <stdio.h>

#define SIZE 64

struct queue {
  int element[SIZE];
  int element_count;
  
  int front;
  int rear;
};

void initialize(struct queue *q) {
  /* Sanity check */
  if (q == NULL) return;
  
  q->element_count = 0;
  q->front = 0;
  q->rear = 0;
}

int enqueue(struct queue *q, int data) {
  /* Sanity check */
  if (q == NULL) return -1;
  
  if (q->element_count >= SIZE) {
    printf("Queue is full.\n");
    return -1;
  }
  
  q->element[q->front] = data;
  q->element_count++;

  q->front = (q->front + 1) % SIZE;
  return 0;
}

int dequeue(struct queue *q) {
  int data;

  if (q == NULL || q->element_count == 0) {
    printf("Queue is empty.\n");
    return -1;
  }
  
  data = q->element[q->rear];
  q->element_count--;

  q->rear = (q->rear + 1) % SIZE;
  
  return data;
}

int peek(struct queue *q) {

  if (q == NULL || q->element_count == 0) {
    printf("Queue is empty.\n");
    return -1;
  }
  
  return q->element[q->rear];
}

void display(struct queue *q) {
  int data;
  int i;
  if (q == NULL || q->element_count == 0) {
    printf("Queue is empty.\n");
    return;
  }
  
  for (i = 0; i < q->element_count; i++) {
    printf("%d ", q->element[(q->rear + i) % SIZE]);
  }
  
  printf("\n");
}

int main(){
  struct queue q;
  int data;
  int repeat = 1;
  int choice;
  
  initialize(&q);
  
  while(repeat) {
    printf("\nOptions: \n");
    printf(" 1. Enqueue \n");
    printf(" 2. Dequeue \n");
    printf(" 3. Peek \n");
    printf(" 4. Display \n");
    printf(" 5. Exit \n");
    
    printf("Enter choice: ");
    scanf("%d", &choice);
    
    switch(choice) {
      case 1:
        printf("Enter a number: ");
        scanf("%d", &data);
        enqueue(&q, data);
        printf("The entered number is enqueued.\n");
        break;
      case 2:
        data = dequeue(&q);
        printf("The dequeued numner: %d.\n", data);
        break;
      case 3:
        data = peek(&q);
        printf("The peeked numner: %d.\n", data);
        break;
      case 4:
        printf("The queue elements: ");
        display(&q);
        break;
      case 5:
        repeat = 0;
        break;
      default:
        printf("Wrong choice.\n");
        break;
    }
  }
  return 0;
}

This program runs in an infinite loop. In every iteration, it asks for options like – enqueue, dequeue, peek, display or exit. Based on the choice given, it takes input or displays value or exit.

Here is a sample output.

$ cc test.c -o test
$ ./test

Options:
 1. Enqueue
 2. Dequeue
 3. Peek
 4. Display
 5. Exit
Enter choice: 1
Enter a number: 45
The entered number is enqueued.

Options:
 1. Enqueue
 2. Dequeue
 3. Peek
 4. Display
 5. Exit
Enter choice: 1
Enter a number: 64
The entered number is enqueued.

Options:
 1. Enqueue
 2. Dequeue
 3. Peek
 4. Display
 5. Exit
Enter choice: 1
Enter a number: 23
The entered number is enqueued.

Options:
 1. Enqueue
 2. Dequeue
 3. Peek
 4. Display
 5. Exit
Enter choice: 1
Enter a number: 78
The entered number is enqueued.

Options:
 1. Enqueue
 2. Dequeue
 3. Peek
 4. Display
 5. Exit
Enter choice: 4
The queue elements: 45 64 23 78

Options:
 1. Enqueue
 2. Dequeue
 3. Peek
 4. Display
 5. Exit
Enter choice: 3
The peeked numner: 45.

Options:
 1. Enqueue
 2. Dequeue
 3. Peek
 4. Display
 5. Exit
Enter choice: 2
The dequeued numner: 45.

Options:
 1. Enqueue
 2. Dequeue
 3. Peek
 4. Display
 5. Exit
Enter choice: 4
The queue elements: 64 23 78

Options:
 1. Enqueue
 2. Dequeue
 3. Peek
 4. Display
 5. Exit
Enter choice: 5

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

0
0
3
0
6
2