Advertisement
ChaeYuriya

Assignment 8 : queue_vaksinasi.cpp

Nov 7th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.44 KB | None | 0 0
  1. #include <iostream>
  2. #include "queue_vaksinasi.h"
  3.  
  4. using namespace std;
  5.  
  6. void createQueue(Queue &Q) {
  7.  head(Q) = NULL;
  8.  tail(Q) = NULL;
  9. }
  10.  
  11. bool isEmpty(Queue Q) {
  12.  return head(Q) == NULL;
  13. }
  14. ElemQ* createElemQueue(string nama, int usia, string pekerjaan, int nomor_antrean) {
  15.  ElemQ *P = new ElemQ;
  16.  info(P).nama = nama;
  17.  info(P).usia = usia;
  18.  info(P).pekerjaan = pekerjaan;
  19.  info(P).prioritas = (usia >= 60 || pekerjaan == "tenaga kesehatan");
  20.  info(P).nomor_antrean = nomor_antrean;
  21.  info(P).kondisi_darurat = false;
  22.  next(P) = NULL;
  23.  return P;
  24. }
  25.  
  26. void enqueue(Queue &Q, ElemQ *P) {
  27.  if (isEmpty(Q)) {
  28.      head(Q) = P;
  29.      tail(Q) = P;
  30.  }else if (info(P).prioritas){
  31.     if (!info(head(Q)).prioritas){
  32.         next(P) = head(Q);
  33.         head(Q) = P;
  34.     }else{
  35.         ElemQ *temp = head(Q);
  36.         while (next(temp) != NULL &&
  37.         info(next(temp)).prioritas) {
  38.         temp = next(temp);
  39.     }
  40.     next(P) = next(temp);
  41.     next(temp) = P;
  42.     if (next(P) == NULL) {
  43.         tail(Q) = P;
  44.     }
  45.     }
  46.  }else{
  47.     next(tail(Q)) = P;
  48.     tail(Q) = P;
  49.  }
  50. }
  51.  
  52. void dequeue(Queue &Q, ElemQ *&P) {
  53.  if (isEmpty(Q)) {
  54.  P = NULL;
  55.  cout << "Semua warga telah terlayani."
  56. << endl;
  57.  } else {
  58.  P = head(Q);
  59.  head(Q) = next(head(Q));
  60.  if (head(Q) == NULL) {
  61.  tail(Q) = NULL;
  62.  }
  63.  next(P) = NULL;
  64. }
  65. }
  66.  
  67. ElemQ* front(Queue Q) {
  68.  return head(Q);
  69. }
  70.  
  71. ElemQ* back(Queue Q) {
  72.  return tail(Q);
  73. }
  74.  
  75. int size(Queue Q) {
  76.  int count = 0;
  77.  ElemQ *temp = head(Q);
  78.  while (temp != NULL) {
  79.  count++;
  80.  temp = next(temp);
  81.  }
  82.  return count;
  83. }
  84.  
  85. void printInfo(Queue Q){
  86.     ElemQ *dataFirst = head(Q);
  87.     while(dataFirst != NULL){
  88.         cout<<"Nama : " << info(dataFirst).nama<<endl;
  89.         cout<<"Usia : " << info(dataFirst).usia<<endl;
  90.         cout<<"Pekerjaan : " << info(dataFirst).pekerjaan<<endl;
  91.         if(info(dataFirst).prioritas){
  92.             cout<<"Prioritas : Ya" <<endl;
  93.         }else{
  94.             cout<<"Prioritas : Tidak" <<endl;
  95.         }
  96.  
  97.         cout<<"Waktu Antrean : " << info(dataFirst).waktu_daftar<<endl;
  98.         cout<<"Nomor Antrean : " << info(dataFirst).nomor_antrean<<endl;
  99.         cout<<"------------------------------------------"<<endl;
  100.         dataFirst = next(dataFirst);
  101.     }
  102. }
  103.  
  104. void serveQueue(Queue &Q){
  105.     int MaxData = 100;
  106.     int servedCount = 0;
  107.     ElemQ *dataFirst;
  108.  
  109.     while (!isEmpty(Q) && servedCount < MaxData) {
  110.         dequeue(Q, dataFirst);
  111.  
  112.         cout<<"Melayani warga : "<<endl;
  113.         cout<<"Nama : " << info(dataFirst).nama<<endl;
  114.         cout<<"Usia : " << info(dataFirst).usia<<endl;
  115.         cout<<"Pekerjaan : " << info(dataFirst).pekerjaan<<endl;
  116.         if(info(dataFirst).prioritas){
  117.             cout<<"Prioritas : Ya" <<endl;
  118.         }else{
  119.             cout<<"Prioritas : Tidak" <<endl;
  120.         }
  121.         cout<<"Vaksinasi berhasil."<<endl;
  122.         cout<<"------------------------------------------"<<endl;
  123.         servedCount++;
  124.     }
  125.  
  126.     if (servedCount == MaxData) {
  127.         cout << "Kapasitas telah penuh." << endl;
  128.     }
  129.  
  130.     if (!isEmpty(Q)) {
  131.         cout << "Mohon maaf warga yang belum terlayani tolong datang besok." << endl;
  132.     }
  133. }
  134.  
  135. void reassignQueue(Queue &Q) {
  136.     Queue priorityQueue, normalQueue;
  137.     createQueue(priorityQueue);
  138.     createQueue(normalQueue);
  139.  
  140.     ElemQ *currentElem;
  141.  
  142.     while (!isEmpty(Q)) {
  143.         dequeue(Q, currentElem);
  144.  
  145.         if (info(currentElem).prioritas) {
  146.             enqueue(priorityQueue, currentElem);
  147.         } else {
  148.             enqueue(normalQueue, currentElem);
  149.         }
  150.     }
  151.  
  152.     while (!isEmpty(priorityQueue)) {
  153.         dequeue(priorityQueue, currentElem);
  154.         enqueue(Q, currentElem);
  155.     }
  156.  
  157.     while (!isEmpty(normalQueue)) {
  158.         dequeue(normalQueue, currentElem);
  159.         enqueue(Q, currentElem);
  160.     }
  161. }
  162.  
  163. void checkWaitingTime(Queue &Q,int waktu_sekarang){
  164.     ElemQ *dataFirst = head(Q);
  165.     while(dataFirst != NULL){
  166.         info(dataFirst).waktu_daftar += waktu_sekarang;
  167.         if(info(dataFirst).waktu_daftar >= 120){
  168.             info(dataFirst).prioritas = true;
  169.         }
  170.         dataFirst = next(dataFirst);
  171.     }
  172. }
  173.  
  174. void emergencyHandle(Queue &Q, int nomor_antrean) {
  175.     ElemQ *prev = NULL;
  176.     ElemQ *current = head(Q);
  177.     ElemQ *emergency = NULL;
  178.  
  179.     while (current != NULL) {
  180.         if (info(current).nomor_antrean == nomor_antrean) {
  181.             info(current).kondisi_darurat = true;
  182.             emergency = current;
  183.             break;
  184.         }
  185.         prev = current;
  186.         current = next(current);
  187.     }
  188.  
  189.     if (emergency == NULL) {
  190.         cout << "Nomor Antrian tidak ditemukan " <<endl;
  191.         return;
  192.     }
  193.  
  194.     if (emergency == head(Q)) {
  195.         return;
  196.     }
  197.  
  198.     if (prev != NULL) {
  199.         next(prev) = next(emergency);
  200.     }
  201.  
  202.     if (emergency == tail(Q)) {
  203.         tail(Q) = prev;
  204.     }
  205.  
  206.     next(emergency) = head(Q);
  207.     head(Q) = emergency;
  208. }
  209.  
  210. void updatePriority(Queue &Q){
  211.     Queue priorityQueue, normalQueue;
  212.     createQueue(priorityQueue);
  213.     createQueue(normalQueue);
  214.  
  215.     ElemQ *currentElem;
  216.  
  217.     while (!isEmpty(Q)) {
  218.         dequeue(Q, currentElem);
  219.         if(info(currentElem).kondisi_darurat){
  220.             enqueue(priorityQueue, currentElem);
  221.             emergencyHandle(priorityQueue,info(currentElem).nomor_antrean);
  222.         }else if (info(currentElem).prioritas || info(currentElem).waktu_daftar >= 120){
  223.             enqueue(priorityQueue, currentElem);
  224.         } else {
  225.             enqueue(normalQueue, currentElem);
  226.         }
  227.     }
  228.  
  229.     while (!isEmpty(priorityQueue)) {
  230.         dequeue(priorityQueue, currentElem);
  231.         enqueue(Q, currentElem);
  232.     }
  233.  
  234.     while (!isEmpty(normalQueue)) {
  235.         dequeue(normalQueue, currentElem);
  236.         enqueue(Q, currentElem);
  237.     }
  238. }
  239.  
  240. ElemQ* findAndRemove(Queue &Q, int nomor_antrean){
  241.     ElemQ *prev = NULL;
  242.     ElemQ *current = head(Q);
  243.  
  244.     while (current != NULL) {
  245.         if (info(current).nomor_antrean == nomor_antrean) {
  246.  
  247.             if(current == head(Q)){
  248.                 head(Q) = next(current);
  249.             }
  250.             else if(current == tail(Q)){
  251.                 tail(Q) = prev;
  252.                 next(prev) = NULL;
  253.             }else{
  254.                 next(prev) = next(current);
  255.             }
  256.             next(current) = NULL;
  257.             return current;
  258.         }
  259.         prev = current;
  260.         current = next(current);
  261.     }
  262.  
  263.     cout<< "Elemen tidak ditemukan" << endl;
  264.     return NULL;
  265. }
  266.  
  267.  
  268.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement