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 implemented the queue as a circular array. Otherwise, the number of the enqueue (described later) operations will be limited to the 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.
- 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.
- Dequeue: Get the element from the queue which came first. The retrieved element will be removed from the queue.
- 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.
- An array to hold the queue elements.
- A counter to hold the number of elements in the queue in a particular moment.
- The front index to represent the position in the array where the next enqueued element will be placed.
- 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
- If the number of elements is greater than the array size, return failure. The queue is already full – no room for new element.
- Place the new element where the front index is pointing to.
- 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
- If the element count is 0, return failure. The queue is empty.
- 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;
}
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) {
int data;
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
The post C Program to Implement Queue Using Circular Array appeared first on QnA Plus.