|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | + |
| 4 | +#define SIZE 10 // Define the size of the queue |
| 5 | + |
| 6 | +// FIFO: First In - First Out |
| 7 | + |
| 8 | +// Function prototypes |
| 9 | +void PrintQueue(int queue[SIZE], int size); |
| 10 | +void QueueConstructor(int *front, int *rear); |
| 11 | +bool IsQueueEmpty(int front, int rear); |
| 12 | +bool IsQueueFull(int rear); |
| 13 | +void Enqueue(int queue[SIZE], int value, int *rear); |
| 14 | +void Dequeue(int queue[SIZE], int *front, int rear); |
| 15 | +int QueueSize(int rear, int front); |
| 16 | + |
| 17 | +int main() |
| 18 | +{ |
| 19 | + // Initialize variables |
| 20 | + setlocale(LC_ALL, "en-US"); |
| 21 | + |
| 22 | + int front, rear, value; |
| 23 | + int queue[SIZE] = {0}; // Initialize the queue with zeros |
| 24 | + |
| 25 | + // Set up the queue with initial values for front and rear |
| 26 | + QueueConstructor(&front, &rear); |
| 27 | + |
| 28 | + // Enqueue some values |
| 29 | + Enqueue(queue, 5, &rear); |
| 30 | + Enqueue(queue, 7, &rear); |
| 31 | + |
| 32 | + // Dequeue and print values |
| 33 | + Dequeue(queue, &front, rear); |
| 34 | + Dequeue(queue, &front, rear); |
| 35 | + Dequeue(queue, &front, rear); |
| 36 | + |
| 37 | + // Enqueue a new value |
| 38 | + Enqueue(queue, 8, &rear); |
| 39 | + |
| 40 | + // Print the current queue and its size |
| 41 | + PrintQueue(queue, QueueSize(rear, front)); |
| 42 | + |
| 43 | + std::cout << std::endl; |
| 44 | + system("PAUSE"); |
| 45 | + return 0; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Prints the current state of the queue. |
| 50 | + * |
| 51 | + * @param queue The queue array. |
| 52 | + * @param size The size of the queue. |
| 53 | + */ |
| 54 | +void PrintQueue(int queue[SIZE], int size) |
| 55 | +{ |
| 56 | + // Print each element in the queue, followed by a separator |
| 57 | + for (int i = 0; i < SIZE; ++i) |
| 58 | + { |
| 59 | + std::cout << queue[i] << " - "; |
| 60 | + } |
| 61 | + std::cout << std::endl; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Initializes the queue with the front at 0 and the rear at -1. |
| 66 | + * |
| 67 | + * @param front A pointer to the front index of the queue. |
| 68 | + * @param rear A pointer to the rear index of the queue. |
| 69 | + */ |
| 70 | +void QueueConstructor(int *front, int *rear) |
| 71 | +{ |
| 72 | + *front = 0; // Set the front pointer to 0 |
| 73 | + *rear = -1; // Set the rear pointer to -1, indicating an empty queue |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Checks if the queue is empty. |
| 78 | + * |
| 79 | + * @param front The front index of the queue. |
| 80 | + * @param rear The rear index of the queue. |
| 81 | + * @return True if the queue is empty, otherwise false. |
| 82 | + */ |
| 83 | +bool IsQueueEmpty(int front, int rear) |
| 84 | +{ |
| 85 | + return front > rear; // Queue is empty if front index is greater than rear |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Checks if the queue is full. |
| 90 | + * |
| 91 | + * @param rear The rear index of the queue. |
| 92 | + * @return True if the queue is full, otherwise false. |
| 93 | + */ |
| 94 | +bool IsQueueFull(int rear) |
| 95 | +{ |
| 96 | + return rear == SIZE - 1; // Queue is full if rear reaches the last index |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Adds a value to the queue (enqueue operation). |
| 101 | + * |
| 102 | + * @param queue The queue array. |
| 103 | + * @param value The value to be added to the queue. |
| 104 | + * @param rear A pointer to the rear index of the queue. |
| 105 | + */ |
| 106 | +void Enqueue(int queue[SIZE], int value, int *rear) |
| 107 | +{ |
| 108 | + // Check if the queue is full |
| 109 | + if (IsQueueFull(*rear)) |
| 110 | + { |
| 111 | + std::cout << "Queue is full.\n"; |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + *rear = *rear + 1; // Increment the rear pointer |
| 116 | + queue[*rear] = value; // Place the value at the rear of the queue |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Removes a value from the queue (dequeue operation). |
| 122 | + * |
| 123 | + * @param queue The queue array. |
| 124 | + * @param front A pointer to the front index of the queue. |
| 125 | + * @param rear The rear index of the queue. |
| 126 | + */ |
| 127 | +void Dequeue(int queue[SIZE], int *front, int rear) |
| 128 | +{ |
| 129 | + // Check if the queue is empty |
| 130 | + if (IsQueueEmpty(*front, rear)) |
| 131 | + { |
| 132 | + std::cout << "\nUnable to dequeue from an empty queue.\n"; |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + // Print the value that is dequeued |
| 137 | + std::cout << "\nValue " << queue[*front] << " was removed.\n"; |
| 138 | + queue[*front] = 0; // Set the dequeued position to 0 (optional) |
| 139 | + *front = *front + 1; // Increment the front pointer |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Returns the current size of the queue. |
| 145 | + * |
| 146 | + * @param rear The rear index of the queue. |
| 147 | + * @param front The front index of the queue. |
| 148 | + * @return The current size of the queue. |
| 149 | + */ |
| 150 | +int QueueSize(int rear, int front) |
| 151 | +{ |
| 152 | + return (rear - front) + 1; // The size of the queue is the difference between rear and front |
| 153 | +} |
0 commit comments