ClavinJune

Hospital Nested Linked List

Jul 21st, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.40 KB | None | 0 0
  1. /*
  2. * @author: ClavinJune
  3. */
  4. #include <stdio.h>
  5. #include <malloc.h>
  6. #include <string.h>
  7. #define MAX 10
  8.  
  9. struct Patient{
  10.     char name[255];
  11.     struct Patient *next;
  12. };
  13.  
  14. struct Hospital{
  15.     char name[255];
  16.     int totalPatient;
  17.     struct Patient *queueHead, *queueTail;
  18.     struct Hospital *next;
  19. }*head, *tail;
  20.  
  21. struct Patient* createPatient( char name[] ){
  22.     struct Patient* temp = (struct Patient*)malloc(sizeof(struct Patient));
  23.     strcpy(temp->name, name);
  24.     temp->next = NULL;
  25.     return temp;
  26. };
  27.  
  28. struct Hospital* createHospital( char name[] ){
  29.     struct Hospital* temp = (struct Hospital*)malloc(sizeof(struct Hospital));
  30.     strcpy(temp->name, name);
  31.     temp->queueHead = temp->queueTail = NULL;
  32.     int totalPatient = 0;
  33.     temp->next = NULL;
  34.  
  35.     return temp;
  36. }
  37.  
  38. void pushHospital( struct Hospital* newHospital ){
  39.     if( !head )
  40.         head = tail = newHospital;
  41.     else{
  42.         tail->next = newHospital;
  43.         tail = newHospital;
  44.     }
  45. }
  46.  
  47. void pushPatient( struct Patient* newPatient ){
  48.     if( !head ) return;
  49.  
  50.     struct Hospital* tempHospital = head;
  51.     while( tempHospital->totalPatient == MAX - 1 ){
  52.         tempHospital = tempHospital->next; //pindah rumah sakit
  53.     }
  54.  
  55.     if( !tempHospital ) return; // rumah sakitnya penuh semua
  56.  
  57.     if( !tempHospital->queueHead ){
  58.         tempHospital->queueHead = tempHospital->queueTail = newPatient;
  59.     }
  60.     else{
  61.         tempHospital->queueTail->next = newPatient;
  62.         tempHospital->queueTail = newPatient;
  63.         ++tempHospital->totalPatient;
  64.     }
  65. }
  66.  
  67. void view(){
  68.     if( !head ) return;
  69.     struct Hospital* temp = head;
  70.     while( temp ){
  71.         printf("===========\n%s\n============\n", temp->name);
  72.         struct Patient* tempQueue = temp->queueHead;
  73.         while( tempQueue ){
  74.             printf("%s\n", tempQueue->name);
  75.             tempQueue = tempQueue->next;
  76.         }
  77.         temp = temp->next;
  78.     }
  79.  
  80. }
  81.  
  82. int main( int argc, char **argv, char **env ){
  83.     pushHospital( createHospital("A") );
  84.     pushHospital( createHospital("B") );
  85.     pushPatient( createPatient("A 1") );
  86.     pushPatient( createPatient("A 2") );
  87.     pushPatient( createPatient("A 3") );
  88.     pushPatient( createPatient("A 4") );
  89.     pushPatient( createPatient("A 5") );
  90.     pushPatient( createPatient("A 6") );
  91.     pushPatient( createPatient("A 7") );
  92.     pushPatient( createPatient("A 8") );
  93.     pushPatient( createPatient("A 9") );
  94.     pushPatient( createPatient("A 10") );
  95.     pushPatient( createPatient("B 1") );
  96.     pushPatient( createPatient("B 2") );
  97.     pushPatient( createPatient("B 3") );
  98.     view();
  99.     getchar();
  100.     return 0;
  101. }
Add Comment
Please, Sign In to add comment