joseleeph

Untitled

Nov 5th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.06 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. the hash function should return the sum
  39. of the ascii values of the first 3 letters
  40. in char. it works even if the string is fewer
  41. than 3
  42. */
  43. unsigned int hash(const char *word)
  44. {
  45.  
  46. char c;
  47. int val = 0;
  48. //int cval;
  49. for (int i = 0; word[i] != '\0'; i++)
  50. {
  51. if (i < 3)
  52. {
  53. c = word[i];
  54. val += c;
  55. }
  56. else
  57. {
  58. continue;
  59. }
  60. /*
  61. this should work even if there
  62. is no char stored at word i
  63. when the word is shorter than 3 chars?
  64. */
  65.  
  66. }
  67. return val%26;
  68. }
  69. bool find(dllnode *head, char *string)
  70. {
  71. dllnode *trav = head;
  72. bool found = false;
  73.  
  74. for (int i = 0; trav != NULL; i++)
  75. {
  76. for (int j = 0; trav->word[j] != '\0'; j++)
  77. {
  78. if (trav->word[j] == string[j]) // this returns true if any of the characters match
  79. {
  80. found = true;
  81. //continue;?
  82. }
  83. else
  84. found = false;
  85. }
  86. trav=trav->next;
  87. }
  88. return found;
  89. }
  90.  
  91. //unsigned int size(void)
  92. unsigned int size(dllnode *innode)
  93. {
  94. dllnode *nownode = innode;
  95. int count = 0;
  96. int i = 0;
  97. while (nownode->next != NULL)
  98. {
  99. count++;
  100. i++;
  101. nownode = nownode->next;
  102. }
  103. return count;
  104. }
  105. // size table[1] returns 2... why not 3?
  106.  
  107. /*
  108. return the number of words in the dictionary
  109. you might iterate over every linked list in
  110. the hashtable counting the number of
  111. nodes in the linked list
  112.  
  113. or
  114. as you are loading the hash table
  115. you could keep track of the number
  116. of words loaded so farin the linked list and return
  117. the value in the size function
  118.  
  119. */
  120. dllnode *insert(dllnode *head, char *string)
  121. {
  122. //dllnode *tmp = head;
  123.  
  124. dllnode *input = malloc(sizeof(dllnode));
  125. for (int i = 0; string[i] != '\0'; i++)
  126. {
  127. input->word[i] = string[i];
  128. }
  129.  
  130.  
  131. //input->next = head;
  132. //input->prev = NULL;
  133. head->prev = input;
  134. input->prev = NULL;
  135. input->next = head;
  136.  
  137. return input;
  138.  
  139. }
  140.  
  141. void erase(dllnode *target)
  142. {
  143. target->prev->next = target->next;
  144. target->next->prev = target->prev;
  145. free(target);
  146. }
  147.  
  148. /*
  149. void delpoint(dllnode *head)
  150. {
  151. dllnode *cursor = head;
  152. dllnode *tmp = cursor->next;
  153. while (cursor != NULL)
  154. {
  155. //dllnode *tmp = cursor;
  156. free(tmp)
  157. }
  158. }
  159.  
  160.  
  161. steps involved
  162. a. if you've reached a null pointer, stop
  163. b. delete the rest of the list..... how to delete? in a loop? set the value to null? empty?
  164. c. free the current node
  165.  
  166. recursion
  167. delete everyone else
  168. then delete me
  169.  
  170. in order to work with linked lists effectively, there are a number of operations to understand
  171. 1. create a linked list when it doesn't already exist
  172. 2. search through a linked list to find an element
  173. 3.insert a new node into the linked list.
  174. 4. delete a single element from a linked list
  175. this is not easy as you need to keep two pointers
  176. doubly linked lists resolve this issue
  177. 5. delete an entire linked list
  178.  
  179. */
  180.  
  181. void destroy(dllnode *head)
  182. {
  183. dllnode *cursor = head;// create a pointer, "cursor", and point it to the same place as head
  184. while (cursor != NULL)
  185. {
  186. dllnode *temp = cursor->next;// create a pointer and point it to the same place as cursor->next
  187. //free(temp);//free the memory that cursor points to
  188. // prints Soundgarden `
  189. free(cursor);
  190. //prints # `#, - `-
  191. //free the memory that cursor points to
  192. cursor = temp;// set cursor to point to the same place as temp
  193. //free(cursor);
  194. }
  195. //free(cursor);
  196. //free(head);
  197. //dllnode *elim = head;
  198.  
  199. //dllnode *cursor = elim->next;
  200. /*
  201. head points to the first node of the linked list
  202. head and elim point to the first node in the linked list
  203. they hold the address of that node, but they are not that node
  204. elim is also a local variable to the function so setting it to
  205. NULL doesn't affect the actual original head pointer that is in main
  206.  
  207. erasing the address doesn't demolish the building, you just
  208. won't know where it is. setting elim == NULL doesn't magically demolish
  209. the building. the building is still there, the address is now gone
  210.  
  211. if you want to release the memory used by a node,
  212. you have to use free to say that you don't need it anymore.
  213. because the pointer is separate from the thing it points to
  214. calling free on that pointer does not set it to NULL
  215. the pointer will still be pointing to the memory
  216. you freed, it just won't be in memory anymore
  217.  
  218. to free a linked list:
  219.  
  220. 1. Create a pointer "cursor" and point it to the same place as head
  221. 2. as long as cursor is not NULL:
  222. a. create a pointer "temp" and point it to the same place as
  223. "cursor->next"
  224. b. free the memory that "cursor" points to
  225. c. set "cursor" to point to the same place as tmp
  226. */
  227.  
  228. //while (elim->next != NULL)
  229. //while (elim != NULL)
  230. //while (cursor != NULL)
  231. //{
  232. //cursor = cursor->next;
  233. //if (cursor == NULL)
  234.  
  235. //dllnode *cursor = elim->next;
  236.  
  237. //elim = elim->next;
  238. //if (elim != NULL)
  239. //elim = elim->next;
  240.  
  241. /*
  242. if (elim->next == NULL)
  243. {
  244. elim = NULL;
  245. //elim = elim->next;
  246. }
  247. else
  248. {
  249. elim = elim->next;
  250. }
  251. */
  252. //}
  253. return;
  254. /*
  255. dllnode *eliminate = head;
  256. while (eliminate != NULL)
  257. {
  258. eliminate = NULL;
  259. eliminate = eliminate->next;
  260. //eliminate = NULL;
  261. free(eliminate);
  262. }
  263. */
  264. }
  265.  
  266. //bool unload(void)
  267. bool unload(dllnode *nownode)
  268. {
  269. int i = 0;
  270. dllnode *cursor = nownode;
  271. while (cursor->next != NULL)
  272. {
  273. dllnode *tmp = cursor;
  274. cursor = cursor->next;
  275. free(tmp);
  276. i++;
  277. }
  278. return false;
  279. }
  280.  
  281. int main(int argc, char *argv[])
  282. {
  283.  
  284. dllnode *fruit1 = malloc(sizeof(dllnode));
  285. dllnode *fruit2 = malloc(sizeof(dllnode));
  286. dllnode *fruit3 = malloc(sizeof(dllnode));
  287.  
  288. char *f1 = "apple";
  289. char *f2 = "avocado";
  290. char *f3 = "papaya";
  291.  
  292. char *f4 = "avocado";
  293. bool cmp = strcmp(f4, f3);
  294. printf("comparing strings:\n");
  295. printf("string f2 prints: %s\n", f2);
  296. printf("string f4 prints: %s\n", f4);
  297. printf("strcmp(f4, f2) prints: ");
  298. printf("%d ",strcmp(f4,f2));
  299. if(strcmp(f4, f2) == 0)
  300. {
  301. printf("Match!\n");
  302. }
  303. else
  304. {
  305. printf("strings don't match:( \n");
  306. }
  307.  
  308. /*
  309. for (int i = 0; f1[i] != '\0'; i++)
  310. {
  311. fruit1->word[i] = f1[i];
  312. }
  313. */
  314. printf("fruit1->word prints %s\n", fruit1->word);
  315. printf("applying strcpy: \n");
  316. strcpy(fruit1->word,f1);
  317. fruit1->next = fruit2;
  318. fruit1->prev = NULL;
  319.  
  320. strcpy(fruit2->word,f2);
  321. fruit2->next = fruit3;
  322. fruit2->prev = fruit1;
  323.  
  324. strcpy(fruit3->word, f3);
  325. fruit3->next = NULL;
  326. fruit3->prev = fruit2;
  327.  
  328. printf("\n\nnow printing linked list of fruits: \n\n");
  329. printf("fruit1->word prints: %s\n",fruit1->word);
  330. printf("fruit1->next->word prints: %s\n",fruit1->next->word);
  331. printf("fruit1->prev->word prints: %s\n",fruit1->prev->word);
  332. printf("fruit2->word prints: %s\n",fruit2->word);
  333. printf("fruit2->next->word prints: %s\n",fruit2->next->word);
  334. printf("fruit2->prev->word prints: %s\n",fruit2->prev->word);
  335. printf("fruit3->next->word prints: %s\n\n",fruit3->next->word);
  336. printf("fruit3->word prints: %s\n\n",fruit3->word);
  337. printf("fruit3->prev->word prints: %s\n\n",fruit3->prev->word);
  338.  
  339. dllnode *band1 = malloc(sizeof(dllnode));
  340. dllnode *band2 = malloc(sizeof(dllnode));
  341. dllnode *band3 = malloc(sizeof(dllnode));
  342.  
  343. char *b2 = "Nirvana"; // points to mudhoney next
  344. char *b3 = "Mudhoney";
  345.  
  346.  
  347. printf("strcpytest: \n");
  348. printf("band1->word prints %s\n",band1->word);
  349. printf("applying strcpy: \n");
  350.  
  351. strcpy(band1->word,"Soundgarden"/*b1*/);
  352. band1->next = band2;
  353. band1->prev = NULL;
  354.  
  355. printf("now band1->word prints %s\n",band1->word);
  356.  
  357.  
  358. printf("band2->word prints %s\n", band2->word);
  359. printf("applying strcpy: \n");
  360. strcpy(band2->word,b2);
  361. band2->next = band3;
  362. band2->prev = band1;
  363.  
  364. printf("band3->word prints %s\n", band3->word);
  365. printf("applying strcpy: \n");
  366. strcpy(band3->word,b3);
  367. band3->next = NULL;
  368. band3->prev = band2;
  369.  
  370. printf("band1->word prints %s\n", band1->word);
  371. printf("band1->next->word prints %s\n", band1->next->word);
  372.  
  373. dllnode *prog1 = malloc(sizeof(dllnode));
  374. dllnode *prog2 = malloc(sizeof(dllnode));
  375. dllnode *prog3 = malloc(sizeof(dllnode));
  376.  
  377. char *p1 = "Photoshop";
  378. char *p2 = "Maya";
  379. char *p3 = "Zbrush";
  380.  
  381. strcpy(prog1->word, p1);
  382. prog1->next = prog2;
  383. prog1->prev = NULL;
  384.  
  385. strcpy(prog2->word, p2);
  386. prog2->next = prog3;
  387. prog2->prev = prog1;
  388.  
  389. strcpy(prog3->word, p3);
  390. prog3->next = NULL;
  391. prog3->prev = prog2;
  392.  
  393. dllnode *table[3];
  394. table[0] = fruit1;
  395. table[1] = band1;
  396. table[2] = prog1;
  397.  
  398. for (int i = 0; i < 3; i++)
  399. {
  400. printf("table[%i]->word prints: %s\n",i,table[i]->word);
  401. printf("table[%i]->next->word prints: %s\n",i,table[i]->next->word);
  402. printf("table[%i]->next->next->word prints: %s\n",i,table[i]->next->next->word);
  403.  
  404. }
  405.  
  406. for (int i = 0; i < 3; i++)
  407. {
  408. for (int j = 0; table[i]->word[j] != '\0'; j++)
  409. {
  410.  
  411. if (table[i]->word[j] == '\0')
  412. {
  413. printf("%c \n",table[i]->word[j]);
  414. }
  415. else
  416. {
  417. printf("%c \n", table[i]->word[j]);
  418. }
  419. }
  420. }
  421.  
  422. printf("\n\n");
  423.  
  424. printf("cursor word loop forwards\n");
  425. for (int i = 0; i < 3; i++) // how do i know how big table is?
  426. {
  427. dllnode *cursor = table[i];
  428. for (int j = 0; cursor != NULL; j++)
  429. {
  430. printf("%s ", cursor->word);
  431. cursor = cursor->next;
  432. }
  433. }
  434. printf("\n\n");
  435.  
  436. printf("cursor word loop backwards\n");
  437. for (int i = 2; i >= 0; i--) // how do i know how big table is?
  438. {
  439. dllnode *cursor = table[i]->next->next;
  440. for (int j = 0; cursor != NULL; j++)
  441. {
  442. printf("%s ", cursor->word);
  443. cursor = cursor->prev;
  444. }
  445. }
  446. printf("\n\n");
  447. printf("testing the find function");
  448.  
  449. bool resultm = find(table[2],"Maya");
  450. printf("now looking for a word with the find() function\n");
  451. printf("does the word maya appear in the table[2]?\n");
  452. printf("looking for the word Maya in table[2]\n");
  453. printf("find(table[2],resultm) prints: \n");
  454. printf("%d\n",resultm);
  455. printf("resultm prints:\n");
  456. if (resultm)
  457. {
  458. printf("Found!\n\n");
  459. }
  460. else
  461. printf("Not Found :(\n\n");
  462.  
  463. printf("searching table[1] for papaya\n");
  464. bool resultp = find(table[0], "papaya");
  465. if (resultp)
  466. {
  467. printf("papaya found!\n");
  468. }
  469. else
  470. printf("papaya not found :(");
  471.  
  472. bool results = find(table[1], "stairs");
  473. if (results)
  474. {
  475. printf("stairs found!\n");
  476. }
  477. else
  478. printf("stairs not found :(\n");
  479.  
  480. printf("the words in table[1] are:\n\n");
  481.  
  482. dllnode *cursor = table[1];
  483. for (int j = 0; cursor != NULL; j++)
  484. {
  485. printf("%s ", cursor->word);
  486. cursor = cursor->next;
  487. }
  488. printf("\n");
  489.  
  490. printf("does the word boobs appear in table[1]?\n");
  491. bool resultb = find(table[1], "boobs");
  492. if (resultb)
  493. {
  494. printf("boobs found!\n");
  495. }
  496. else
  497. printf("boobs not found :(\n");
  498.  
  499. printf("testing the insert function:\n");
  500.  
  501. printf("the words in table[0] before insert() are:\n\n");
  502.  
  503. dllnode *traverse = table[0];
  504. for (int j = 0; traverse != NULL; j++)
  505. {
  506. printf("%s ", traverse->word);
  507. traverse = traverse->next;
  508. }
  509. printf("\n\n");
  510.  
  511.  
  512. char *insword = "hereiam!";
  513. char *outword = table[0]->next->word;
  514.  
  515. table[0] = insert(table[0], insword);
  516.  
  517.  
  518. printf("the words in table[0] after insert() are:\n\n");
  519.  
  520. traverse = table[0];
  521. for (int j = 0; traverse != NULL; j++)
  522. {
  523. printf("%s ", traverse->word);
  524. traverse = traverse->next;
  525. }
  526. printf("\n\n");
  527.  
  528. printf("testing the erase function:\n");
  529. printf("trying to erase the node stored at table[0]->next:\n");
  530. erase(table[0]->next);
  531. printf("the words in table[0] after erase() are: \n");
  532.  
  533. /*
  534. delete is a key word and shouldn't be used?
  535. prints hereiam! avocado papaya
  536. apple has been erased
  537. */
  538.  
  539. traverse = table[0];
  540. for (int j = 0; traverse != NULL; j++)
  541. {
  542. printf("%s ", traverse->word);
  543. traverse = traverse->next;
  544. }
  545. printf("\n\n");
  546.  
  547. printf("testing the destroy() function: \n");
  548. printf("the words in table[1], before the destroy() function\n");
  549.  
  550. //travel = table[1];
  551. for (dllnode *travel = table[1];travel != NULL; travel = travel->next)
  552. {
  553. printf("%s ",travel->word);
  554. }
  555. printf("\n");
  556. /*
  557. printf("the words in table[1], after the destroy() function\n");
  558. destroy(table[1]);
  559. for (dllnode *travel = table[1];travel != NULL; travel = travel->next)
  560. {
  561. printf("%s ",travel->word);
  562. }
  563. printf("\n");
  564. */
  565. printf("all the words each node of table before destroy print: \n");
  566. //for (int i = 0, dllnode *travel = table[i];travel != NULL, table[i] != NULL;i++, travel = travel->next)
  567. //for (int i = 0, dllnode *travel = table[i]; travel != NULL; travel = travel->next,i++)
  568.  
  569. //for (int i = 0; table[i] != NULL; i++)
  570. for (int i = 0; i < 3; i++)
  571. {
  572. dllnode *travel = table[i];
  573. while (travel != NULL)
  574. //while (travel->next != NULL)
  575. {
  576. printf("%s ",travel->word);
  577. travel = travel->next;
  578. }
  579.  
  580. //printf("all the words each each node of table print: ");
  581. }
  582. printf("\n\n");
  583. printf("all the words eachnode of table after destroy print: \n");
  584. //for (int i = 0; table[i] != NULL; i++)
  585. for (int i = 0; i < 2; i++)
  586. {
  587. dllnode *travel = table[i];
  588. //while (travel != NULL)
  589. /*
  590. the inner loop is concerned with the
  591. contents one one particular index
  592. it should loop through that linked list
  593. until it finds a NULL pointer
  594. */
  595. while (travel->next != NULL)
  596. {
  597. destroy(travel);
  598. travel = travel->next;
  599. }
  600. }
  601. //for (int i = 0; table[i] != NULL; i++)
  602. /*
  603. the value that i holds is
  604. not used to determine if you stop or not
  605.  
  606. your outer for loop should not be concerned
  607. with the values stored in the table
  608. it's only job is to iterate over every index of
  609. the table
  610. */
  611. for (int i = 0; i < 2; i++)
  612. {
  613. dllnode *travel = table[i];
  614. while (travel != NULL)
  615. {
  616. printf("%s ",travel->word);
  617. travel = travel->next;
  618. }
  619. }
  620. printf("\n\n");
  621.  
  622. printf("all the words in table after unload\n");
  623. printf("unloading table:\n");
  624.  
  625. for (int i = 0; i < 2; i++)
  626. {
  627. unload(table[i]);
  628. }
  629.  
  630. printf("printing table after unload\n");
  631. for (int i = 0; i < 2; i++)
  632. {
  633. dllnode *travel = table[i];
  634. while (travel != NULL)
  635. {
  636. printf("%s ",travel->word);
  637. travel = travel->next;
  638. }
  639. }
  640. printf("\n");
  641. printf("testing hash function!\nHashing absolute\n");
  642. char *phen = "absolute";
  643. int hp = hash(phen);
  644. printf("hash(phen) prints %i\n",hp);
  645. printf("phen prints %s\n", phen);
  646. // prints 5
  647. char a = 'a';
  648. char b = 'b';
  649. char s = 's';
  650. printf("char a = %c\nchar b = %c\nchar s = %c\n",a,b,s);
  651. printf("the integer values of a, b, and c are:\n");
  652. printf("a = %i\nb = %i\ns = %i\n",a,b,s);
  653. printf("the value of (a + b + s) = %i\n",(a + b + s));
  654. printf("the value of (a + b + s) modulus 26 = %i\n",(a + b + s)%26);
  655. int modtest = 310%26;
  656. printf("310 modulus 26 = %i\n",modtest);
  657.  
  658. char *name = "Ja";
  659. //int hn = hash(name);
  660. printf("name prints %s\n",name);
  661. printf("hash(name) prints %i\n",hash(name));
  662. printf("name[0] prints: %i\n",name[0]);
  663. printf("name[1] prints: %i\n",name[1]);
  664. printf("name[0] + name[1] prints: %i\n",name[0]+name[1]);
  665. printf("171 modulus 26 prints %i\n", 171%26);
  666. printf("name[0] + name[1] modulus 26 prints: %i\n",(name[0]+name[1])%26);
  667. printf("size(table[1]) prints: ");
  668.  
  669. int tabsize = size(table[1]);
  670. printf("%i\n",tabsize);
  671. // why does size print 2 and not 3?
  672.  
  673. //unload(table[1]);
  674.  
  675. //printf("hashing words into a hash table");
  676.  
  677.  
  678. free(prog1);
  679. free(prog2);
  680. free(prog3);
  681. free(band1);
  682. free(band2);
  683. free(band3);
  684. free(fruit1);
  685. free(fruit2);
  686. free(fruit3);
  687. //free(input);
  688. }
  689.  
Add Comment
Please, Sign In to add comment