Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // S005
  6. //struct Doctor - char* name
  7. //
  8.  
  9. enum Speciality{
  10.     NEUROLOGY = 5,
  11.     CARDIOLOGY = 10
  12. };
  13.  
  14. struct Doctor {
  15.     char* name;
  16.     Speciality spec;
  17. };
  18.  
  19. struct List {
  20.     Doctor* info;
  21.     List* next;
  22.     List* prev;
  23. };
  24.  
  25. struct Queue {
  26.     List* head;
  27.     List* tail;
  28. };
  29.  
  30. Doctor* createDoctor(char* buffer, int spec){
  31.     Doctor* doctor = NULL;
  32.     doctor = (Doctor*)malloc(sizeof(Doctor));
  33.     doctor->name = (char*)malloc(sizeof(char)*strlen(buffer)+1);
  34.     strcpy(doctor->name, buffer);
  35.     doctor->spec = (Speciality)spec; // Speciality::NEUROLOGY; //because it's enum
  36.    
  37.     return doctor;
  38. }
  39.  
  40. List* createNode(Doctor* info){
  41.     List* node = (List*)malloc(sizeof(List));
  42.     node->info = info;
  43.     node->next = node->prev = NULL;
  44.     return node;
  45. }
  46.  
  47. void putQueue(Queue* queue, List* node){
  48.     if (queue->head == NULL){
  49.         queue->tail = queue->head = node;
  50.     }
  51.     else {
  52.         node->prev = queue->tail;
  53.         queue->tail->next = node;
  54.         queue->tail = node;
  55.        
  56.     }
  57. }
  58.  
  59. Doctor* getQueue(Queue& queue){
  60.     Doctor* doctor = NULL;
  61.     if (queue.head){
  62.         doctor = queue.head->info;
  63.         List* tmp = queue.head;//save the head
  64.         queue.head = queue.head->next;
  65.         queue.head->prev = NULL;
  66.         free(tmp);
  67.     }
  68.  
  69.     return doctor;
  70.  
  71. }
  72.  
  73. //NOT
  74. void printQueue(Queue queue){
  75.     while (queue.head){
  76.         printf("Name: %s\n",queue.head->info->name);
  77.         queue.head = queue.head->next;
  78.     }
  79. }
  80.  
  81. void main(){
  82.     FILE* pFile = fopen("Test.txt", "r");
  83.     Queue queue;
  84.     queue.head = queue.tail = NULL;//always init pointers with null oxccc err !
  85.     if (pFile){
  86.         char buffer[100];
  87.         int spec;
  88.         fscanf(pFile, "%s", buffer);
  89.         while (!feof(pFile)){
  90.             fscanf(pFile, "%%d", spec);
  91.             //1. create the doctor element
  92.             Doctor* doctor = createDoctor(buffer, spec);
  93.             //2. create list element
  94.             List* node = createNode(doctor);
  95.             //3. enqueue the element
  96.             putQueue(&queue, node);//reference
  97.             fscanf(pFile, "%s", buffer);
  98.         }
  99.         fclose(pFile);
  100.         //printQueue(queue);//extract print put them back ->queue/stack
  101.         while (Doctor* doctor = getQueue(queue)){
  102.             printf("Name: %s\n", doctor->name);
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement