joseleeph

Untitled

Nov 4th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.00 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.  
  114. void erase(dllnode *target)
  115. {
  116. target->prev->next = target->next;
  117. target->next->prev = target->prev;
  118. free(target);
  119. }
  120.  
  121. /*
  122. void delpoint(dllnode *head)
  123. {
  124. dllnode *cursor = head;
  125. dllnode *tmp = cursor->next;
  126. while (cursor != NULL)
  127. {
  128. //dllnode *tmp = cursor;
  129. free(tmp)
  130. }
  131. }
  132.  
  133.  
  134. steps involved
  135. a. if you've reached a null pointer, stop
  136. b. delete the rest of the list..... how to delete? in a loop? set the value to null? empty?
  137. c. free the current node
  138.  
  139. recursion
  140. delete everyone else
  141. then delete me
  142.  
  143. in order to work with linked lists effectively, there are a number of operations to understand
  144. 1. create a linked list when it doesn't already exist
  145. 2. search through a linked list to find an element
  146. 3.insert a new node into the linked list.
  147. 4. delete a single element from a linked list
  148. this is not easy as you need to keep two pointers
  149. doubly linked lists resolve this issue
  150. 5. delete an entire linked list
  151.  
  152. */
  153.  
  154. void destroy(dllnode *head)
  155. {
  156. dllnode *cursor = head;// create a pointer, "cursor", and point it to the same place as head
  157. while (cursor != NULL)
  158. {
  159. dllnode *temp = cursor->next;// create a pointer and point it to the same place as cursor->next
  160. //free(temp);//free the memory that cursor points to
  161. // prints Soundgarden `
  162. free(cursor);
  163. //prints # `#, - `-
  164. //free the memory that cursor points to
  165. cursor = temp;// set cursor to point to the same place as temp
  166. //free(cursor);
  167. }
  168. //free(cursor);
  169. //free(head);
  170. //dllnode *elim = head;
  171.  
  172. //dllnode *cursor = elim->next;
  173. /*
  174. head points to the first node of the linked list
  175. head and elim point to the first node in the linked list
  176. they hold the address of that node, but they are not that node
  177. elim is also a local variable to the function so setting it to
  178. NULL doesn't affect the actual original head pointer that is in main
  179.  
  180. erasing the address doesn't demolish the building, you just
  181. won't know where it is. setting elim == NULL doesn't magically demolish
  182. the building. the building is still there, the address is now gone
  183.  
  184. if you want to release the memory used by a node,
  185. you have to use free to say that you don't need it anymore.
  186. because the pointer is separate from the thing it points to
  187. calling free on that pointer does not set it to NULL
  188. the pointer will still be pointing to the memory
  189. you freed, it just won't be in memory anymore
  190.  
  191. to free a linked list:
  192.  
  193. 1. Create a pointer "cursor" and point it to the same place as head
  194. 2. as long as cursor is not NULL:
  195. a. create a pointer "temp" and point it to the same place as
  196. "cursor->next"
  197. b. free the memory that "cursor" points to
  198. c. set "cursor" to point to the same place as tmp
  199. */
  200.  
  201. //while (elim->next != NULL)
  202. //while (elim != NULL)
  203. //while (cursor != NULL)
  204. //{
  205. //cursor = cursor->next;
  206. //if (cursor == NULL)
  207.  
  208. //dllnode *cursor = elim->next;
  209.  
  210. //elim = elim->next;
  211. //if (elim != NULL)
  212. //elim = elim->next;
  213.  
  214. /*
  215. if (elim->next == NULL)
  216. {
  217. elim = NULL;
  218. //elim = elim->next;
  219. }
  220. else
  221. {
  222. elim = elim->next;
  223. }
  224. */
  225. //}
  226. return;
  227. /*
  228. dllnode *eliminate = head;
  229. while (eliminate != NULL)
  230. {
  231. eliminate = NULL;
  232. eliminate = eliminate->next;
  233. //eliminate = NULL;
  234. free(eliminate);
  235. }
  236. */
  237. }
  238.  
  239. int main(int argc, char *argv[])
  240. {
  241.  
  242. dllnode *fruit1 = malloc(sizeof(dllnode));
  243. dllnode *fruit2 = malloc(sizeof(dllnode));
  244. dllnode *fruit3 = malloc(sizeof(dllnode));
  245.  
  246. char *f1 = "apple";
  247. char *f2 = "avocado";
  248. char *f3 = "papaya";
  249. /*
  250. for (int i = 0; f1[i] != '\0'; i++)
  251. {
  252. fruit1->word[i] = f1[i];
  253. }
  254. */
  255. printf("fruit1->word prints %s\n", fruit1->word);
  256. printf("applying strcpy: \n");
  257. strcpy(fruit1->word,f1);
  258. fruit1->next = fruit2;
  259. fruit1->prev = NULL;
  260.  
  261. strcpy(fruit2->word,f2);
  262. fruit2->next = fruit3;
  263. fruit2->prev = fruit1;
  264.  
  265. strcpy(fruit3->word, f3);
  266. fruit3->next = NULL;
  267. fruit3->prev = fruit2;
  268.  
  269. printf("\n\nnow printing linked list of fruits: \n\n");
  270. printf("fruit1->word prints: %s\n",fruit1->word);
  271. printf("fruit1->next->word prints: %s\n",fruit1->next->word);
  272. printf("fruit1->prev->word prints: %s\n",fruit1->prev->word);
  273. printf("fruit2->word prints: %s\n",fruit2->word);
  274. printf("fruit2->next->word prints: %s\n",fruit2->next->word);
  275. printf("fruit2->prev->word prints: %s\n",fruit2->prev->word);
  276. printf("fruit3->next->word prints: %s\n\n",fruit3->next->word);
  277. printf("fruit3->word prints: %s\n\n",fruit3->word);
  278. printf("fruit3->prev->word prints: %s\n\n",fruit3->prev->word);
  279.  
  280. dllnode *band1 = malloc(sizeof(dllnode));
  281. dllnode *band2 = malloc(sizeof(dllnode));
  282. dllnode *band3 = malloc(sizeof(dllnode));
  283.  
  284. char *b2 = "Nirvana"; // points to mudhoney next
  285. char *b3 = "Mudhoney";
  286.  
  287.  
  288. printf("strcpytest: \n");
  289. printf("band1->word prints %s\n",band1->word);
  290. printf("applying strcpy: \n");
  291.  
  292. strcpy(band1->word,"Soundgarden"/*b1*/);
  293. band1->next = band2;
  294. band1->prev = NULL;
  295.  
  296. printf("now band1->word prints %s\n",band1->word);
  297.  
  298.  
  299. printf("band2->word prints %s\n", band2->word);
  300. printf("applying strcpy: \n");
  301. strcpy(band2->word,b2);
  302. band2->next = band3;
  303. band2->prev = band1;
  304.  
  305. printf("band3->word prints %s\n", band3->word);
  306. printf("applying strcpy: \n");
  307. strcpy(band3->word,b3);
  308. band3->next = NULL;
  309. band3->prev = band2;
  310.  
  311. printf("band1->word prints %s\n", band1->word);
  312. printf("band1->next->word prints %s\n", band1->next->word);
  313.  
  314. dllnode *prog1 = malloc(sizeof(dllnode));
  315. dllnode *prog2 = malloc(sizeof(dllnode));
  316. dllnode *prog3 = malloc(sizeof(dllnode));
  317.  
  318. char *p1 = "Photoshop";
  319. char *p2 = "Maya";
  320. char *p3 = "Zbrush";
  321.  
  322. strcpy(prog1->word, p1);
  323. prog1->next = prog2;
  324. prog1->prev = NULL;
  325.  
  326. strcpy(prog2->word, p2);
  327. prog2->next = prog3;
  328. prog2->prev = prog1;
  329.  
  330. strcpy(prog3->word, p3);
  331. prog3->next = NULL;
  332. prog3->prev = prog2;
  333.  
  334. dllnode *table[3];
  335. table[0] = fruit1;
  336. table[1] = band1;
  337. table[2] = prog1;
  338.  
  339. for (int i = 0; i < 3; i++)
  340. {
  341. printf("table[%i]->word prints: %s\n",i,table[i]->word);
  342. printf("table[%i]->next->word prints: %s\n",i,table[i]->next->word);
  343. printf("table[%i]->next->next->word prints: %s\n",i,table[i]->next->next->word);
  344.  
  345. }
  346.  
  347. for (int i = 0; i < 3; i++)
  348. {
  349. for (int j = 0; table[i]->word[j] != '\0'; j++)
  350. {
  351.  
  352. if (table[i]->word[j] == '\0')
  353. {
  354. printf("%c \n",table[i]->word[j]);
  355. }
  356. else
  357. {
  358. printf("%c \n", table[i]->word[j]);
  359. }
  360. }
  361. }
  362.  
  363. printf("\n\n");
  364.  
  365. printf("cursor word loop forwards\n");
  366. for (int i = 0; i < 3; i++) // how do i know how big table is?
  367. {
  368. dllnode *cursor = table[i];
  369. for (int j = 0; cursor != NULL; j++)
  370. {
  371. printf("%s ", cursor->word);
  372. cursor = cursor->next;
  373. }
  374. }
  375. printf("\n\n");
  376.  
  377. printf("cursor word loop backwards\n");
  378. for (int i = 2; i >= 0; i--) // how do i know how big table is?
  379. {
  380. dllnode *cursor = table[i]->next->next;
  381. for (int j = 0; cursor != NULL; j++)
  382. {
  383. printf("%s ", cursor->word);
  384. cursor = cursor->prev;
  385. }
  386. }
  387. printf("\n\n");
  388. printf("testing the find function");
  389.  
  390. bool resultm = find(table[2],"Maya");
  391. printf("now looking for a word with the find() function\n");
  392. printf("does the word maya appear in the table[2]?\n");
  393. printf("looking for the word Maya in table[2]\n");
  394. printf("find(table[2],resultm) prints: \n");
  395. printf("%d\n",resultm);
  396. printf("resultm prints:\n");
  397. if (resultm)
  398. {
  399. printf("Found!\n\n");
  400. }
  401. else
  402. printf("Not Found :(\n\n");
  403.  
  404. printf("searching table[1] for papaya\n");
  405. bool resultp = find(table[0], "papaya");
  406. if (resultp)
  407. {
  408. printf("papaya found!\n");
  409. }
  410. else
  411. printf("papaya not found :(");
  412.  
  413. bool results = find(table[1], "stairs");
  414. if (results)
  415. {
  416. printf("stairs found!\n");
  417. }
  418. else
  419. printf("stairs not found :(\n");
  420.  
  421. printf("the words in table[1] are:\n\n");
  422.  
  423. dllnode *cursor = table[1];
  424. for (int j = 0; cursor != NULL; j++)
  425. {
  426. printf("%s ", cursor->word);
  427. cursor = cursor->next;
  428. }
  429. printf("\n");
  430.  
  431. printf("does the word boobs appear in table[1]?\n");
  432. bool resultb = find(table[1], "boobs");
  433. if (resultb)
  434. {
  435. printf("boobs found!\n");
  436. }
  437. else
  438. printf("boobs not found :(\n");
  439.  
  440. printf("testing the insert function:\n");
  441.  
  442. printf("the words in table[0] before insert() are:\n\n");
  443.  
  444. dllnode *traverse = table[0];
  445. for (int j = 0; traverse != NULL; j++)
  446. {
  447. printf("%s ", traverse->word);
  448. traverse = traverse->next;
  449. }
  450. printf("\n\n");
  451.  
  452.  
  453. char *insword = "hereiam!";
  454. char *outword = table[0]->next->word;
  455.  
  456. table[0] = insert(table[0], insword);
  457.  
  458.  
  459. printf("the words in table[0] after insert() are:\n\n");
  460.  
  461. traverse = table[0];
  462. for (int j = 0; traverse != NULL; j++)
  463. {
  464. printf("%s ", traverse->word);
  465. traverse = traverse->next;
  466. }
  467. printf("\n\n");
  468.  
  469. printf("testing the erase function:\n");
  470. printf("trying to erase the node stored at table[0]->next:\n");
  471. erase(table[0]->next);
  472. printf("the words in table[0] after erase() are: \n");
  473.  
  474. /*
  475. delete is a key word and shouldn't be used?
  476. prints hereiam! avocado papaya
  477. apple has been erased
  478. */
  479.  
  480. traverse = table[0];
  481. for (int j = 0; traverse != NULL; j++)
  482. {
  483. printf("%s ", traverse->word);
  484. traverse = traverse->next;
  485. }
  486. printf("\n\n");
  487.  
  488. printf("testing the destroy() function: \n");
  489. printf("the words in table[1], before the destroy() function\n");
  490.  
  491. //travel = table[1];
  492. for (dllnode *travel = table[1];travel != NULL; travel = travel->next)
  493. {
  494. printf("%s ",travel->word);
  495. }
  496. printf("\n");
  497. printf("the words in table[1], after the destroy() function\n");
  498. destroy(table[1]);
  499. for (dllnode *travel = table[1];travel != NULL; travel = travel->next)
  500. {
  501. printf("%s ",travel->word);
  502. }
  503. printf("\n");
  504. printf("all the words each each node of table before destroy print: \n");
  505. //for (int i = 0, dllnode *travel = table[i];travel != NULL, table[i] != NULL;i++, travel = travel->next)
  506. //for (int i = 0, dllnode *travel = table[i]; travel != NULL; travel = travel->next,i++)
  507.  
  508. for (int i = 0; table[i] != NULL; i++)
  509. {
  510. dllnode *travel = table[i];
  511. while (travel != NULL)
  512. //while (travel->next != NULL)
  513. {
  514. printf("%s ",travel->word);
  515. travel = travel->next;
  516. }
  517.  
  518. //printf("all the words each each node of table print: ");
  519. }
  520. printf("\n");
  521.  
  522. free(prog1);
  523. free(prog2);
  524. free(prog3);
  525. free(band1);
  526. free(band2);
  527. free(band3);
  528. free(fruit1);
  529. free(fruit2);
  530. free(fruit3);
  531. //free(input);
  532. }
  533.  
Advertisement
Add Comment
Please, Sign In to add comment