joseleeph

Untitled

Nov 5th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <cs50.h>
  5. #include <string.h>
  6.  
  7. typedef struct dllist
  8. {
  9. char word[26];
  10. struct dllist *next;
  11. struct dllist *prev;
  12. }
  13. dllnode;
  14.  
  15.  
  16. bool find(dllnode *head, char *string);
  17.  
  18. unsigned int hash(const char *word)
  19. {
  20.  
  21. char c;
  22. int val = 0;
  23. //int cval;
  24. for (int i = 0; word[i] != '\0'; i++)
  25. {
  26. if (i < 3)
  27. {
  28. c = word[i];
  29. val += c;
  30. }
  31. else
  32. {
  33. continue;
  34. }
  35. }
  36. return val%26;
  37. }
  38. bool find(dllnode *head, char *string)
  39. {
  40. dllnode *trav = head;
  41. bool found = false;
  42.  
  43. for (int i = 0; trav != NULL; i++)
  44. {
  45. for (int j = 0; trav->word[j] != '\0'; j++)
  46. {
  47. if (trav->word[j] == string[j]) // this returns true if any of the characters match
  48. {
  49. found = true;
  50. //continue;?
  51. }
  52. else
  53. found = false;
  54. }
  55. trav=trav->next;
  56. }
  57. return found;
  58. }
  59.  
  60. //unsigned int size(void)
  61. unsigned int size(dllnode *innode)
  62. {
  63. dllnode *nownode = innode;
  64. int count = 0;
  65. int i = 0;
  66. while (nownode->next != NULL)
  67. {
  68. count++;
  69. i++;
  70. nownode = nownode->next;
  71. }
  72. return count;
  73. }
  74. // size table[1] returns 2... why not 3?
  75.  
  76. /*
  77. return the number of words in the dictionary
  78. you might iterate over every linked list in
  79. the hashtable counting the number of
  80. nodes in the linked list
  81.  
  82. or
  83. as you are loading the hash table
  84. you could keep track of the number
  85. of words loaded so farin the linked list and return
  86. the value in the size function
  87.  
  88. */
  89. dllnode *insert(dllnode *head, char *string)
  90. {
  91. //dllnode *tmp = head;
  92.  
  93. dllnode *input = malloc(sizeof(dllnode));
  94. for (int i = 0; string[i] != '\0'; i++)
  95. {
  96. input->word[i] = string[i];
  97. }
  98.  
  99.  
  100. //input->next = head;
  101. //input->prev = NULL;
  102. head->prev = input;
  103. input->prev = NULL;
  104. input->next = head;
  105.  
  106. return input;
  107.  
  108. }
  109.  
  110. void erase(dllnode *target)
  111. {
  112. target->prev->next = target->next;
  113. target->next->prev = target->prev;
  114. free(target);
  115. }
  116.  
  117.  
  118. void destroy(dllnode *head)
  119. {
  120. dllnode *cursor = head;// create a pointer, "cursor", and point it to the same place as head
  121. //while (cursor != NULL)
  122. while (cursor->next != NULL)
  123. // the different between destroy and unload is cursor->next
  124. {
  125. dllnode *tmp = cursor;
  126. cursor = cursor->next;
  127. free(tmp);
  128. }
  129. return;
  130. }
  131.  
  132. //bool unload(void)
  133. bool unload(dllnode *nownode)
  134. {
  135. //int i = 0;
  136. dllnode *cursor = nownode;
  137. //while (cursor->next != NULL)
  138. while (cursor != NULL)
  139. {
  140. dllnode *tmp = cursor;
  141. cursor = cursor->next;
  142. free(tmp);
  143. //i++;
  144. }
  145. return true;
  146. }
  147.  
  148. int main(int argc, char *argv[])
  149. {
  150.  
  151. dllnode *fruit1 = malloc(sizeof(dllnode));
  152. dllnode *fruit2 = malloc(sizeof(dllnode));
  153. dllnode *fruit3 = malloc(sizeof(dllnode));
  154.  
  155. char *f1 = "apple";
  156. char *f2 = "avocado";
  157. char *f3 = "papaya";
  158.  
  159. char *f4 = "avocado";
  160. bool cmp = strcmp(f4, f3);
  161. //printf("comparing strings:\n");
  162. //printf("string f2 prints: %s\n", f2);
  163. //printf("string f4 prints: %s\n", f4);
  164. //printf("strcmp(f4, f2) prints: ");
  165. //printf("%d ",strcmp(f4,f2));
  166. //if(strcmp(f4, f2) == 0)
  167. //{
  168. //printf("Match!\n");
  169. //}
  170. //else
  171. //{
  172. //printf("strings don't match:( \n");
  173. //}
  174.  
  175. /*
  176. for (int i = 0; f1[i] != '\0'; i++)
  177. {
  178. fruit1->word[i] = f1[i];
  179. }
  180. */
  181. //printf("fruit1->word prints %s\n", fruit1->word);
  182. //printf("applying strcpy: \n");
  183. strcpy(fruit1->word,f1);
  184. fruit1->next = fruit2;
  185. fruit1->prev = NULL;
  186.  
  187. strcpy(fruit2->word,f2);
  188. fruit2->next = fruit3;
  189. fruit2->prev = fruit1;
  190.  
  191. strcpy(fruit3->word, f3);
  192. fruit3->next = NULL;
  193. fruit3->prev = fruit2;
  194.  
  195.  
  196.  
  197. dllnode *band1 = malloc(sizeof(dllnode));
  198. dllnode *band2 = malloc(sizeof(dllnode));
  199. dllnode *band3 = malloc(sizeof(dllnode));
  200.  
  201. char *b2 = "Nirvana"; // points to mudhoney next
  202. char *b3 = "Mudhoney";
  203.  
  204.  
  205. strcpy(band1->word,"Soundgarden"/*b1*/);
  206. band1->next = band2;
  207. band1->prev = NULL;
  208.  
  209. //printf("now band1->word prints %s\n",band1->word);
  210.  
  211.  
  212. //printf("band2->word prints %s\n", band2->word);
  213. //printf("applying strcpy: \n");
  214. strcpy(band2->word,b2);
  215. band2->next = band3;
  216. band2->prev = band1;
  217.  
  218. //printf("band3->word prints %s\n", band3->word);
  219. //printf("applying strcpy: \n");
  220. strcpy(band3->word,b3);
  221. band3->next = NULL;
  222. band3->prev = band2;
  223.  
  224. //printf("band1->word prints %s\n", band1->word);
  225. //printf("band1->next->word prints %s\n", band1->next->word);
  226.  
  227. dllnode *prog1 = malloc(sizeof(dllnode));
  228. dllnode *prog2 = malloc(sizeof(dllnode));
  229. dllnode *prog3 = malloc(sizeof(dllnode));
  230.  
  231. char *p1 = "Photoshop";
  232. char *p2 = "Maya";
  233. char *p3 = "Zbrush";
  234.  
  235. strcpy(prog1->word, p1);
  236. prog1->next = prog2;
  237. prog1->prev = NULL;
  238.  
  239. strcpy(prog2->word, p2);
  240. prog2->next = prog3;
  241. prog2->prev = prog1;
  242.  
  243. strcpy(prog3->word, p3);
  244. prog3->next = NULL;
  245. prog3->prev = prog2;
  246.  
  247. dllnode *table[3];
  248. table[0] = fruit1;
  249. table[1] = band1;
  250. table[2] = prog1;
  251.  
  252.  
  253. printf("all the words each each node of table before unload print: \n");
  254. for (int i = 0; i < 3; i++)
  255. {
  256. dllnode *travel = table[i];
  257. while (travel != NULL)
  258. //while (travel->next != NULL)
  259. {
  260. printf("%s ",travel->word);
  261. travel = travel->next;
  262. }
  263.  
  264. //printf("all the words each each node of table print: ");
  265. }
  266. printf("\n\n");
  267.  
  268.  
  269. printf("all the words in table after unload: \n");
  270. printf("unloading table:\n");
  271.  
  272. for (int i = 0; i < 3; i++)
  273. {
  274. unload(table[i]);
  275. }
  276.  
  277. printf("printing table after unload\n");
  278. for (int i = 0; i < 3; i++)
  279. {
  280. dllnode *travel = table[i];
  281. while (travel != NULL)
  282. {
  283. printf("%s ",travel->word);
  284. travel = travel->next;
  285. }
  286. }
  287. printf("\n");
  288. printf("the size of the doubly linked list at table[1] is: \n");
  289. printf("%i\n",size(table[1]));
  290.  
  291. //printf("\n");
  292. free(prog1);
  293. free(prog2);
  294. free(prog3);
  295. free(band1);
  296. free(band2);
  297. free(band3);
  298. free(fruit1);
  299. free(fruit2);
  300. free(fruit3);
  301. //free(input);
  302. }
  303.  
Advertisement
Add Comment
Please, Sign In to add comment