Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. //-----------------------------------------------------------------------------
  2. // Dictionary.c
  3. // Michelle Pham
  4. // mpham14
  5. // 12/05/19
  6. // CSE 15
  7. // .c file for the Dictionary ADT
  8. //-----------------------------------------------------------------------------
  9. #include<stdlib.h>
  10. #include<stdio.h>
  11. #include<assert.h>
  12. #include<string.h>
  13. #include"Dictionary.h"
  14.  
  15. // Private Types and Functions ------------------------------------------------
  16.  
  17. // NodeObj type
  18. typedef struct NodeObj{
  19. char* key;
  20. char* value;
  21. struct NodeObj* next;
  22. } NodeObj;
  23.  
  24. // Node type
  25. typedef NodeObj* Node;
  26.  
  27. // DictrionaryObj type
  28. typedef struct DictionaryObj{
  29. Node* table; // array of linked lists
  30. int numPairs; // number of pairs in this Dictionary
  31. } DictionaryObj;
  32.  
  33. // newNode()
  34. // constructor for the Node type
  35. Node newNode(char* k, char* v) {
  36. Node N = malloc(sizeof(NodeObj));
  37. N->key = k;
  38. N->value = v;
  39. N->next = NULL;
  40. return N;
  41. }
  42.  
  43. // freeNode()
  44. // destructor for the Node type
  45. void freeNode(Node* pN){
  46. if( pN!=NULL && *pN!=NULL ){
  47. free(*pN);
  48. *pN = NULL;
  49. }
  50. }
  51.  
  52. // freeAllNodes()
  53. // uses recursion to free all the Nodes in list headed by H
  54. void freeAllNodes(Node H){
  55. if( H!=NULL ){
  56. freeAllNodes(H->next);
  57. freeNode(&H);
  58. }
  59. }
  60.  
  61. // countChars()
  62. // Returns the number of characters in the (key, value) pairs.
  63. int countChars(Node D){
  64. int count = 0;
  65. if( D!=NULL ){
  66. count += countChars(D->next);
  67. count += strlen(D->key)+strlen(D->value)+2;
  68. }
  69. return count;
  70. }
  71.  
  72. const int tableSize=101; // or some prime other than 101
  73.  
  74. // rotate_left()
  75. // rotate the bits in an unsigned int
  76. unsigned int rotate_left(unsigned int value, int shift) {
  77. int sizeInBits = 8*sizeof(unsigned int);
  78. shift = shift & (sizeInBits - 1);
  79. if ( shift == 0 )
  80. return value;
  81. return (value << shift) | (value >> (sizeInBits - shift));
  82. }
  83.  
  84. // pre_hash()
  85. // turn a string into an unsigned int
  86. unsigned int pre_hash(char* input) {
  87. unsigned int result = 0xBAE86554;
  88. while (*input) {
  89. result ^= *input++;
  90. result = rotate_left(result, 5);
  91. }
  92. return result;
  93. }
  94.  
  95. // hash()
  96. // turns a string into an int in the range 0 to tableSize-1
  97. int hash(char* key){
  98. return pre_hash(key)%tableSize;
  99. }
  100.  
  101. // findKey()
  102. // Returns the Node containing key k, or returns NULL if no such Node exists
  103. Node findKey(Dictionary D, char* k) {
  104. int i = hash(k);
  105. Node N = D->table[i];
  106. while(N!=NULL){
  107. if(strcmp(N->key, k) == 0){
  108. return N;
  109. }
  110. N = N->next;
  111. }
  112. return NULL;
  113. }
  114.  
  115. // Constructors-Destructors ---------------------------------------------------
  116.  
  117. // newDictionary()
  118. // Constructor for the Dictionary ADT
  119. Dictionary newDictionary(){
  120. Dictionary D = malloc(sizeof(DictionaryObj));
  121. D->table = calloc(tableSize, sizeof(Node));
  122. D->numPairs = 0;
  123.  
  124. return D;
  125. }
  126.  
  127. // freeDictionary()
  128. // Destructor for the Dictionary ADT.
  129. void freeDictionary(Dictionary* pD){
  130. if( pD!=NULL && *pD!=NULL ){
  131. makeEmpty(*pD);
  132. free((*pD)->table);
  133. free(*pD);
  134. *pD = NULL;
  135. }
  136. }
  137.  
  138. // ADT operations -------------------------------------------------------------
  139.  
  140. // size()
  141. // Return the number of (key, value) pairs in Dictionary D.
  142. int size(Dictionary D){
  143. if(D==NULL) {
  144. fprintf(stderr, "Dictionary Error: size() called on NULL Dictionary reference\n");
  145. exit(EXIT_FAILURE);
  146. }
  147. return D->numPairs;
  148. }
  149.  
  150. // lookup()
  151. // If D contains a pair whose key matches argument k, then return the
  152. // corresponding value, otherwise return NULL.
  153. char* lookup(Dictionary D, char* k) {
  154. Node N;
  155. if(D==NULL) {
  156. fprintf(stderr, "Dictionary Error: lookup() called on NULL Dictionary reference\n");
  157. exit(EXIT_FAILURE);
  158. }
  159.  
  160. N = findKey(D, k);
  161. if(N!=NULL) {
  162. return N->value;
  163. }
  164. else {
  165. return NULL;
  166. }
  167. }
  168.  
  169. // insert()
  170. // Insert the pair (k,v) into D.
  171. // Pre: lookup(D, k)==NULL (D does not contain a pair whose first member is k.)
  172. void insert(Dictionary D, char* k, char* v){
  173. Node N;
  174. if( D==NULL ){
  175. fprintf(stderr, "Dictionary Error: calling insert() on NULL Dictionary reference\n");
  176. exit(EXIT_FAILURE);
  177. }
  178.  
  179. if( findKey(D, k)!=NULL ){
  180. fprintf(stderr, "Dictionary Error: cannot insert() duplicate key: \"%s\"\n", k);
  181. exit(EXIT_FAILURE);
  182. }
  183.  
  184. int i = hash(k);
  185.  
  186. N = newNode(k, v);
  187. N->next = D->table[i];
  188. D->table[i] = N;
  189.  
  190. (D->numPairs)++;
  191. }
  192.  
  193. // delete()
  194. // Remove pair whose first member is the argument k from D.
  195. // Pre: lookup(D,k)!=NULL (D contains a pair whose first member is k.)
  196. void delete(Dictionary D, char* k){
  197. Node N, P, S;
  198. //int x;
  199. if( D==NULL ){
  200. fprintf(stderr, "Dictionary Error: calling delete() on NULL Dictionary reference\n");
  201. exit(EXIT_FAILURE);
  202. }
  203.  
  204. int i = hash(k);
  205. N = findKey(D, k);
  206.  
  207. if( N==NULL ){
  208. fprintf(stderr, "Dictionary Error: cannot delete() non-existent key: \"%s\"\n", k);
  209. exit(EXIT_FAILURE);
  210. }
  211.  
  212. if(D->table[i] == N){ // Node is at the beginning
  213. S = D->table[i];
  214. D->table[i] = D->table[i]->next;
  215. }
  216. else{ // Node is anywhere else
  217. P = D->table[i];
  218. while(P->next != N){
  219. P = P->next;
  220. }
  221. S = P->next;
  222. P->next = S->next;
  223. }
  224. S->next = NULL;
  225. freeNode(&S);
  226. (D->numPairs)--;
  227. }
  228.  
  229. // makeEmpty()
  230. // Reset D to the empty state, the empty set of pairs.
  231. void makeEmpty(Dictionary D){
  232. if(D==NULL) {
  233. fprintf(stderr, "Dictionary Error: calling makeEmpty() on NULL Dictionary reference\n");
  234. exit(EXIT_FAILURE);
  235. }
  236. for(int k = 0; k < tableSize; k++){
  237. freeAllNodes(D->table[k]);
  238. D->table[k] = NULL;
  239. }
  240. D->numPairs = 0;
  241. }
  242.  
  243. // Other Operations -----------------------------------------------------------
  244.  
  245. // DictionaryToString()
  246. // Determines a text representation of the current state of Dictionary D. Each
  247. // (key, value) pair is represented as the chars in key, followed by a space,
  248. // followed by the chars in value, followed by a newline '\n' char. The pairs
  249. // occur in alphabetical order by key. The function returns a pointer to a char
  250. // array, allocated from heap memory, containing the text representation
  251. // described above, followed by a terminating null '\0' char. It is the
  252. // responsibility of the calling function to free this memory.
  253. char* DictionaryToString(Dictionary D){
  254. if( D==NULL ){
  255. fprintf(stderr,
  256. "Dictionary Error: calling DictionaryToString() on NULL Dictionary "\
  257. "reference\n");
  258. exit(EXIT_FAILURE);
  259. }
  260.  
  261. char* str;
  262. int n = 0;
  263. Node N;
  264.  
  265. for(int k = 0; k < tableSize; k++) {
  266. if(D->table[k]!=NULL){
  267. n += countChars(D->table[k]);
  268. }
  269. }
  270.  
  271. str = calloc(n+1, sizeof(char));
  272.  
  273. for(int k = 0; k < tableSize; k++) {
  274. if(D->table[k]!=NULL){
  275. N = D->table[k];
  276. while(N != NULL){
  277. strcat(str, N->key);
  278. strcat(str, " ");
  279. strcat(str, N->value);
  280. strcat(str, "\n");
  281. N = N->next;
  282. }
  283. }
  284. }
  285. str[n] = '\0';
  286. return str;
  287.  
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement