In the outpatient department of an hospital, where there is a single doctor for consultation, patients are issued tokens in increasing order of their arrival. The token also carries a number indicating the severity of the case of a patient. For example, if a patient with a cold and cough comes at 9.55AM, he gets a token number 10 and priority number 3 whereas when a patient with a heart attack comes at 10 AM, he gets a token number 11 and priority number 1. The hospital staff routes the patient with highest priority number to the doctor’s queue using a procedure described below:
1. There are 3 priority numbers depending on severity - 3 for casual cases, 2 for moderate cases and 1 for severe cases.
2. For every 1 patient in casual queue, 2 patients from moderate queue and 3 from severe queue are sent to the doctor’s queue. Let C indicate casual queue, M the moderate queue and S the severe queue. If there are 9 patients in C, 10 in M and 9 in S, then the doctor’s queue would be: 3S-2M-1C-3S-2M-1C-3S-2M-1C-2M-1C-2M-5C
Within the same queue, they can be sent in the order in which the input has been given. i.e, if token 10 and token 21 both have priority 3, then 10 will be sent first and 21 next. You do NOT have to sort based on token number for the same priority(The input will be provided in increasing order of token numbers).
Using Queue ADT provided to you, you have to develop a program that takes as input, token numbers and corresponding priority number of a set of 10 patients and output the doctor’s queue based on the procedure described above. Your output has to list the token numbers as is in the doctor’s queue.
INPUT:
20 integers - the first integer would be token number, the second integer would be priority number, the third would again be token number and the fourth would be corresponding priority number and so on.
OUTPUT:
List of token numbers as would be in the final doctor’s queue each separated by a single space. The last token number should also be followed by a space (so as not to have a separate “cout” for the last entry).#include <iostream> using namespace std; struct Node{ int info; Node *next; }; class QueueADT{ private: Node *qFront; Node *qRear; public: QueueADT(){ //constructor qFront = 0; qRear = 0; } bool isEmpty(){ if (qFront==NULL) return(true); else return(false); } int dequeue(){ int data; data = qFront->info; qFront = qFront->next; if (qFront==0) qRear = 0; return data; } void enqueue(int data){ Node *temp = new Node(); temp->info = data; temp->next = NULL; if (qFront == 0) { qFront = qRear = temp; } else{ qRear->next = temp; qRear = temp; } } }; int main(){ int i, count, token, priority; QueueADT q1, q2, q3, q4; for (i=0;i<10;i++){ cin>>token>>priority; if(priority==1) q1.enqueue(token); if(priority==2) q2.enqueue(token); if(priority==3) q3.enqueue(token); } i=0; while(i<10) { count=0; while( q1.isEmpty()==false && count<3 ) { q4.enqueue(q1.dequeue()); count++; i++; } count=0; while( q2.isEmpty()==false && count<2 ) { q4.enqueue(q2.dequeue()); count++; i++; } count=0; while( q3.isEmpty()==false && count<1 ) { q4.enqueue(q3.dequeue()); count++; i++; } } for (i=0;i<10;i++) cout<<q4.dequeue()<<" "; }
No comments:
Post a Comment