joseleeph

Untitled

Oct 29th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <cs50.h>
  5.  
  6. //#define LENGTH 26;
  7.  
  8. typedef struct dllist
  9. {
  10. char word[26];
  11. struct dllist *next;
  12. struct dllist *prev;
  13. }
  14. dllnode;
  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. dllnode *dlinsert(dllnode *head, char *string)
  38. {
  39. //dllnode *tmp = head->next;
  40. //dllnode *tmp = head;
  41. dllnode *input = malloc(sizeof(dllnode));
  42.  
  43. for (int i = 0; string[i] != '\0'; i++)
  44. {
  45. input->word[i] = string[i];
  46. }
  47. input->next = head;
  48. head->prev = input;
  49. //input->next = head;
  50. //input->prev = NULL;
  51. return input;
  52. //free(input);
  53. // how to make sure I didn't run out of memory
  54. // have i inserted correctly?
  55. //will i orphan the entire list this way?... it would seem not
  56. // the old hea
  57. }
  58. /*
  59.  
  60. steps involved
  61.  
  62. a. dynamically allocate space for a new dllnode
  63. b. check to make sure we didn't run out of memory.
  64. ... how to
  65. c. populate and insert the node at the beginning of the linked list
  66. d. fix the prev pointer of the old head of the linked list
  67. each node points to two nodes
  68. the old head of the linked list needs to point backwards
  69. to the new head of the linked list
  70. e. return a pointer to the new head of the linked list
  71.  
  72. */
  73.  
  74. unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
  75. {
  76.  
  77. char c;
  78. int val = 0;
  79. //int cval;
  80. for (int i = 0; word[i] != '\0'; i++)
  81. {
  82. c = word[i];
  83. if (isalpha(c))
  84. {
  85. if (isupper(c))
  86. {
  87. //c = word[i]
  88. val += c - 65;
  89. }
  90. if (islower(c))
  91. {
  92. val += c - 97;
  93. }
  94. if (c == 13)
  95. {
  96. continue;
  97. }
  98. }
  99. else
  100. continue;
  101.  
  102. }
  103. return val%26;
  104. }
  105. /*
  106. linked lists both sinly and doubly support efficient insertion and deletion of elements
  107. these operations can be done in constant time
  108. the downside id the ability to randomly access list elements is gone... one must search
  109. through the entire list and thus search is now linear time
  110. */
  111. bool find(dllnode *head, char *string)
  112. {
  113. dllnode *trav = head;
  114. bool found = false;
  115.  
  116. for (int i = 0; trav != NULL; i++)
  117. {
  118. for (int j = 0; trav->word[j] != '\0'; j++)
  119. {
  120. if (trav->word[j] == string[j]) // this returns true if any of the characters match
  121. {
  122. found = true;
  123. //continue;?
  124. }
  125. else
  126. found = false;
  127. }
  128. trav=trav->next;
  129. }
  130. return found;
  131. }
  132.  
  133. /*
  134. deleting nodes is an advantage doubly linked lists offer over singly linked lists
  135. deleting a linked node
  136. steps involved
  137. a. fix the pointers of the surrounding nodes to "skip over" target
  138. b. free target
  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 destroy(slnode* head)
  150. {
  151.  
  152. }
  153.  
  154.  
  155. steps involved
  156. a. if you've reached a null pointer, stop
  157. b. delete the rest of the list..... how to delete? in a loop? set the value to null? empty?
  158. c. free the current node
  159.  
  160. recursion
  161. delete everyone else
  162. then delete me
  163.  
  164. in order to work with linked lists effectively, there are a number of operations to understand
  165. 1. create a linked list when it doesn't already exist
  166. 2. search through a linked list to find an element
  167. 3.insert a new node into the linked list.
  168. 4. delete a single element from a linked list
  169. this is not easy as you need to keep two pointers
  170. doubly linked lists resolve this issue
  171. 5. delete an entire linked list
  172. */
  173. //make a function that will add an input string to the word field of a dllnode
  174. dllnode *addstr(dllnode *node,char *string)
  175. {
  176. node = malloc(sizeof(dllnode));
  177. for (int i = 0; string[i] != '\0';i++)
  178. {
  179. node->word[i] = string[i];
  180. }
  181. //for(int i = 0; string[i] != '\0'; i++)
  182. return node;
  183. //free(node);
  184. }
  185.  
  186. int main(int argc, char *argv[])
  187. {
  188.  
  189. //make a function that will add an input string to the word field of a dllnode
  190.  
  191. dllnode *fruit1 = malloc(sizeof(dllnode));
  192. dllnode *fruit2 = malloc(sizeof(dllnode));
  193. dllnode *fruit3 = malloc(sizeof(dllnode));
  194.  
  195. addstr(fruit1, "apple");
  196. /*
  197. char *f1 = "apple";
  198. char *f2 = "avocado";
  199. char *f3 = "papaya";
  200.  
  201. for (int i = 0; f1[i] != '\0'; i++)
  202. {
  203. fruit1->word[i] = f1[i];
  204. }
  205. */
  206. fruit1->next = fruit2;
  207. fruit1->prev = NULL;
  208. /*
  209. for (int i = 0; f2[i] != '\0'; i++)
  210. {
  211. fruit2->word[i] = f2[i];
  212. }
  213. */
  214. addstr(fruit2, "avocado");
  215. fruit2->next = fruit3;
  216. fruit2->prev = fruit1;
  217. /*
  218. for (int i = 0; f3[i] != '\0'; i++)
  219. {
  220. fruit3->word[i] = f3[i];
  221. }
  222. */
  223. addstr(fruit3, "papaya");
  224. fruit3->next = NULL;
  225. fruit3->prev = fruit2;
  226.  
  227. printf("\n\nnow printing linked list of fruits: \n\n");
  228. printf("fruit1->word prints: %s\n",fruit1->word);
  229. printf("fruit1->next->word prints: %s\n",fruit1->next->word);
  230. printf("fruit1->prev->word prints: %s\n",fruit1->prev->word);
  231. printf("fruit2->word prints: %s\n",fruit2->word);
  232. printf("fruit2->next->word prints: %s\n",fruit2->next->word);
  233. printf("fruit2->prev->word prints: %s\n",fruit2->prev->word);
  234. printf("fruit3->next->word prints: %s\n\n",fruit3->next->word);
  235. printf("fruit3->word prints: %s\n\n",fruit3->word);
  236. printf("fruit3->prev->word prints: %s\n\n",fruit3->prev->word);
  237.  
  238. dllnode *band1 = malloc(sizeof(dllnode));
  239. dllnode *band2 = malloc(sizeof(dllnode));
  240. dllnode *band3 = malloc(sizeof(dllnode));
  241.  
  242. char *b1 = "Soundgarden"; // points to nirvana next
  243. char *b2 = "Nirvana"; // points to mudhoney next
  244. char *b3 = "Mudhoney";
  245.  
  246. for (int i = 0; b1[i] != '\0'; i++)
  247. {
  248. band1->word[i] = b1[i];
  249. }
  250. band1->next = band2;
  251. band1->prev = NULL;
  252.  
  253. for (int i = 0; b2[i] != '\0'; i++)
  254. {
  255. band2->word[i] = b2[i];
  256. }
  257. band2->next = band3;
  258. band2->prev = band1;
  259.  
  260. for (int i = 0; b3[i] != '\0'; i++)
  261. {
  262. band3->word[i] = b3[i];
  263. }
  264. band3->next = NULL;
  265. band3->prev = band2;
  266.  
  267. printf("band1->word prints %s\n", band1->word);
  268. printf("band1->next->word prints %s\n", band1->next->word);
  269.  
  270. dllnode *prog1 = malloc(sizeof(dllnode));
  271. dllnode *prog2 = malloc(sizeof(dllnode));
  272. dllnode *prog3 = malloc(sizeof(dllnode));
  273.  
  274. char *p1 = "Photoshop";
  275. char *p2 = "Maya";
  276. char *p3 = "Zbrush";
  277.  
  278. // copy characters from pointers to word field
  279.  
  280. for (int i = 0; p1[i] != '\0'; i++)
  281. {
  282. prog1->word[i] = p1[i];
  283. }
  284. prog1->next = prog2;
  285. prog1->prev = NULL;
  286.  
  287. for (int i = 0; p2[i] != '\0'; i++)
  288. {
  289. prog2->word[i] = p2[i];
  290. }
  291. prog2->next = prog3;
  292. prog2->prev = prog1;
  293.  
  294. for (int i = 0; p3[i] != '\0'; i++)
  295. {
  296. prog3->word[i] = p3[i];
  297. }
  298. prog3->next = NULL;
  299. prog3->prev = prog2;
  300.  
  301.  
  302.  
  303. //node *table[N] = malloc(N*sizeof(node));
  304.  
  305. dllnode *table[3];
  306. table[0] = fruit1;
  307. table[1] = band1;
  308. table[2] = prog1;
  309.  
  310. for (int i = 0; i < 3; i++)
  311. {
  312. printf("table[%i]->word prints: %s\n",i,table[i]->word);
  313. printf("table[%i]->next->word prints: %s\n",i,table[i]->next->word);
  314. printf("table[%i]->next->next->word prints: %s\n",i,table[i]->next->next->word);
  315.  
  316. }
  317.  
  318.  
  319. for (int i = 0; i < 3; i++)
  320. {
  321. for (int j = 0; table[i]->word[j] != '\0'; j++)
  322. {
  323.  
  324. if (table[i]->word[j] == '\0')
  325. {
  326. printf("%c \n",table[i]->word[j]);
  327. }
  328. else
  329. {
  330. printf("%c \n", table[i]->word[j]);
  331. }
  332. }
  333. }
  334.  
  335. printf("\n\n");
  336.  
  337. printf("cursor word loop forwards\n");
  338. for (int i = 0; i < 3; i++) // how do i know how big table is?
  339. {
  340. dllnode *cursor = table[i];
  341. //for (int j = 0; cursor->next != NULL; j++)
  342. for (int j = 0; cursor != NULL; j++)
  343. {
  344. printf("%s ", cursor->word);
  345. cursor = cursor->next;
  346. }
  347. }
  348. printf("\n\n");
  349.  
  350. printf("cursor word loop backwards\n");
  351. for (int i = 2; i >= 0; i--) // how do i know how big table is?
  352. {
  353. dllnode *cursor = table[i]->next->next;
  354. //for (int j = 0; cursor->next != NULL; j++)
  355. for (int j = 0; cursor != NULL; j++)
  356. {
  357. printf("%s ", cursor->word);
  358. cursor = cursor->prev;
  359. }
  360. }
  361. printf("\n\n");
  362.  
  363. //bool found = false;
  364. printf("testing the find function");
  365.  
  366. bool resultm = find(table[2],"Maya");
  367. printf("now looking for a word with the find() function\n");
  368. printf("does the word maya appear in the table[2]?\n");
  369. printf("looking for the word Maya in table[2]\n");
  370. printf("find(table[2],resultm) prints: \n");
  371. printf("%d\n",resultm);
  372. printf("resultm prints:\n");
  373. if (resultm)
  374. {
  375. printf("Found!\n\n");
  376. }
  377. else
  378. printf("Not Found :(\n\n");
  379.  
  380. printf("searching table[1] for papaya\n");
  381. bool resultp = find(table[0], "papaya");
  382. if (resultp)
  383. {
  384. printf("papaya found!\n");
  385. }
  386. else
  387. printf("papaya not found :(");
  388.  
  389. bool results = find(table[1], "stairs");
  390. if (results)
  391. {
  392. printf("stairs found!\n");
  393. }
  394. else
  395. printf("stairs not found :(\n");
  396.  
  397. printf("the words in table[1] are:\n\n");
  398.  
  399. dllnode *cursor = table[1];
  400. for (int j = 0; cursor != NULL; j++)
  401. {
  402. printf("%s ", cursor->word);
  403. cursor = cursor->next;
  404. }
  405. printf("\n");
  406.  
  407. printf("does the word boobs appear in table[1]?\n");
  408. bool resultb = find(table[1], "boobs");
  409. if (resultb)
  410. {
  411. printf("boobs found!\n");
  412. }
  413. else
  414. printf("boobs not found :(\n");
  415.  
  416. printf("testing the dlinsert function:\n");
  417.  
  418. printf("the words in table[0] before dlinsert() are:\n\n");
  419.  
  420. dllnode *traverse = table[0];
  421. for (int j = 0; traverse != NULL; j++)
  422. {
  423. printf("%s ", traverse->word);
  424. traverse = traverse->next;
  425. }
  426. printf("\n\n");
  427.  
  428. /*
  429. dllnode *insertnod = malloc(sizeof(dllnode));
  430.  
  431. char *insword = "hereiam!";
  432. for (int i = 0; insword[i] != '\0'; i++)
  433. {
  434. insertnod->word[i] = insword[i];
  435. }
  436. */
  437.  
  438. char *insword = "hereiam!";
  439. char *outword = table[0]->next->word;
  440.  
  441. table[0] = dlinsert(table[0], insword);
  442.  
  443.  
  444. printf("the words in table[0] after dlinsert() are:\n\n");
  445.  
  446. traverse = table[0];
  447. for (int j = 0; traverse != NULL; j++)
  448. {
  449. printf("%s ", traverse->word);
  450. traverse = traverse->next;
  451. }
  452. printf("\n\n");
  453.  
  454. printf("testing the erase function:\n");
  455. printf("trying to erase the node stored at table[0]->next\n");
  456. erase(table[0]->next);
  457. printf("the words in table[0] after erase() are: \n"); // delete is a key word and shouldn't be used?
  458.  
  459. //table[0] = erase(table[0]->next);
  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.  
  470.  
  471.  
  472.  
  473. free(prog1);
  474. free(prog2);
  475. free(prog3);
  476. free(band1);
  477. free(band2);
  478. free(band3);
  479. free(fruit1);
  480. free(fruit2);
  481. free(fruit3);
  482. //free(input);
  483. }
  484.  
Add Comment
Please, Sign In to add comment