Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "Dictionary.h"
  4.  
  5. typedef struct NodeObj{
  6. char* key;
  7. char* value;
  8. } NodeObj;
  9.  
  10. typedef NodeObj* Node;
  11.  
  12. Node newNode(char* k, char* v){
  13. Node N = malloc(sizeof(NodeObj));
  14. N->key = k;
  15. N->value = v;
  16. return N;
  17. }
  18.  
  19. void freeNode(Node* pN){
  20. if(pN != NULL && *pN != NULL){
  21. free(*pN);
  22. *pN = NULL;
  23. }
  24. }
  25.  
  26. void freeAllNodes(Node H){
  27. if(H != NULL){
  28. freeAllNodes(H->next);
  29. freeNode(&H);
  30. }
  31. }
  32.  
  33. typedef struct DictionaryObj*{
  34. Node head;
  35. int size;
  36. } DictionaryObj;
  37.  
  38. Dictionary newDictionary(){
  39. Dictionary D = malloc(sizeof(DictionaryObj));
  40. D->head = NULL;
  41. D->size = 0;
  42. }
  43.  
  44. void freeDictionary(Dictionary* pD){
  45. if(pD != NULL && *pD != NULL){
  46. makeEmpty(*pD);
  47. free(*pD);
  48. *pD = NULL;
  49. }
  50. }
  51.  
  52. Node findKey(Dictionary* D, char* k){
  53. Node N = D->head;
  54. for(int i = 0; i < D->size; i++){
  55. if(N == k){
  56. return N;
  57. }
  58. N = N->next;
  59. }
  60.  
  61. return NULL;
  62. }
  63.  
  64. int size(Dictionary D){
  65. if(D == NULL){
  66. fprintf(stderr, "Dictionary Error: size() called on NULL IntegerList reference.\n");
  67. exit(EXIT_FAILURE);
  68. }
  69.  
  70. return D->size;
  71. }
  72.  
  73. char* lookup(Dictionary D, char* k){
  74. Node N;
  75. if(D == NULL){
  76. fprintf(stderr, "Dictionary Error: lookup() called on NULL IntegerList reference.\n");
  77. exit(EXIT_FAILURE);
  78. }
  79.  
  80. N = findKey(D, k);
  81. if(N == NULL){
  82. return NULL;
  83. }
  84.  
  85. return N->value;
  86. }
  87.  
  88. void insert(Dictionary D, char* k, char* v){
  89. Node N;
  90. if(D == NULL){
  91. fprintf(stderr, "Dictionary Error: insert() called on NULL IntegerList reference.\n");
  92. exit(EXIT_FAILURE);
  93. }
  94.  
  95. if(findKey(D, k) != NULL){
  96. fprintf(stderr, "Dictionary Error: cannot insert() duplicate key: \"%s\"\n", k);
  97. exit(EXIT_FAILURE);
  98. }
  99.  
  100. N = newNode(k, v);
  101. N->next = D->head;
  102.  
  103. D->size++;
  104. }
  105.  
  106. void delete(Dictionary D, char* k){
  107. if(D == NULL){
  108. fprintf(stderr, "Dictionary Error: delete() called on NULL IntegerList reference.\n");
  109. exit(EXIT_FAILURE);
  110. }
  111.  
  112. D->size--;
  113. }
  114.  
  115. void makeEmpty(Dictionary D){
  116. if(D == NULL){
  117. fprintf(stderr, "Dictionary Error: makeEmpty() called on NULL IntegerList reference.\n");
  118. exit(EXIT_FAILURE);
  119. }
  120.  
  121. D->size = 0;
  122. }
  123.  
  124. char* DictionaryToString(Dictionary D){
  125. if(D == NULL){
  126. fprintf(stderr, "Dictionary Error: DictionaryToString() called on NULL IntegerList reference.\n");
  127. exit(EXIT_FAILURE);
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement