September 13th, 2012, 03:33 AM
[RESOLVED] Help in Enqueue & Dequeue
9/14/2012 - I've not used any of the suggestions made by the members of the forums. I decided to fix the current problem I had instead.
Use your language’s native queue class to write the following program: create a class, Patient. The class has 2 members: name of type string; wait_time, a non-negative integer. Read definitions of patients from a file and add the patients to a queue. Write a loop that dequeues a patient from the queue and then decrement the patient’s wait_time. If the patient’s wait time is greater than 0, then the program enqueues the patient again. Otherwise it discards the patient. The loop continues until the queue is empty.
I've worked the WHOLE day on this **** and it's not looking very good. It compiles and runs but after inputting stuff, the program stops responding. I'm not very good in data structures and my logic is so **** right now. Any from of help is appreciated.
bool empty() const;
bool Patient::empty() const
//Post: Return true if the Queue is empty, otherwise return false.
return count == 0;
using namespace std;
name = name;
wait_time = 0;
count = 0;
void serve(string waiting_list, int dequeue_in, int maxperson, int rounds)
int serve_entry = rounds-1;
cout << "\n";
dequeue_in[serve_entry] = dequeue_in[serve_entry] - 1;
cout<< ">>P" << rounds <<" - "
<< waiting_list[serve_entry] << " : " << dequeue_in[serve_entry];
int next_name = serve_entry+1;
int next_wait = serve_entry+1;
for (; next_name < maxperson; next_name++, next_wait++)
cout<< "\n"<< "P" << next_name+1 <<" - "
<< waiting_list[next_name] << " : " << dequeue_in[next_wait];
cout << "\n";
void append(string waiting_list, int dequeue_in, int maxperson, int rounds)
int append_entry = rounds - 1;
for (; append_entry < maxperson; append_entry++)
temp = waiting_list[append_entry]; //Holds the value for a bit
for ( int i = 1; i < maxperson; i++)
waiting_list[i-1] = waiting_list[i];
waiting_list[maxperson-1] = temp;
// int to_back = maxperson - 1 - append_entry;
// cout<< "\n"<< "P" << to_back+1 <<" - "
// << waiting_list[to_back] << " : " << dequeue_in[to_back];
}//maybe do a swap?
cout <<"How many patients total? ";
cin >> total_patients;
cout << "\n";
const int maxperson = total_patients; //Pass a varying variable to a constant like this.
string waiting_list[maxperson]; //Arrays always need a [size].
for (int i = 1, j = 0; i <= total_patients; i++, j++)
cout << "Enter patient #"<< i << "'s name: ";
cin >> patients.name;
waiting_list[i-1] = patients.name;
cout << "What is their wait time? ";
cin >> patients.wait_time;
dequeue_in[j] = patients.wait_time;
int rounds = 0;
for(int i=0;i < maxperson; i++)
rounds += dequeue_in[i];
cout << "Max rounds: " << rounds <<"\n\n";
/* for(int i = 0, j= 0; i < total_patients; i++, j++)
if (i < total_patients - 1)
cout<< waiting_list[i] << " : " << dequeue_in[j] << ", ";
cout<< waiting_list[i] << " : " << dequeue_in[j] << endl;
//Maybe put the round looping here instead.
for (int i = 1; i <= rounds; i++)
cout<< "\n"<< "_______Round " << i << " Serve________" << endl; //Rounds depend on the person with the most wait_time.
serve(waiting_list, dequeue_in, maxperson, i);
cout<< "\n"<< "_______Round " << i << " Append________" << endl; //Rounds depend on the person with the most wait_time.
append(waiting_list, dequeue_in, maxperson, i);
Last edited by Lethargic; September 14th, 2012 at 05:31 AM.
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!