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]; void Enqueue(int 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
- Check if the
front
pointer is-1
. - If
front == -1
, the queue is empty, returntrue
. - 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, returntrue
. - 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 Dynamic allocation ensures memory is allocated only as needed, which is especially useful in memory-constrained systems.
|
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() { int Peek() { int IsEmpty() { int IsFull() { void DisplayQueue() { int main() { // Dynamically allocate memory for the queue int choice, element; switch (choice) { // Free the dynamically allocated memory 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: