joseleeph

Untitled

Nov 3rd, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.23 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. // this will take more bytes of memory
  16. // than a singly linked list
  17. // don't use if you don't want to delete elements
  18.  
  19. // why can't you call it dllnode initially?
  20.  
  21.  
  22. /*
  23. singly linked lists are easy to add to, but hard to remove from
  24. doubly linked lists resolve this issue
  25.  
  26. singly linked lists exted the ability to collect and organize data,
  27. however they can only ever move in one direction through the list
  28. this makes it difficult to delete a node
  29.  
  30. a doubly linked list allows moving forward and backward
  31. through the list, all by adding one extra pointer to our
  32. struct definition
  33. */
  34.  
  35. bool find(dllnode *head, char *string);
  36.  
  37.  
  38. unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
  39. {
  40.  
  41. char c;
  42. int val = 0;
  43. //int cval;
  44. for (int i = 0; word[i] != '\0'; i++)
  45. {
  46. c = word[i];
  47. val += c;
  48. /*
  49. if (isalpha(c))
  50. {
  51. if (isupper(c))
  52. {
  53. //c = word[i]
  54. val += c - 65;
  55. }
  56. if (islower(c))
  57. {
  58. val += c - 97;
  59. }
  60. if (c == 13)
  61. {
  62. continue;
  63. }
  64. }
  65. else
  66. continue;
  67. */
  68. }
  69. return val%26;
  70. }
  71. bool find(dllnode *head, char *string)
  72. {
  73. dllnode *trav = head;
  74. bool found = false;
  75.  
  76. for (int i = 0; trav != NULL; i++)
  77. {
  78. for (int j = 0; trav->word[j] != '\0'; j++)
  79. {
  80. if (trav->word[j] == string[j]) // this returns true if any of the characters match
  81. {
  82. found = true;
  83. //continue;?
  84. }
  85. else
  86. found = false;
  87. }
  88. trav=trav->next;
  89. }
  90. return found;
  91. }
  92.  
  93. dllnode *insert(dllnode *head, char *string)
  94. {
  95. //dllnode *tmp = head;
  96.  
  97. dllnode *input = malloc(sizeof(dllnode));
  98. for (int i = 0; string[i] != '\0'; i++)
  99. {
  100. input->word[i] = string[i];
  101. }
  102.  
  103.  
  104. //input->next = head;
  105. //input->prev = NULL;
  106. head->prev = input;
  107. input->prev = NULL;
  108. input->next = head;
  109.  
  110. return input;
  111.  
  112. }
  113. void erase(dllnode *target)
  114. {
  115. target->prev->next = target->next;
  116. target->next->prev = target->prev;
  117. free(target);
  118. }
  119.  
  120.  
  121. void destroy(dllnode *head)
  122. {
  123. dllnode *elim = head;
  124. while (elim != NULL)
  125. {
  126. if (elim->next == NULL)
  127. {
  128. elim = NULL;
  129. elim = elim->next;
  130. }
  131. }
  132. return;
  133. /*
  134. dllnode *eliminate = head;
  135. while (eliminate != NULL)
  136. {
  137. eliminate = NULL;
  138. eliminate = eliminate->next;
  139. //eliminate = NULL;
  140. free(eliminate);
  141. }
  142. */
  143. }
  144.  
  145. int main(int argc, char *argv[])
  146. {
  147.  
  148. dllnode *fruit1 = malloc(sizeof(dllnode));
  149. dllnode *fruit2 = malloc(sizeof(dllnode));
  150. dllnode *fruit3 = malloc(sizeof(dllnode));
  151.  
  152. char *f1 = "apple";
  153. char *f2 = "avocado";
  154. char *f3 = "papaya";
  155. /*
  156. for (int i = 0; f1[i] != '\0'; i++)
  157. {
  158. fruit1->word[i] = f1[i];
  159. }
  160. */
  161. printf("fruit1->word prints %s\n", fruit1->word);
  162. printf("applying strcpy: \n");
  163. strcpy(fruit1->word,f1);
  164. fruit1->next = fruit2;
  165. fruit1->prev = NULL;
  166.  
  167. strcpy(fruit2->word,f2);
  168. fruit2->next = fruit3;
  169. fruit2->prev = fruit1;
  170.  
  171. strcpy(fruit3->word, f3);
  172. fruit3->next = NULL;
  173. fruit3->prev = fruit2;
  174.  
  175. printf("\n\nnow printing linked list of fruits: \n\n");
  176. printf("fruit1->word prints: %s\n",fruit1->word);
  177. printf("fruit1->next->word prints: %s\n",fruit1->next->word);
  178. printf("fruit1->prev->word prints: %s\n",fruit1->prev->word);
  179. printf("fruit2->word prints: %s\n",fruit2->word);
  180. printf("fruit2->next->word prints: %s\n",fruit2->next->word);
  181. printf("fruit2->prev->word prints: %s\n",fruit2->prev->word);
  182. printf("fruit3->next->word prints: %s\n\n",fruit3->next->word);
  183. printf("fruit3->word prints: %s\n\n",fruit3->word);
  184. printf("fruit3->prev->word prints: %s\n\n",fruit3->prev->word);
  185.  
  186. dllnode *band1 = malloc(sizeof(dllnode));
  187. dllnode *band2 = malloc(sizeof(dllnode));
  188. dllnode *band3 = malloc(sizeof(dllnode));
  189.  
  190. char *b2 = "Nirvana"; // points to mudhoney next
  191. char *b3 = "Mudhoney";
  192.  
  193.  
  194. printf("strcpytest: \n");
  195. printf("band1->word prints %s\n",band1->word);
  196. printf("applying strcpy: \n");
  197.  
  198. strcpy(band1->word,"Soundgarden"/*b1*/);
  199. band1->next = band2;
  200. band1->prev = NULL;
  201.  
  202. printf("now band1->word prints %s\n",band1->word);
  203.  
  204.  
  205. printf("band2->word prints %s\n", band2->word);
  206. printf("applying strcpy: \n");
  207. strcpy(band2->word,b2);
  208. band2->next = band3;
  209. band2->prev = band1;
  210.  
  211. printf("band3->word prints %s\n", band3->word);
  212. printf("applying strcpy: \n");
  213. strcpy(band3->word,b3);
  214. band3->next = NULL;
  215. band3->prev = band2;
  216.  
  217. printf("band1->word prints %s\n", band1->word);
  218. printf("band1->next->word prints %s\n", band1->next->word);
  219.  
  220. dllnode *prog1 = malloc(sizeof(dllnode));
  221. dllnode *prog2 = malloc(sizeof(dllnode));
  222. dllnode *prog3 = malloc(sizeof(dllnode));
  223.  
  224. char *p1 = "Photoshop";
  225. char *p2 = "Maya";
  226. char *p3 = "Zbrush";
  227.  
  228. strcpy(prog1->word, p1);
  229. prog1->next = prog2;
  230. prog1->prev = NULL;
  231.  
  232. strcpy(prog2->word, p2);
  233. prog2->next = prog3;
  234. prog2->prev = prog1;
  235.  
  236. strcpy(prog3->word, p3);
  237. prog3->next = NULL;
  238. prog3->prev = prog2;
  239.  
  240. dllnode *table[3];
  241. table[0] = fruit1;
  242. table[1] = band1;
  243. table[2] = prog1;
  244.  
  245. for (int i = 0; i < 3; i++)
  246. {
  247. printf("table[%i]->word prints: %s\n",i,table[i]->word);
  248. printf("table[%i]->next->word prints: %s\n",i,table[i]->next->word);
  249. printf("table[%i]->next->next->word prints: %s\n",i,table[i]->next->next->word);
  250.  
  251. }
  252.  
  253. for (int i = 0; i < 3; i++)
  254. {
  255. for (int j = 0; table[i]->word[j] != '\0'; j++)
  256. {
  257.  
  258. if (table[i]->word[j] == '\0')
  259. {
  260. printf("%c \n",table[i]->word[j]);
  261. }
  262. else
  263. {
  264. printf("%c \n", table[i]->word[j]);
  265. }
  266. }
  267. }
  268.  
  269. printf("\n\n");
  270.  
  271. printf("cursor word loop forwards\n");
  272. for (int i = 0; i < 3; i++) // how do i know how big table is?
  273. {
  274. dllnode *cursor = table[i];
  275. for (int j = 0; cursor != NULL; j++)
  276. {
  277. printf("%s ", cursor->word);
  278. cursor = cursor->next;
  279. }
  280. }
  281. printf("\n\n");
  282.  
  283. printf("cursor word loop backwards\n");
  284. for (int i = 2; i >= 0; i--) // how do i know how big table is?
  285. {
  286. dllnode *cursor = table[i]->next->next;
  287. for (int j = 0; cursor != NULL; j++)
  288. {
  289. printf("%s ", cursor->word);
  290. cursor = cursor->prev;
  291. }
  292. }
  293. printf("\n\n");
  294. printf("testing the find function");
  295.  
  296. bool resultm = find(table[2],"Maya");
  297. printf("now looking for a word with the find() function\n");
  298. printf("does the word maya appear in the table[2]?\n");
  299. printf("looking for the word Maya in table[2]\n");
  300. printf("find(table[2],resultm) prints: \n");
  301. printf("%d\n",resultm);
  302. printf("resultm prints:\n");
  303. if (resultm)
  304. {
  305. printf("Found!\n\n");
  306. }
  307. else
  308. printf("Not Found :(\n\n");
  309.  
  310. printf("searching table[1] for papaya\n");
  311. bool resultp = find(table[0], "papaya");
  312. if (resultp)
  313. {
  314. printf("papaya found!\n");
  315. }
  316. else
  317. printf("papaya not found :(");
  318.  
  319. bool results = find(table[1], "stairs");
  320. if (results)
  321. {
  322. printf("stairs found!\n");
  323. }
  324. else
  325. printf("stairs not found :(\n");
  326.  
  327. printf("the words in table[1] are:\n\n");
  328.  
  329. dllnode *cursor = table[1];
  330. for (int j = 0; cursor != NULL; j++)
  331. {
  332. printf("%s ", cursor->word);
  333. cursor = cursor->next;
  334. }
  335. printf("\n");
  336.  
  337. printf("does the word boobs appear in table[1]?\n");
  338. bool resultb = find(table[1], "boobs");
  339. if (resultb)
  340. {
  341. printf("boobs found!\n");
  342. }
  343. else
  344. printf("boobs not found :(\n");
  345.  
  346. printf("testing the insert function:\n");
  347.  
  348. printf("the words in table[0] before insert() are:\n\n");
  349.  
  350. dllnode *traverse = table[0];
  351. for (int j = 0; traverse != NULL; j++)
  352. {
  353. printf("%s ", traverse->word);
  354. traverse = traverse->next;
  355. }
  356. printf("\n\n");
  357.  
  358.  
  359. char *insword = "hereiam!";
  360. char *outword = table[0]->next->word;
  361.  
  362. table[0] = insert(table[0], insword);
  363.  
  364.  
  365. printf("the words in table[0] after insert() are:\n\n");
  366.  
  367. traverse = table[0];
  368. for (int j = 0; traverse != NULL; j++)
  369. {
  370. printf("%s ", traverse->word);
  371. traverse = traverse->next;
  372. }
  373. printf("\n\n");
  374.  
  375. printf("testing the erase function:\n");
  376. printf("trying to erase the node stored at table[0]->next:\n");
  377. erase(table[0]->next);
  378. printf("the words in table[0] after erase() are: \n");
  379. // delete is a key word and shouldn't be used?
  380. // prints hereiam! avocado papaya
  381. // apple has been erased
  382.  
  383.  
  384. traverse = table[0];
  385. for (int j = 0; traverse != NULL; j++)
  386. {
  387. printf("%s ", traverse->word);
  388. traverse = traverse->next;
  389. }
  390. printf("\n\n");
  391.  
  392. printf("testing the destroy() function: \n");
  393. printf("the words in table[1], before the destroy() function\n");
  394.  
  395. //travel = table[1];
  396. for (dllnode *travel = table[1];travel != NULL; travel = travel->next)
  397. {
  398. printf("%s ",travel->word);
  399. }
  400. printf("\n");
  401. printf("the words in table[1], after the destroy() function\n");
  402. destroy(table[1]);
  403. for (dllnode *travel = table[1];travel != NULL; travel = travel->next)
  404. {
  405. printf("%s ",travel->word);
  406. }
  407.  
  408. free(prog1);
  409. free(prog2);
  410. free(prog3);
  411. free(band1);
  412. free(band2);
  413. free(band3);
  414. free(fruit1);
  415. free(fruit2);
  416. free(fruit3);
  417. //free(input);
  418. }
  419.  
Advertisement
Add Comment
Please, Sign In to add comment