Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // S005
- //struct Doctor - char* name
- //
- enum Speciality{
- NEUROLOGY = 5,
- CARDIOLOGY = 10
- };
- struct Doctor {
- char* name;
- Speciality spec;
- };
- struct List {
- Doctor* info;
- List* next;
- List* prev;
- };
- struct Queue {
- List* head;
- List* tail;
- };
- Doctor* createDoctor(char* buffer, int spec){
- Doctor* doctor = NULL;
- doctor = (Doctor*)malloc(sizeof(Doctor));
- doctor->name = (char*)malloc(sizeof(char)*strlen(buffer)+1);
- strcpy(doctor->name, buffer);
- doctor->spec = (Speciality)spec; // Speciality::NEUROLOGY; //because it's enum
- return doctor;
- }
- List* createNode(Doctor* info){
- List* node = (List*)malloc(sizeof(List));
- node->info = info;
- node->next = node->prev = NULL;
- return node;
- }
- void putQueue(Queue* queue, List* node){
- if (queue->head == NULL){
- queue->tail = queue->head = node;
- }
- else {
- node->prev = queue->tail;
- queue->tail->next = node;
- queue->tail = node;
- }
- }
- Doctor* getQueue(Queue& queue){
- Doctor* doctor = NULL;
- if (queue.head){
- doctor = queue.head->info;
- List* tmp = queue.head;//save the head
- queue.head = queue.head->next;
- queue.head->prev = NULL;
- free(tmp);
- }
- return doctor;
- }
- //NOT
- void printQueue(Queue queue){
- while (queue.head){
- printf("Name: %s\n",queue.head->info->name);
- queue.head = queue.head->next;
- }
- }
- void main(){
- FILE* pFile = fopen("Test.txt", "r");
- Queue queue;
- queue.head = queue.tail = NULL;//always init pointers with null oxccc err !
- if (pFile){
- char buffer[100];
- int spec;
- fscanf(pFile, "%s", buffer);
- while (!feof(pFile)){
- fscanf(pFile, "%%d", spec);
- //1. create the doctor element
- Doctor* doctor = createDoctor(buffer, spec);
- //2. create list element
- List* node = createNode(doctor);
- //3. enqueue the element
- putQueue(&queue, node);//reference
- fscanf(pFile, "%s", buffer);
- }
- fclose(pFile);
- //printQueue(queue);//extract print put them back ->queue/stack
- while (Doctor* doctor = getQueue(queue)){
- printf("Name: %s\n", doctor->name);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement