Advertisement
Guest User

Untitled

a guest
May 5th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. using namespace std;
  5.  
  6.  
  7. //Implementare coada dubla
  8.  
  9. struct Command {
  10.     char* name;
  11. };
  12. struct QueueNode
  13. {
  14.     Command* info;
  15.     QueueNode* next;
  16.     QueueNode* prev;
  17. };
  18. QueueNode* createQueueNode(Command* is)
  19. {
  20.     //1. declare a new variable
  21.     QueueNode* newEl = NULL;
  22.     //2. allocate memory
  23.     newEl = (QueueNode*)malloc(sizeof(QueueNode));
  24.     //3. initialization
  25.     newEl->info = is;
  26.     newEl->next = newEl->prev = NULL;
  27.     //4. return
  28.     return newEl;
  29. }
  30. void putQueueElement(QueueNode*& head, QueueNode* node)
  31. {
  32.     if (head == NULL)
  33.     {
  34.         //one queue element (circular)
  35.         head = node;
  36.         head->next = head->prev = head;
  37.     }
  38.     else
  39.     {
  40.         node->next = head;
  41.         node->prev = head->prev;
  42.         head->prev->next = node;
  43.         head->prev = node;
  44.     }
  45. }
  46. void printfQueueElements(QueueNode* head)
  47. {
  48.     if (head)
  49.     {
  50.         QueueNode* tmp = head;
  51.         while (tmp->next != head)
  52.         {
  53.             printf("Comanda: %s\n", tmp->info->name);
  54.             tmp = tmp->next;
  55.         }
  56.         printf("Comanda: %s\n", tmp->info->name);
  57.         printf("----------Backwards----------------\n");
  58.         while (tmp != head)
  59.         {
  60.             printf("Comanda: %s\n", tmp->info->name);
  61.             tmp = tmp->prev;
  62.         }
  63.         printf("Comanda: %s\n", tmp->info->name);
  64.     }
  65. }
  66. bool emptyQueue(QueueNode* head)
  67. {
  68.     return (head == nullptr) ? true : false;
  69. }
  70. void printQueueElement(QueueNode* el)
  71. {
  72.     printf("Subscriber: %s\n", el->info->name);
  73.     printf("\n");
  74. }
  75. QueueNode* getQueueElement(QueueNode*& head)
  76. {
  77.     QueueNode* aux = head;
  78.     if (head == head->next && head == head->prev)
  79.     {
  80.         head = nullptr;
  81.     }
  82.     else
  83.     {
  84.         head->next->prev = head->prev;
  85.         head->prev->next = head->next;
  86.         head = head->next;
  87.     }
  88.     return aux;
  89. }
  90.  
  91. //End Coada Dubla
  92.  
  93. void main()
  94. {
  95.     //Apel Coada Dubla
  96.     FILE* pFile = fopen("Text.txt", "r");
  97.     QueueNode* head = NULL;
  98.     if (pFile)
  99.     {
  100.         char buffer[50];
  101.         Command* subscriber = NULL;
  102.         fscanf(pFile, "%[^\n]s", buffer);
  103.  
  104.         while (!feof(pFile))
  105.         {
  106.             subscriber = (Command*)malloc(sizeof(Command));
  107.             subscriber->name = (char*)malloc(strlen(buffer) + 1);
  108.             strcpy(subscriber->name, buffer);
  109.             fscanf(pFile, "\n");
  110.             QueueNode* newNode = createQueueNode(subscriber);
  111.             putQueueElement(head, newNode);
  112.             fscanf(pFile, "%[^\n]s", buffer);
  113.         }
  114.         fclose(pFile);
  115.     }
  116.     printf("----------Print circular double link list--------\n");
  117.     printfQueueElements(head);
  118.     printf("\n----------Print Queue elements----------\n");
  119.     while (!emptyQueue(head))
  120.     {
  121.         QueueNode* element = getQueueElement(head);
  122.         printQueueElement(element);
  123.     }
  124.     getchar();
  125.     //End Apel Coada Dubla
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement