Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "queue_vaksinasi.h"
- using namespace std;
- void createQueue(Queue &Q) {
- head(Q) = NULL;
- tail(Q) = NULL;
- }
- bool isEmpty(Queue Q) {
- return head(Q) == NULL;
- }
- ElemQ* createElemQueue(string nama, int usia, string pekerjaan, int nomor_antrean) {
- ElemQ *P = new ElemQ;
- info(P).nama = nama;
- info(P).usia = usia;
- info(P).pekerjaan = pekerjaan;
- info(P).prioritas = (usia >= 60 || pekerjaan == "tenaga kesehatan");
- info(P).nomor_antrean = nomor_antrean;
- info(P).kondisi_darurat = false;
- next(P) = NULL;
- return P;
- }
- void enqueue(Queue &Q, ElemQ *P) {
- if (isEmpty(Q)) {
- head(Q) = P;
- tail(Q) = P;
- }else if (info(P).prioritas){
- if (!info(head(Q)).prioritas){
- next(P) = head(Q);
- head(Q) = P;
- }else{
- ElemQ *temp = head(Q);
- while (next(temp) != NULL &&
- info(next(temp)).prioritas) {
- temp = next(temp);
- }
- next(P) = next(temp);
- next(temp) = P;
- if (next(P) == NULL) {
- tail(Q) = P;
- }
- }
- }else{
- next(tail(Q)) = P;
- tail(Q) = P;
- }
- }
- void dequeue(Queue &Q, ElemQ *&P) {
- if (isEmpty(Q)) {
- P = NULL;
- cout << "Semua warga telah terlayani."
- << endl;
- } else {
- P = head(Q);
- head(Q) = next(head(Q));
- if (head(Q) == NULL) {
- tail(Q) = NULL;
- }
- next(P) = NULL;
- }
- }
- ElemQ* front(Queue Q) {
- return head(Q);
- }
- ElemQ* back(Queue Q) {
- return tail(Q);
- }
- int size(Queue Q) {
- int count = 0;
- ElemQ *temp = head(Q);
- while (temp != NULL) {
- count++;
- temp = next(temp);
- }
- return count;
- }
- void printInfo(Queue Q){
- ElemQ *dataFirst = head(Q);
- while(dataFirst != NULL){
- cout<<"Nama : " << info(dataFirst).nama<<endl;
- cout<<"Usia : " << info(dataFirst).usia<<endl;
- cout<<"Pekerjaan : " << info(dataFirst).pekerjaan<<endl;
- if(info(dataFirst).prioritas){
- cout<<"Prioritas : Ya" <<endl;
- }else{
- cout<<"Prioritas : Tidak" <<endl;
- }
- cout<<"Waktu Antrean : " << info(dataFirst).waktu_daftar<<endl;
- cout<<"Nomor Antrean : " << info(dataFirst).nomor_antrean<<endl;
- cout<<"------------------------------------------"<<endl;
- dataFirst = next(dataFirst);
- }
- }
- void serveQueue(Queue &Q){
- int MaxData = 100;
- int servedCount = 0;
- ElemQ *dataFirst;
- while (!isEmpty(Q) && servedCount < MaxData) {
- dequeue(Q, dataFirst);
- cout<<"Melayani warga : "<<endl;
- cout<<"Nama : " << info(dataFirst).nama<<endl;
- cout<<"Usia : " << info(dataFirst).usia<<endl;
- cout<<"Pekerjaan : " << info(dataFirst).pekerjaan<<endl;
- if(info(dataFirst).prioritas){
- cout<<"Prioritas : Ya" <<endl;
- }else{
- cout<<"Prioritas : Tidak" <<endl;
- }
- cout<<"Vaksinasi berhasil."<<endl;
- cout<<"------------------------------------------"<<endl;
- servedCount++;
- }
- if (servedCount == MaxData) {
- cout << "Kapasitas telah penuh." << endl;
- }
- if (!isEmpty(Q)) {
- cout << "Mohon maaf warga yang belum terlayani tolong datang besok." << endl;
- }
- }
- void reassignQueue(Queue &Q) {
- Queue priorityQueue, normalQueue;
- createQueue(priorityQueue);
- createQueue(normalQueue);
- ElemQ *currentElem;
- while (!isEmpty(Q)) {
- dequeue(Q, currentElem);
- if (info(currentElem).prioritas) {
- enqueue(priorityQueue, currentElem);
- } else {
- enqueue(normalQueue, currentElem);
- }
- }
- while (!isEmpty(priorityQueue)) {
- dequeue(priorityQueue, currentElem);
- enqueue(Q, currentElem);
- }
- while (!isEmpty(normalQueue)) {
- dequeue(normalQueue, currentElem);
- enqueue(Q, currentElem);
- }
- }
- void checkWaitingTime(Queue &Q,int waktu_sekarang){
- ElemQ *dataFirst = head(Q);
- while(dataFirst != NULL){
- info(dataFirst).waktu_daftar += waktu_sekarang;
- if(info(dataFirst).waktu_daftar >= 120){
- info(dataFirst).prioritas = true;
- }
- dataFirst = next(dataFirst);
- }
- }
- void emergencyHandle(Queue &Q, int nomor_antrean) {
- ElemQ *prev = NULL;
- ElemQ *current = head(Q);
- ElemQ *emergency = NULL;
- while (current != NULL) {
- if (info(current).nomor_antrean == nomor_antrean) {
- info(current).kondisi_darurat = true;
- emergency = current;
- break;
- }
- prev = current;
- current = next(current);
- }
- if (emergency == NULL) {
- cout << "Nomor Antrian tidak ditemukan " <<endl;
- return;
- }
- if (emergency == head(Q)) {
- return;
- }
- if (prev != NULL) {
- next(prev) = next(emergency);
- }
- if (emergency == tail(Q)) {
- tail(Q) = prev;
- }
- next(emergency) = head(Q);
- head(Q) = emergency;
- }
- void updatePriority(Queue &Q){
- Queue priorityQueue, normalQueue;
- createQueue(priorityQueue);
- createQueue(normalQueue);
- ElemQ *currentElem;
- while (!isEmpty(Q)) {
- dequeue(Q, currentElem);
- if(info(currentElem).kondisi_darurat){
- enqueue(priorityQueue, currentElem);
- emergencyHandle(priorityQueue,info(currentElem).nomor_antrean);
- }else if (info(currentElem).prioritas || info(currentElem).waktu_daftar >= 120){
- enqueue(priorityQueue, currentElem);
- } else {
- enqueue(normalQueue, currentElem);
- }
- }
- while (!isEmpty(priorityQueue)) {
- dequeue(priorityQueue, currentElem);
- enqueue(Q, currentElem);
- }
- while (!isEmpty(normalQueue)) {
- dequeue(normalQueue, currentElem);
- enqueue(Q, currentElem);
- }
- }
- ElemQ* findAndRemove(Queue &Q, int nomor_antrean){
- ElemQ *prev = NULL;
- ElemQ *current = head(Q);
- while (current != NULL) {
- if (info(current).nomor_antrean == nomor_antrean) {
- if(current == head(Q)){
- head(Q) = next(current);
- }
- else if(current == tail(Q)){
- tail(Q) = prev;
- next(prev) = NULL;
- }else{
- next(prev) = next(current);
- }
- next(current) = NULL;
- return current;
- }
- prev = current;
- current = next(current);
- }
- cout<< "Elemen tidak ditemukan" << endl;
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement