joseleeph

Untitled

Oct 27th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 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 sllist
  9. {
  10. char word[26];
  11. struct sllist *next;
  12. }
  13. sllnode;
  14. const unsigned int N = 26;
  15. /*
  16.  
  17. typdef struc sllist
  18. {
  19. value val;
  20. struct sllist *net;
  21. }
  22. sllnode;
  23.  
  24. */
  25.  
  26. bool find(sllnode *head, char *string);
  27. // *head is a pointer to the first element of a linked list
  28. // you will always want to keep the first element of the linked list
  29. // you might want to put it in a global variable
  30. // you always want to keep track of the first element of the linked list since they're all
  31. // chained together
  32. /*
  33. steps involved
  34. a. create a traversal pointer pointing to the list's head
  35. b. if the current node's val field is what we're looking for, report success.
  36. c. if not, set the traversal pointer to the next pointer in the list and go back to step b
  37. d. if you've reached the end of the list, report failure
  38. */
  39.  
  40. bool find(sllnode *head, char *string)
  41. {
  42. sllnode *trav = head;
  43. bool found = true;
  44. for (int i = 0; trav->word[i] != '\0'; i++)
  45. {
  46. if (trav->word[i] != string[i])
  47. {
  48. found = false;
  49. }
  50. else
  51. {
  52. trav = trav->next;
  53. }
  54. //return found;
  55. }
  56. return found;
  57.  
  58. /*
  59. if (trav->word == string)
  60. {
  61. return true;
  62. }
  63. if (trav->word != string)
  64. {
  65. trav = trav->next;
  66. }
  67. if (trav == NULL)
  68. {
  69. return false;
  70. }
  71. else
  72. return false;
  73. */
  74.  
  75. }
  76. // i don't think this works as it's comparing two strings. must compare each char with loop
  77. // try using strcmp()
  78.  
  79. // insert a new node into the linked list
  80. sllnode *insert(sllnode *head, char *string)
  81. {
  82. sllnode *tmp = head;
  83. sllnode *input = malloc(sizeof(sllnode));
  84. for (int i = 0; string[i] != '\0'; i++)
  85. {
  86. input->word[i] = string[i];
  87. }
  88. input->next = tmp;
  89. return input;
  90.  
  91. // how to make sure I didn't run out of memory
  92. }
  93. /*
  94.  
  95. steps involved
  96.  
  97. a. dynamically allocate space for a new sllnode
  98. b. check to make sure we didn't run out of memory.
  99. c. populate and insert the node at the beginning of the linked list
  100. d. return a pointer to the new head of the linked list
  101.  
  102. */
  103.  
  104. unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
  105. {
  106.  
  107. char c;
  108. int val = 0;
  109. //int cval;
  110. for (int i = 0; word[i] != '\0'; i++)
  111. {
  112. c = word[i];
  113. if (isalpha(c))
  114. {
  115. if (isupper(c))
  116. {
  117. //c = word[i]
  118. val += c - 65;
  119. }
  120. if (islower(c))
  121. {
  122. val += c - 97;
  123. }
  124. if (c == 13)
  125. {
  126. continue;
  127. }
  128. }
  129. else
  130. continue;
  131.  
  132. }
  133. return val%26;
  134. }
  135. int main(int argc, char *argv[])
  136. {
  137.  
  138. sllnode *fruit1 = malloc(sizeof(sllnode));
  139. sllnode *fruit2 = malloc(sizeof(sllnode));
  140. sllnode *fruit3 = malloc(sizeof(sllnode));
  141.  
  142. char *f1 = "apple";
  143. char *f2 = "avocado";
  144. char *f3 = "papaya";
  145.  
  146. for (int i = 0; f1[i] != '\0'; i++)
  147. {
  148. fruit1->word[i] = f1[i];
  149. }
  150. fruit1->next = fruit2;
  151.  
  152. for (int i = 0; f2[i] != '\0'; i++)
  153. {
  154. fruit2->word[i] = f2[i];
  155. }
  156. fruit2->next = fruit3;
  157.  
  158. for (int i = 0; f3[i] != '\0'; i++)
  159. {
  160. fruit3->word[i] = f3[i];
  161. }
  162. fruit3->next = NULL;
  163.  
  164. printf("\n\nnow printing linked list of fruits: \n\n");
  165. printf("fruit1->word prints: %s\n",fruit1->word);
  166. printf("fruit2->word prints: %s\n",fruit2->word);
  167. printf("fruit3->word prints: %s\n\n",fruit3->word);
  168.  
  169. sllnode *band1 = malloc(sizeof(sllnode));
  170. sllnode *band2 = malloc(sizeof(sllnode));
  171. sllnode *band3 = malloc(sizeof(sllnode));
  172.  
  173. char *b1 = "Soundgarden"; // points to nirvana next
  174. char *b2 = "Nirvana"; // points to mudhoney next
  175. char *b3 = "Mudhoney";
  176.  
  177. for (int i = 0; b1[i] != '\0'; i++)
  178. {
  179. band1->word[i] = b1[i];
  180. }
  181. band1->next = band2;
  182.  
  183. for (int i = 0; b2[i] != '\0'; i++)
  184. {
  185. band2->word[i] = b2[i];
  186. }
  187. band2->next = band3;
  188. for (int i = 0; b3[i] != '\0'; i++)
  189. {
  190. band3->word[i] = b3[i];
  191. }
  192. band3->next = NULL;
  193.  
  194. printf("band1->word prints %s\n", band1->word);
  195. printf("band1->next->word prints %s\n", band1->next->word);
  196.  
  197. sllnode *prog1 = malloc(sizeof(sllnode));
  198. sllnode *prog2 = malloc(sizeof(sllnode));
  199. sllnode *prog3 = malloc(sizeof(sllnode));
  200.  
  201. char *p1 = "Photoshop";
  202. char *p2 = "Maya";
  203. char *p3 = "Zbrush";
  204.  
  205. // copy characters from pointers to word field
  206.  
  207. for (int i = 0; p1[i] != '\0'; i++)
  208. {
  209. prog1->word[i] = p1[i];
  210. }
  211. prog1->next = prog2;
  212.  
  213. for (int i = 0; p2[i] != '\0'; i++)
  214. {
  215. prog2->word[i] = p2[i];
  216. }
  217. prog2->next = prog3;
  218.  
  219. for (int i = 0; p3[i] != '\0'; i++)
  220. {
  221. prog3->word[i] = p3[i];
  222. }
  223. prog3->next = NULL;
  224.  
  225.  
  226.  
  227. //node *table[N] = malloc(N*sizeof(node));
  228.  
  229. sllnode *table[3];
  230. table[0] = fruit1;
  231. table[1] = band1;
  232. table[2] = prog1;
  233.  
  234. //printf("table[0]->word prints %s\n",table[0]->word);
  235.  
  236. //for (int i = 0; table[i] != NULL; i++)
  237. for (int i = 0; i < 3; i++)
  238. {
  239. printf("table[%i]->word prints: %s\n",i,table[i]->word);
  240. printf("table[%i]->next->word prints: %s\n",i,table[i]->next->word);
  241. printf("table[%i]->next->next->word prints: %s\n",i,table[i]->next->next->word);
  242. //printf("table[%i]->word->next->word prints: %s\n",i,table[i]->word->next->word);
  243. //printf("table[%i]->word->next->word->next->word prints: %s\n",i,table[i]->word->next->word->next->word);
  244. //table[i]->word = table[i]->word->next
  245. }
  246.  
  247.  
  248. for (int i = 0; i < 3; i++)
  249. {
  250. for (int j = 0; table[i]->word[j] != '\0'; j++)
  251. {
  252.  
  253. if (table[i]->word[j] == '\0')
  254. {
  255. printf("%c \n",table[i]->word[j]);
  256. }
  257. else
  258. {
  259. printf("%c \n", table[i]->word[j]);
  260. }
  261. //printf("table[%i]->word[%i] = %c\n", i, j, table[i]->word[j]);
  262. //printf("table[%i]->word[%i]->next->word[%i] = %c\n"i, i, j, table[i]->word[j]);
  263. //for (int k = 0; )
  264. }
  265. }
  266.  
  267. printf("\n\n");
  268.  
  269. printf("cursor word loop\n");
  270. for (int i = 0; i < 3; i++)
  271. {
  272. sllnode *cursor = table[i];
  273. //for (int j = 0; cursor->next != NULL; j++)
  274. for (int j = 0; cursor != NULL; j++)
  275. {
  276. printf("%s ", cursor->word);
  277. cursor = cursor->next;
  278. }
  279. }
  280. printf("\n\n");
  281.  
  282.  
  283.  
  284. printf("now looking for a word with the find() function\n");
  285. printf("does the word maya appear in the table[2]?\n");
  286. printf("looking for the word Maya in table[2]\n");
  287. printf("find(table[2],Maya) prints: \n");
  288. free(prog1);
  289. free(prog2);
  290. free(prog3);
  291. free(band1);
  292. free(band2);
  293. free(band3);
  294. free(fruit1);
  295. free(fruit2);
  296. free(fruit3);
  297. }
  298.  
Advertisement
Add Comment
Please, Sign In to add comment