Hi guys. I'm currently doing a queue system and trying to emulate a real life example of a clinic.
Patients press <1> to register their particulars. If it is full system will tell them that queue is full.
Doctor press <2> to see the next patient. If there is no more patients, the system will tell the doctor that queue is empty.
Press <3> to see who is currently in the queue.
I am using an array for this queue example. My array has a size of 4(for now).
I use the variables "first" and "last" as indexes telling me which is the first and last patient in the queue. (Eg. i use it like this: queueArray[first]. if first == 0 then patient in queueArray[0] is the first patient in the queue)
I believe that there is a problem in either my add function or my printAll function. (I have a hunch its the add function since I took 2 days to figure it out). Btw I am using classes.
#include <iostream>
#include <string>
#include "patient.h"
#include "queue.h"
usingnamespace std;
// constructor
queue::queue(int maxSize) {
arraySize = maxSize;
queueArray = new patient[maxSize]; // the array to contain patient objects
first = -1; // a index to show who is first
last = 0; // a index to show who is last
};
bool queue::isEmpty () {
if (first == -1 && last == 0) {
returntrue;
}
else {
returnfalse;
}
};
bool queue::isFull (){
if (first == 0 && last == arraySize) {
returntrue;
}
else {
returnfalse;
}
};
void queue::add (string inputName, string inputId, int inputQueueNumber) {
// if last is out of bounds
if (isFull() == true) {
cout << "Sorry Queue is full!" <<endl;
}
elseif (isEmpty() == true) {
// 1st initialize the patient's record
newPatient.initialize(inputName, inputId, inputQueueNumber);
first = 0;
queueArray[first] = newPatient;
}
//else if (isFull() == false && last == arraySize) {
// // 1st initialize the patient's record
// newPatient.initialize(inputName, inputId, inputQueueNumber);
// last = 0;
// queueArray[last] = newPatient;
// last+=1;
//}
elseif (last >= first) {
last+=1;
if (last < arraySize) {
newPatient.initialize(inputName, inputId, inputQueueNumber);
queueArray[last] = newPatient;
}
else {
if (first != 0) {
last = last%arraySize; //reset last index pointer
newPatient.initialize(inputName, inputId, inputQueueNumber);
queueArray[last] = newPatient;
}
else {
cout << "Queue is full!" <<endl;
}
}
};
};
void queue::dequeue(void) {
//// move the first pointer index to the next element as
//// the current element is not needed anymore
//first+=1;
// As long as array is empty then
// move the first index pointer to the next patient
if(isEmpty() == false) {
first+=1;
}
//if (first == last) {
// // reset index pointers
// // indicates that array is empty
// first = -1;
// last = 0;
//}
else {
cout << "Queue is empty!" <<endl;
}
};
void queue::printNext(void) {
if (isEmpty() == true) {
cout << "There are currently no patients in the queue!" <<endl;
}
elseif (first >= 0) {
cout <<endl << queueArray[first];
}
};
void queue::printAll(void) {
if (isEmpty() == true) {
cout << "There are currently no patients in the queue!" <<endl;
}
elseif (first == last) {
}
elseif (first < last) {
/*for (int iterator = first; iterator < last; iterator++) {
cout << queueArray[iterator] <<endl;
}*/
int f = first;
while(f < last) {
cout << queueArray[f];
f++;
}
}
elseif (first > last) {
int f = first;
int l = last;
while (f < arraySize) {
cout << queueArray[first];
f+=1;
}
while (l < first) {
cout << queueArray[l];
l+=1;
}
}
};
One of the problems I encountered is when I pressed <1> and added a patient, then I pressed <3> it doesnt show the patient. But funnily enough when I added 4 patients (pressed <1> 4 times) and then pressed <3> the program shows all the patients.
Also I cant seem to add patients to the front of the array(Circular queue). If lets say queue is full and then I pressed <2> then first patient is now queueArray[1]. When I want to add the next patient to queueArray[0] I used last = last%arraySize to make. But there seems to be some problem..
public:
bool isEmpty() { return num_of_elements == 0; }
bool isFull() { return num_of_elements == arraySize; }
void add(...)
{
if (!isFull())
{
int back = (front + num_of_elements++) % arraySize;
queueArray[back] = ...;
}
}
void dequeue()
{
if (!isEmpty())
{
front = (front + 1) % arraySize;
--num_of_elements;
}
}
void printAll()
{
for (int count = 0; count < num_of_elements; ++count)
{
int i = (front + count) % arraySize;
cout << queueArray[i];
}
}
private:
...
int front, num_of_elements; // initialize to 0 in constructor
Why does your queue::add(...) construct patients? a patient should be created outside the queue and passed in as an argument. I would also recommend using templates to make your code generic.