joseleeph

Untitled

Nov 9th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 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. #include <stdbool.h>
  7. typedef struct dllist
  8. #define LENGTH 26
  9. {
  10. char word[26];
  11. struct dllist *next;
  12. struct dllist *prev;
  13. }
  14. dllnode;
  15.  
  16.  
  17. bool find(dllnode *head, char *string);
  18. unsigned int hash(const char *word);
  19. bool load(const char *dictionary)
  20. {
  21.  
  22.  
  23. char word[LENGTH + 1]; // an array of characters
  24. FILE *dictr = fopen(dictionary,"r"); // a pointer to a file in read mode
  25.  
  26. if (dictr == NULL) // if there's nothing there
  27. {
  28. return false;
  29. }
  30. char wordnow[LENGTH+1]; // create an array
  31.  
  32. //node *myword = NULL;
  33.  
  34. while (fscanf(dictr,"%s", word) != EOF)
  35. //scan the file stored in dictr and store the string in word
  36.  
  37. {
  38. dllnode *myword = malloc(sizeof(dllnode));
  39. if (myword == NULL)
  40. {
  41. return false;
  42. }
  43. strcpy(myword->word, word);
  44. // copy word into the string word
  45. // int the word field
  46. // of the myword node
  47. }
  48.  
  49.  
  50. // malloc?
  51. // int *dictr = malloc(3*sizeof(int));
  52.  
  53.  
  54.  
  55. //store the pointer to a space of memory
  56. //called myword at the table index of the hash value of word
  57.  
  58. // walkthrough instructions
  59. /*
  60. for each word you will
  61. create a new node that will
  62. store that word inside the hash function
  63.  
  64. */
  65.  
  66. //fscanf(dictr,"%s",word);
  67. /*
  68. scan words from the pointer to the file dict and send them as a string to word
  69. */
  70.  
  71.  
  72. return true;
  73. }
  74.  
  75. unsigned int hash(const char *word)//const indicates it can't be changed
  76. {
  77.  
  78. char c;
  79. int val = 0;
  80. //int cval;
  81. for (int i = 0; word[i] != '\0'; i++)
  82. {
  83. if (i < 3)
  84. {
  85. c = word[i];
  86. val += c;
  87. }
  88. else
  89. {
  90. continue;
  91. }
  92. }
  93. return val%26;
  94. }
  95. bool find(dllnode *head, char *string)
  96. {
  97. dllnode *trav = head;
  98. bool found = false;
  99.  
  100. for (int i = 0; trav != NULL; i++)
  101. {
  102. for (int j = 0; trav->word[j] != '\0'; j++)
  103. {
  104. if (trav->word[j] == string[j]) // this returns true if any of the characters match
  105. {
  106. found = true;
  107. //continue;?
  108. }
  109. else
  110. found = false;
  111. }
  112. trav=trav->next;
  113. }
  114. return found;
  115. }
  116.  
  117. //unsigned int size(void)
  118. unsigned int size(dllnode *innode)
  119. {
  120. dllnode *nownode = innode;
  121. int count = 0;
  122. int i = 0;
  123. while (nownode != NULL)
  124. {
  125. count++;
  126. i++;
  127. nownode = nownode->next;
  128. }
  129. return count;
  130. }
  131. // size table[1] returns 2... why not 3?
  132. //because you were askining of nownode-> is not NULL
  133.  
  134. /*
  135. return the number of words in the dictionary
  136. you might iterate over every linked list in
  137. the hashtable counting the number of
  138. nodes in the linked list
  139.  
  140. or
  141. as you are loading the hash table
  142. you could keep track of the number
  143. of words loaded so farin the linked list and return
  144. the value in the size function
  145.  
  146. */
  147. dllnode *insert(dllnode *head, char *string)
  148. {
  149. //dllnode *tmp = head;
  150.  
  151. dllnode *input = malloc(sizeof(dllnode));
  152. for (int i = 0; string[i] != '\0'; i++)
  153. {
  154. input->word[i] = string[i];
  155. }
  156.  
  157.  
  158. //input->next = head;
  159. //input->prev = NULL;
  160. head->prev = input;
  161. input->prev = NULL;
  162. input->next = head;
  163.  
  164. return input;
  165.  
  166. }
  167.  
  168. void erase(dllnode *target)
  169. {
  170. target->prev->next = target->next;
  171. target->next->prev = target->prev;
  172. free(target);
  173. }
  174.  
  175.  
  176. void destroy(dllnode *head)
  177. {
  178. dllnode *cursor = head;// create a pointer, "cursor", and point it to the same place as head
  179. //while (cursor != NULL)
  180. while (cursor->next != NULL)
  181. // the different between destroy and unload is cursor->next
  182. {
  183. dllnode *tmp = cursor;
  184. cursor = cursor->next;
  185. free(tmp);
  186. }
  187. return;
  188. }
  189.  
  190. //bool unload(void)
  191. bool unload(dllnode *nownode)
  192. {
  193. //int i = 0;
  194. dllnode *cursor = nownode;
  195. //while (cursor->next != NULL)
  196. while (cursor != NULL)
  197. {
  198. dllnode *tmp = cursor;
  199. cursor = cursor->next;
  200. free(tmp);
  201. //i++;
  202. }
  203. return false;
  204. }
  205.  
  206. int main(int argc, char *argv[])
  207. {
  208.  
  209. dllnode *fruit1 = malloc(sizeof(dllnode));
  210. dllnode *fruit2 = malloc(sizeof(dllnode));
  211. dllnode *fruit3 = malloc(sizeof(dllnode));
  212.  
  213. char *f1 = "apple";
  214. char *f2 = "avocado";
  215. char *f3 = "papaya";
  216.  
  217. char *f4 = "avocado";
  218. bool cmp = strcmp(f4, f3);
  219. //printf("comparing strings:\n");
  220. //printf("string f2 prints: %s\n", f2);
  221. //printf("string f4 prints: %s\n", f4);
  222. //printf("strcmp(f4, f2) prints: ");
  223. //printf("%d ",strcmp(f4,f2));
  224. //if(strcmp(f4, f2) == 0)
  225. //{
  226. //printf("Match!\n");
  227. //}
  228. //else
  229. //{
  230. //printf("strings don't match:( \n");
  231. //}
  232.  
  233. /*
  234. for (int i = 0; f1[i] != '\0'; i++)
  235. {
  236. fruit1->word[i] = f1[i];
  237. }
  238. */
  239. //printf("fruit1->word prints %s\n", fruit1->word);
  240. //printf("applying strcpy: \n");
  241. strcpy(fruit1->word,f1);
  242. fruit1->next = fruit2;
  243. fruit1->prev = NULL;
  244.  
  245. strcpy(fruit2->word,f2);
  246. fruit2->next = fruit3;
  247. fruit2->prev = fruit1;
  248.  
  249. strcpy(fruit3->word, f3);
  250. fruit3->next = NULL;
  251. fruit3->prev = fruit2;
  252.  
  253.  
  254.  
  255. dllnode *band1 = malloc(sizeof(dllnode));
  256. dllnode *band2 = malloc(sizeof(dllnode));
  257. dllnode *band3 = malloc(sizeof(dllnode));
  258.  
  259. char *b2 = "Nirvana"; // points to mudhoney next
  260. char *b3 = "Mudhoney";
  261.  
  262.  
  263. strcpy(band1->word,"Soundgarden"/*b1*/);
  264. band1->next = band2;
  265. band1->prev = NULL;
  266.  
  267. //printf("now band1->word prints %s\n",band1->word);
  268.  
  269.  
  270. //printf("band2->word prints %s\n", band2->word);
  271. //printf("applying strcpy: \n");
  272. strcpy(band2->word,b2);
  273. band2->next = band3;
  274. band2->prev = band1;
  275.  
  276. //printf("band3->word prints %s\n", band3->word);
  277. //printf("applying strcpy: \n");
  278. strcpy(band3->word,b3);
  279. band3->next = NULL;
  280. band3->prev = band2;
  281.  
  282. //printf("band1->word prints %s\n", band1->word);
  283. //printf("band1->next->word prints %s\n", band1->next->word);
  284.  
  285. dllnode *prog1 = malloc(sizeof(dllnode));
  286. dllnode *prog2 = malloc(sizeof(dllnode));
  287. dllnode *prog3 = malloc(sizeof(dllnode));
  288.  
  289. char *p1 = "Photoshop";
  290. char *p2 = "Maya";
  291. char *p3 = "Zbrush";
  292.  
  293. strcpy(prog1->word, p1);
  294. prog1->next = prog2;
  295. prog1->prev = NULL;
  296.  
  297. strcpy(prog2->word, p2);
  298. prog2->next = prog3;
  299. prog2->prev = prog1;
  300.  
  301. strcpy(prog3->word, p3);
  302. prog3->next = NULL;
  303. prog3->prev = prog2;
  304.  
  305. //for the speller assignment you will want to have N = 26*3 compartments
  306. dllnode *table[3];
  307. table[0] = fruit1;
  308. table[1] = band1;
  309. table[2] = prog1;
  310.  
  311.  
  312.  
  313.  
  314. printf("all the words each each node of table before unload print: \n");
  315. for (int i = 0; i < 3; i++)
  316. {
  317. dllnode *travel = table[i];
  318. while (travel != NULL)
  319. //while (travel->next != NULL)
  320. {
  321. printf("%s ",travel->word);
  322. travel = travel->next;
  323. }
  324.  
  325. //printf("all the words each each node of table print: ");
  326. }
  327. printf("\n\n");
  328.  
  329.  
  330. printf("all the words in table after unload: \n");
  331. printf("unloading table:\n");
  332.  
  333. for (int i = 0; i < 3; i++)
  334. {
  335. unload(table[i]);
  336. }
  337.  
  338. printf("printing table after unload\n");
  339. for (int i = 0; i < 3; i++)
  340. {
  341. dllnode *travel = table[i];
  342. while (travel != NULL)
  343. {
  344. printf("%s ",travel->word);
  345. travel = travel->next;
  346. }
  347. }
  348. printf("\n");
  349.  
  350. printf("testing the load function to load a dictionary into word: \n");
  351.  
  352. FILE *dictr = fopen("large","r");
  353.  
  354. char rletter;
  355. int i = 0;
  356. while ((rletter = fgetc(dictr)) != EOF)
  357. {
  358.  
  359. char *rword = NULL;
  360.  
  361. while (rletter != '\0')
  362. {
  363. rword[i] = rletter;
  364. i++;
  365. }
  366.  
  367. printf("rword now prints: \n");
  368. printf("%s\n",rword);
  369.  
  370. printf("\n");
  371. }
  372.  
  373. fclose(dictr);
  374.  
  375. printf("\n");
  376. free(prog1);
  377. free(prog2);
  378. free(prog3);
  379. free(band1);
  380. free(band2);
  381. free(band3);
  382. free(fruit1);
  383. free(fruit2);
  384. free(fruit3);
  385. //free(input);
  386. }
  387.  
Advertisement
Add Comment
Please, Sign In to add comment