QUEUES: Array Implementation
QUEUES: Array Implementation
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
/*This function allows the user to view or 'peek' the first element in the queue,
i.e., the element at the front */
int peek() {
return queue[front];
}
/*This function checks if the queue is empty by checking the itemCount variable. If
there are 0 items in the queue, it is empty */
bool isEmpty() {
return itemCount == 0;
//shortened form of if (itemCount==0) return true; else return //false;
}
/*This function checks if the queue is full. If the number of items in the queue is
equal to its maximum capacity upon declaration, the queue is full */
bool isFull() {
return itemCount == MAX;
//shortened form of if (itemCount==MAX) return true; else return //false;
}
© Rafena Mustapha
/*This function returns the total number of elements in the queue*/
int size() {
return itemCount;
}
/*This functions adds a new element to the REAR of the queue if the queue is not
already full. */
void enqueue(int data) {
queue[++rear] = data;//move the rear index to the next free location in the
queue and then add the data to that new location
/* This is condensed code for:
rear = rear + 1; OR rear++;
queue[rear] = data; */
itemCount++; //update itemCount to show the size of the queue has increased
} else {
printf("Queue is full!\n");
}
}
/*This function removes an element from the front of the queue IF the queue is not
already empty*/
int dequeue() {
int data = queue[front++]; //grab the value that is at the front of the queue and
then move the front index to the next element in the queue. The next person in line is
at the front once the current front has been removed
/*This is condensed code for:
int data = queue[front];
front++; OR front = front + 1; */
if (!(isEmpty())){//check if the queue is already empty
if(front == MAX) { //if we have reached the last person in the line, i.e,
there is only one element left in the line
front = 0;//reset the front index to 0. Once the only item in the line has been
removed we will have to add any new item to the first index of the queue which is 0
}
itemCount--; //update itemCount to show the reduction of the size of the
array
return data;//return the item that was removed
} else {
printf("Queue is empty!\n");
}
int main() {
/* insert 5 items */
enqueue(3);
enqueue(5);
enqueue(9);
enqueue(1);
enqueue(12);
enqueue(15);
if(isFull()) {
© Rafena Mustapha
printf("Queue is full!\n");
}
enqueue(16);
enqueue(17);
enqueue(18);
enqueue(19);
printf("Element at front: %d\n",peek());
© Rafena Mustapha
An empty queue
F R = -1;
queue itemCount = 0;
0 1 2 3 4 5
F R
queue 10 20 30 40 itemCount = 4;
0 1 2 3 4 5
F R
queue 10 20 30 40 itemCount = 4;
0 1 2 3 4 5
data = 10
à F R
queue 10 20 30 40 itemCount = 3;
0 1 2 3 4 5
F R
queue 10 20 30 40
0 1 2 3 4 5
NOTE: While it is possible to move every item in the queue forward one space
each time an item is removed, it is not efficient coding because on a larger
data set, say 1000, we would have to move 1000 items forward. This is called
linear complexity, where the efficiency of output depends on the size of the
input. Programmers generally try to avoid linear complexity, although it is
considered better than higher degrees of complexity.
It is easier for us to increment the single variable that stores the index of
the first item in the queue. This makes for much more efficient coding.
© Rafena Mustapha
SPECIAL CASE
Notice the change in procedure if we are removing the very last item in the
queue:
itemCount = 1;
itemCount = 0;
if(front == MAX)
front =0;//At this point we reset our front to first index in the queue
This essentially removes the last item and resets the front index value. The
60 can now be overwritten.
Notice: We can also remove items without worry about the spaces created in
the front index values. As long as the variable front contains the correct
index of the item that is at the front to be removed, the code works
efficiently.
© Rafena Mustapha
Adding an item to a queue
queue[++rear] = data;
F à R
queue 10 20 30 40 itemCount = 4;
0 1 2 3 4 5
itemCount++;
F à R
queue 10 20 30 40 50 itemCount = 5;
0 1 2 3 4 5
if we call enqueue(50), 50 is added to
the rear of the queue
SPECIAL CASE
F rear = -1;
queue 40 50 60 itemCount = 3;
0 1 2 3 4 5
queue[++rear] = data;
R F
queue 70 40 50 60 itemCount = 4;
0 1 2 3 4 5
© Rafena Mustapha