C Program to Implement Queue Using Array

In this article, we will explore how to implement a queue using an array in the C programming language. First, we will explain the major operations of the Queue using an array and then explain the complete C code to implement the queue using array.

1. Enqueue Operation

Enqueue Pseudocode

Function Enqueue(queue, element):
if Queue is Full:
print(“Queue Overflow”)
return
rear = rear + 1
queue[rear] = element
return

Enqueue Algorithm

  • Check if the queue is full.
  • If full, return an overflow error.
  • Otherwise, add the element at the rear position.
  • Increment the rear pointer.

C Program for Enqueue

#include <stdio.h>
#define MAX =5;

int queue[MAX];
int front = -1;
int rear = -1;

void Enqueue(int element) {
if (rear == MAX – 1) {
printf(“Queue Overflow\n”);
return;
}
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = element;
}

2. Dequeue Operation

Dequeue Pseudocode

Function Dequeue(queue):
if Queue is Empty:
print(“Queue Underflow”)
return
element = queue[front]
front = front + 1
if front > rear:
front = rear = -1
return element

Dequeue Algorithm

  • Check if the queue is empty.
  • If empty, return an underflow error.
  • Otherwise, retrieve the front element.
  • Move the front pointer to the next element.
  • If the queue becomes empty, reset both pointers.

Dequeue C Code

int Dequeue() {
if (front == -1) {
printf(“Queue Underflow\n”);
return -1; // Error code
}
int element = queue[front];
front++;
if (front > rear) {
front = rear = -1;
}
return element;
}

3. Peek (or Front) Operation

Peek() Pseudocode

Function Peek(queue):
if Queue is Empty:
print(“Queue is Empty”)
return
return queue[front]

Algorithm for Peek()

  • Check if the queue is empty.
  • If empty, return a message indicating the queue is empty.
  • Otherwise, return the front element without removing it.

C Code for Peek()

int Peek() {
if (front == -1) {
printf(“Queue is Empty\n”);
return -1; // Error code
}
return queue[front];
}

4. IsEmpty() Operation

IsEmpty() Pseudocode

Function IsEmpty(queue):
if front == -1:
return true
return false

IsEmpty() Algorithm

  1. Check if the front pointer is -1.
  2. If front == -1, the queue is empty, return true.
  3. Otherwise, return false.

C Code for IsEmpty()

int IsEmpty() {
return (front == -1);
}

5. IsFull() Operation (for Fixed-Size Queues)

Pseudocode for IsFull()

Function IsFull(queue):
if rear == MAX – 1:
return true
return false

Algorithm for IsFull()

  • Check if the rear pointer has reached the maximum capacity.
  • If rear == MAX - 1, the queue is full, return true.
  • Otherwise, return false.

C Code for IsFull()

int IsFull() {
return (rear == MAX – 1);
}

Full C Program for Queue Operations

This program will perform the following

1. Dynamic Memory Allocation: Allocates memory for the queue array based on user-specified MAX.

Important:

If a statically defined array-like int queue[100]; is used, 100 integer spaces are reserved in memory regardless of the queue size required.

Dynamic allocation ensures memory is allocated only as needed, which is especially useful in memory-constrained systems.

  • int *queue;
  • printf(“Enter the maximum size of the queue: “);
  • scanf(“%d”, &MAX);
  • queue = (int *)malloc(MAX * sizeof(int));

2. Menu-Driven Interface

  • Provides a menu for performing various queue operations.
  • Calls respective functions based on user input.
  • Repeats until the user selects the exit option.

3. Cleanup: Frees the dynamically allocated memory for the queue before exiting.

Here is the complete code in C programming for queue implementation using an array

#include <stdio.h>
#include <stdlib.h>int *queue;
int front = -1;
int rear = -1;
int MAX;void Enqueue(int element) {
if (rear == MAX – 1) {
printf(“Queue Overflow\n”);
return;
}
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = element;
printf(“Enqueued %d\n”, element);
}

int Dequeue() {
if (front == -1) {
printf(“Queue Underflow\n”);
return -1; // Error code
}
int element = queue[front];
front++;
if (front > rear) {
front = rear = -1;
}
return element;
}

int Peek() {
if (front == -1) {
printf(“Queue is Empty\n”);
return -1; // Error code
}
return queue[front];
}

int IsEmpty() {
return (front == -1);
}

int IsFull() {
return (rear == MAX – 1);
}

void DisplayQueue() {
if (front == -1) {
printf(“Queue is Empty\n”);
return;
}
printf(“Queue elements: “);
for (int i = front; i <= rear; i++) {
printf(“%d “, queue[i]);
}
printf(“\n”);
}

int main() {
printf(“Enter the maximum size of the queue: “);
scanf(“%d”, &MAX);

// Dynamically allocate memory for the queue
queue = (int *)malloc(MAX * sizeof(int));

int choice, element;
do {
printf(“\nMain Menu:\n”);
print(” 1. Enqueue\n”);
print(” 2. Dequeue\n”);
print(” 3. Peek\n”);
print(” 4. Check if Empty\n”);
print(” 5. Check if Full\n”);
print(” 6. Display Queue\n”);
print(” 7. Exit\n”);
printf(“Enter your choice: “);
scanf(“%d”, &choice);

switch (choice) {
case 1:
printf(“Enter element to enqueue: “);
scanf(“%d”, &element);
Enqueue(element);
break;
case 2:
element = Dequeue();
if (element != -1) {
printf(“Dequeued element: %d\n”, element);
}
break;
case 3:
element = Peek();
if (element != -1) {
printf(“Front element: %d\n”, element);
}
break;
case 4:
printf(“Is queue empty? %s\n”, IsEmpty() ? “Yes” : “No”);
break;
case 5:
printf(“Is queue full? %s\n”, IsFull() ? “Yes” : “No”);
break;
case 6:
DisplayQueue();
break;
case 7:
printf(“Exiting…\n”);
break;
default:
printf(“Invalid choice!\n”);
}
} while (choice != 7);

// Free the dynamically allocated memory
free(queue);

return 0;
}

Output:

Enter the maximum size of the queue: 10

Main Menu:
1. Enqueue
2. Dequeue
3. Peek
4. Check if Empty
5. Check if Full
6. Display Queue
7. Exit
Enter your choice: