Introduction:
The queue is a fundamental data structure based on the concept of First-In-First-Out (FIFO), where the first added element is the first one to be removed and processed
Appending new element in queue is implementing circularly.
Some functions in Queue are described as bellow:
bool is_full(): returns true if the queue is full (i.e., size == MAX_SIZE), otherwise, return false.bool is_empty(): returns true if the queue is empty(i.e., size == 0); otherwise, returns false.void clear(): Make the queue empty: size=0; front = 0; rear=-1;void enqueue(int element): append elements circularly at the rear of the queue. If the queue is full, no action is performed.
int pop(): remove and return the front element in the queue. If the queue is empty, no action is performed, and return error code -1 int peek(): return without removing the front element of the queue. Return eror code -1 if the queue is empty.
static const int MAX_SIZE = 4; // Capacity of the queue
int data[MAX_SIZE]; // Linearly stores data in a fixed-size array
int size; // Current number of elements in the queue
int front; // Index of the front element, where to take/process the data
int rear; // Index of the rear element, where to add the data
// Default constructor initializes an empty list
Queue() {
size = 0;
front = 0;
rear = -1;
}
Note: The capacity of the queue is MAX_SIZE = 4. If an operation cannot be performed, the queue remains unchanged.
Based on the above configuration and function definitions, complete the table in your question paper:
| Id | Operators | data[] | size | front | rear | is_empty() | is_full() |
|---|---|---|---|---|---|---|---|
| 0 | Queue myQueue = Queue(); | [] | 0 | 0 | -1 | true | false |
| 1 | myQueue.enqueue(5); | [5] | 1 | 0 | 0 | false | false |
| 2 | myQueue.enqueue(8); | [5, 8] | 2 | 0 | 1 | false | false |
| 3 | myQueue.enqueue(3); | ||||||
| 4 | myQueue.enqueue(7); | ||||||
| 5 | myQueue.enqueue(10); | ||||||
| 6 | myQueue.dequeue(); | ||||||
| 7 | myQueue.enqueue(2); | ||||||
| 8 | myQueue.enqueue(9); | ||||||
| 9 | myQueue.peek(); | ||||||
| 10 | myQueue.enqueue(4); | ||||||
| 11 | myQueue.dequeue(); | ||||||
| 12 | myQueue.enqueue(6); | ||||||
| 13 | myQueue.dequeue(); | ||||||
| 14 | myQueue.clear(); |