Advertisement
ioana_martin98

Untitled

May 24th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "malloc.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5.  
  6. using namespace std;
  7. struct DLL
  8. {
  9.     char* profession;
  10.     float salary;
  11.     DLL* pNext;
  12.     DLL* pPrev;
  13. };
  14.  
  15. DLL* createElement(const char*, float);
  16. DLL* insertToHead(DLL*, DLL*);
  17. void insertToTail(DLL**, DLL*);
  18. //void insertToTail(List*&, List*);
  19. void afisareLista(DLL*);
  20. void insertMiddle(const char*, DLL*, DLL*);
  21. void main()
  22. {
  23.     FILE* pFile = fopen("Data.txt", "r");
  24.  
  25.     DLL* listHead = NULL;
  26.  
  27.     if (pFile)
  28.     {
  29.         char buffer[100]; float salary;
  30.         fscanf(pFile, "%s", buffer);
  31.         while (!feof(pFile))
  32.         {
  33.             fscanf(pFile, "%f", &salary);
  34.  
  35.             //1. create an element of type List*
  36.             DLL* newElement = createElement(buffer, salary);
  37.  
  38.             //2. insert the newly created element
  39.             //listHead = insertToHead(listHead, newElement);
  40.             insertToTail(&listHead, newElement);
  41.             fscanf(pFile, "%s", buffer);
  42.         }
  43.         fclose(pFile);
  44.  
  45.         afisareLista(listHead);
  46.         DLL* node = createElement("Avocat", 1300);
  47.         insertMiddle("Judecator", listHead, node);
  48.         printf("----------------------------\n");
  49.         afisareLista(listHead);
  50.     }
  51. }
  52.  
  53. void insertMiddle(const char* cond, DLL* head, DLL* node)
  54. {
  55.     while (head && strcmp(cond, head->profession) != 0)
  56.         head = head->pNext;
  57.     if (head)
  58.     {
  59.         //1.legare nod de structura
  60.         node->pNext = head->pNext;
  61.         node->pPrev = head;
  62.         //2.legare structura de
  63.         if(head->pNext)
  64.             head->pNext->pPrev = node;
  65.         head->pNext = node;
  66.     }
  67. }
  68.  
  69. void afisareLista(DLL* head)
  70. {
  71.     if (head)
  72.         do
  73.         {
  74.             printf("Profesie: %s, salariu: %f \n",
  75.                 head->profession, head->salary);
  76.             head = head->pNext;
  77.         } while (head->pNext);
  78.         printf("Profesie: %s, salariu: %f \n",
  79.             head->profession, head->salary);
  80.         printf("---------------------------------------\n");
  81.         for (DLL* index = head; index; index = index->pPrev)
  82.         {
  83.             printf("Profesie: %s, salariu: %f \n",
  84.                 index->profession, index->salary);
  85.         }
  86.  
  87. }
  88.  
  89. void insertToTail(DLL** head, DLL* newElemenet)
  90. {
  91.     //test if the first element exists
  92.     if (*head == NULL)
  93.     {
  94.         *head = newElemenet;
  95.     }
  96.     else
  97.     {
  98.         DLL* index = *head;
  99.         while (index->pNext)
  100.             index = index->pNext;
  101.         newElemenet->pPrev = index;
  102.         index->pNext = newElemenet;
  103.     }
  104. }
  105.  
  106. DLL* insertToHead(DLL* head, DLL* newElement)
  107. {
  108.     newElement->pNext = head;
  109.     if (head)
  110.         head->pPrev = newElement;
  111.     return newElement;
  112. }
  113.  
  114. DLL* createElement(const char* buffer, float salary)
  115. {
  116.     //1.allocate memory for the new element
  117.     DLL* newElement = (DLL*)malloc(sizeof(DLL));
  118.     //2.initialize it with the params
  119.     //(the connection attributes should remain NULL)
  120.     newElement->pNext = NULL;
  121.     newElement->pPrev = NULL;
  122.     newElement->salary = salary;
  123.     newElement->profession = (char*)malloc(strlen(buffer) + 1);
  124.     strcpy(newElement->profession, buffer);
  125.     //3.return the new element
  126.     return newElement;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement