Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stdio.h>
- #include<stdlib.h>
- using namespace std;
- //Implementare coada dubla
- struct Command {
- char* name;
- };
- struct QueueNode
- {
- Command* info;
- QueueNode* next;
- QueueNode* prev;
- };
- QueueNode* createQueueNode(Command* is)
- {
- //1. declare a new variable
- QueueNode* newEl = NULL;
- //2. allocate memory
- newEl = (QueueNode*)malloc(sizeof(QueueNode));
- //3. initialization
- newEl->info = is;
- newEl->next = newEl->prev = NULL;
- //4. return
- return newEl;
- }
- void putQueueElement(QueueNode*& head, QueueNode* node)
- {
- if (head == NULL)
- {
- //one queue element (circular)
- head = node;
- head->next = head->prev = head;
- }
- else
- {
- node->next = head;
- node->prev = head->prev;
- head->prev->next = node;
- head->prev = node;
- }
- }
- void printfQueueElements(QueueNode* head)
- {
- if (head)
- {
- QueueNode* tmp = head;
- while (tmp->next != head)
- {
- printf("Comanda: %s\n", tmp->info->name);
- tmp = tmp->next;
- }
- printf("Comanda: %s\n", tmp->info->name);
- printf("----------Backwards----------------\n");
- while (tmp != head)
- {
- printf("Comanda: %s\n", tmp->info->name);
- tmp = tmp->prev;
- }
- printf("Comanda: %s\n", tmp->info->name);
- }
- }
- bool emptyQueue(QueueNode* head)
- {
- return (head == nullptr) ? true : false;
- }
- void printQueueElement(QueueNode* el)
- {
- printf("Subscriber: %s\n", el->info->name);
- printf("\n");
- }
- QueueNode* getQueueElement(QueueNode*& head)
- {
- QueueNode* aux = head;
- if (head == head->next && head == head->prev)
- {
- head = nullptr;
- }
- else
- {
- head->next->prev = head->prev;
- head->prev->next = head->next;
- head = head->next;
- }
- return aux;
- }
- //End Coada Dubla
- void main()
- {
- //Apel Coada Dubla
- FILE* pFile = fopen("Text.txt", "r");
- QueueNode* head = NULL;
- if (pFile)
- {
- char buffer[50];
- Command* subscriber = NULL;
- fscanf(pFile, "%[^\n]s", buffer);
- while (!feof(pFile))
- {
- subscriber = (Command*)malloc(sizeof(Command));
- subscriber->name = (char*)malloc(strlen(buffer) + 1);
- strcpy(subscriber->name, buffer);
- fscanf(pFile, "\n");
- QueueNode* newNode = createQueueNode(subscriber);
- putQueueElement(head, newNode);
- fscanf(pFile, "%[^\n]s", buffer);
- }
- fclose(pFile);
- }
- printf("----------Print circular double link list--------\n");
- printfQueueElements(head);
- printf("\n----------Print Queue elements----------\n");
- while (!emptyQueue(head))
- {
- QueueNode* element = getQueueElement(head);
- printQueueElement(element);
- }
- getchar();
- //End Apel Coada Dubla
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement