Advertisement
Void-voiD

Untitled

Dec 31st, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct OneDirection{
  6. struct OneDirection *next;
  7. int key , value;
  8. };
  9.  
  10. int At(int index ,struct OneDirection *HashTable){
  11. struct OneDirection *list = HashTable; //передаем таблицу с нужным ключом
  12. while(list != NULL){
  13. if(list->key == index)
  14. return list->value;
  15. else
  16. list = list->next;
  17. }
  18. return 0;
  19. }
  20.  
  21. struct OneDirection *Assign(int index ,int value , struct OneDirection *HashTable){
  22. struct OneDirection *list = (struct OneDirection*)malloc(sizeof(struct OneDirection));
  23. list->next = HashTable;
  24. list->key = index;
  25. list->value = value;
  26. HashTable = list;
  27. return HashTable;
  28. }
  29.  
  30. void freeList(struct OneDirection *list){
  31. struct OneDirection *t;
  32. while(list != NULL){ //ФУНКЦИЯ ОЧИСТКИ СПИСКОВ
  33. t = list;
  34. list = list->next;
  35. free(t);
  36. }
  37. }
  38.  
  39. int main(){
  40. int count , size;
  41. scanf("%d\n%d" , &count , &size);
  42. struct OneDirection **HashTable = (struct OneDirection**)malloc(size * sizeof(struct OneDirection*));
  43. for (int i = 0; i < size; i++) HashTable[i] = NULL;
  44. for(int i = 0 ; i < count ; i++){ //Можно задавать массив разной длины, в зависимости от размера
  45. char word[10]; //сделал сразу большой
  46. scanf("%s" , word);
  47. if(word[1] == 'T'){
  48. int index;
  49. scanf("%d" , &index);
  50. int key = index % size;
  51. printf("%d\n" , At(index ,HashTable[key])); //передаем таблицу с нужным ключом
  52. }
  53. else{
  54. int value , index;
  55. scanf("%d %d" , &index , &value);
  56. int key = index % size;
  57. HashTable[key] = Assign(index , value , HashTable[key]);
  58. }
  59. }
  60. for(int i = 0 ; i < size ; i++)
  61. freeList(HashTable[i]);
  62. free(HashTable); //ОЧИСТКА ХЭШ-ТАБЛИЦЫ
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement