ioana_martin98

Untitled

May 24th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "malloc.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5. using namespace std;
  6.  
  7. struct DLL
  8. {
  9.     char* profession; //key in hashTable
  10.     float salary;
  11.     DLL* pNext;
  12. };
  13. struct HashTable
  14. {
  15.     DLL** accessTable;
  16.     int size;
  17. };
  18. DLL* createElement(char*, float);
  19. void initHashTable(HashTable*, int);
  20. void insertHashTable(HashTable, DLL*);
  21. int fhash(char*, int);
  22. void printList(int pos, DLL**);
  23. void main()
  24. {
  25.     FILE* pFile = fopen("Sursa.txt", "r");
  26.  
  27.     HashTable hashTable;
  28.     //init hashTable
  29.     initHashTable(&hashTable, 31);
  30.  
  31.     if (pFile)
  32.     {
  33.         char buffer[100]; float salary;
  34.         fscanf(pFile, "%s", buffer);
  35.         while (!feof(pFile))
  36.         {
  37.             fscanf(pFile, "%f", &salary);
  38.             //1. create an element of type List*
  39.             DLL* newElement = createElement(buffer, salary);
  40.             //2. insert the newly created element
  41.             insertHashTable(hashTable, newElement);
  42.             fscanf(pFile, "%s", buffer);
  43.         }
  44.         fclose(pFile);
  45.     }
  46.  
  47.     for (int i = 0; i < hashTable.size; i++)
  48.     {
  49.         printList(i, hashTable.accessTable);
  50.     }
  51. }
  52.  
  53. void printList(int pos, DLL** accessTable)
  54. {
  55.     if (accessTable[pos] == NULL)
  56.     {
  57.         printf("%d: NULL\n",pos);
  58.     }
  59.     else
  60.     {
  61.         DLL* temp = accessTable[pos];
  62.         printf("%d: ", pos);
  63.         while (temp)
  64.         {
  65.             printf("%f; ", temp->salary);
  66.             temp = temp->pNext;
  67.         }
  68.         printf("\n");
  69.     }
  70. }
  71. int fhash(char* key, int size)
  72. {
  73.     int suma = 0;
  74.     for (int i = 0; i < strlen(key); i++)
  75.         suma += key[i];
  76.     return suma % size;
  77. }
  78. void insertHashTable(HashTable hashTable, DLL* element)
  79. {
  80.     int index = fhash(element->profession, hashTable.size);
  81.     if (hashTable.accessTable[index] != NULL)
  82.     {
  83.         DLL* tmp = hashTable.accessTable[index];
  84.         while (tmp->pNext)
  85.             tmp = tmp->pNext;
  86.         tmp->pNext = element;
  87.     }
  88.     else
  89.     {
  90.         hashTable.accessTable[index] = element;
  91.     }
  92. }
  93.  
  94. void initHashTable(HashTable* hashTable, int size)
  95. {
  96.     hashTable->size = size;
  97.     hashTable->accessTable = (DLL**)malloc(sizeof(DLL*)*size);
  98.     memset(hashTable->accessTable, NULL, sizeof(DLL*)*size);
  99. }
  100.  
  101. DLL* createElement(char* buffer, float salary)
  102. {
  103.     //1.allocate memory for the new element
  104.     DLL* newElement = (DLL*)malloc(sizeof(DLL));
  105.     //2.initialize it with the params
  106.     //(the connection attributes should remain NULL)
  107.     newElement->pNext = NULL;
  108.     newElement->salary = salary;
  109.     newElement->profession = (char*)malloc(strlen(buffer) + 1);
  110.     strcpy(newElement->profession, buffer);
  111.     //3.return the new element
  112.     return newElement;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment