Queue using Array in C
Here you will learn and get the program code of Queue using Array in C programming.
A queue is a linear collection in data structure, that follows the First-In-First-Out (FIFO) principle. It stores elements in a manner where the first item added is the first to be removed. Queues are used for managing and processing data in a sequential and orderly fashion, like a waiting line.
Difference between Queue and Stack
Characteristics | Queue | Stack |
Order of Access | First-In-First-Out (FIFO) | Last-In-First-Out (LIFO) |
Insertion and Deletion | Enqueue (rear), Dequeue (front) | Push (top), Pop (top) |
Typical Use Cases | Managing tasks in order of arrival (e.g., print jobs), breadth-first search | Tracking function calls in recursion, undo/redo functionality |
Real-world Analogies | People waiting in line (e.g., ticket counter), checkout lines | Stack of plates, books |
Data Movement | Enqueued at one end, dequeued from the other end | Pushed and popped from the same end (the top) |
Program code of Queue using Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | //Queue using Array in C #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 10 int queue[MAX_SIZE]; int front = -1; int rear = -1; // Check if queue is empty int isEmpty() { return (front == -1 && rear == -1); } // Check if queue is full int isFull() { return (rear == MAX_SIZE - 1); } // add element to the queue void enqueue(int value) { if (isFull()) { printf("Queue is full. Cannot enqueue.\n"); return; } if (isEmpty()) { front = 0; rear = 0; } else { rear++; } queue[rear] = value; } // remove element from the queue int dequeue() { if (isEmpty()) { printf("Queue is empty. Cannot dequeue.\n"); return -1; // Return a sentinel value indicating an error } int removedValue = queue[front]; if (front == rear) { // Last element in the queue, reset front and rear front = -1; rear = -1; } else { front++; } return removedValue; } // Display the elements from queue void display() { if (isEmpty()) { printf("Queue is empty.\n"); return; } printf("Queue elements: "); for (int i = front; i <= rear; i++) { printf("%d ", queue[i]); } printf("\n"); } int main() { int choice, value; while (1) { printf("Queue Menu:\n"); printf("1. Enqueue\n"); printf("2. Dequeue\n"); printf("3. Display\n"); printf("4. Quit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: if (isFull()) { printf("Queue is full. Cannot enqueue.\n"); } else { printf("Enter the value to enqueue: "); scanf("%d", &value); enqueue(value); //printf("Enqueued: %d\n", value); } break; case 2: if (isEmpty()) { printf("\nQueue is empty. Cannot dequeue.\n"); } else { value = dequeue(); printf("Dequeued: %d\n", value); } break; case 3: display(); break; case 4: exit(0); // exit the program default: printf("\nInvalid choice. Please try again.\n"); } } return 0; } |
Output
Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 1
Enter the value to enqueue: 10
Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 1
Enter the value to enqueue: 20
Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 1
Enter the value to enqueue: 30
Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 3
Queue elements: 10 20 30
Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 2
Dequeued: 10
Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 3
Queue elements: 20 30
Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 4