joseleeph

Untitled

Oct 28th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 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.  
  60.  
  61. // insert a new node into the linked list
  62. sllnode *insert(sllnode *head, char *string)
  63. {
  64. sllnode *tmp = head;
  65. sllnode *input = malloc(sizeof(sllnode));
  66. for (int i = 0; string[i] != '\0'; i++)
  67. {
  68. input->word[i] = string[i];
  69. }
  70. input->next = tmp;
  71. return input;
  72.  
  73. // how to make sure I didn't run out of memory
  74. }
  75. /*
  76.  
  77. steps involved
  78.  
  79. a. dynamically allocate space for a new sllnode
  80. b. check to make sure we didn't run out of memory.
  81. c. populate and insert the node at the beginning of the linked list
  82. d. return a pointer to the new head of the linked list
  83.  
  84. */
  85.  
  86. unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
  87. {
  88.  
  89. char c;
  90. int val = 0;
  91. for (int i = 0; word[i] != '\0'; i++)
  92. {
  93. c = word[i];
  94. if (isalpha(c))
  95. {
  96. if (isupper(c))
  97. {
  98. //c = word[i]
  99. val += c - 65;
  100. }
  101. if (islower(c))
  102. {
  103. val += c - 97;
  104. }
  105. if (c == 13)
  106. {
  107. continue;
  108. }
  109. }
  110. else
  111. continue;
  112.  
  113. }
  114. return val%26;
  115. }
  116. int main(int argc, char *argv[])
  117. {
  118.  
  119. sllnode *fruit1 = malloc(sizeof(sllnode));
  120. sllnode *fruit2 = malloc(sizeof(sllnode));
  121. sllnode *fruit3 = malloc(sizeof(sllnode));
  122.  
  123. char *f1 = "apple";
  124. char *f2 = "avocado";
  125. char *f3 = "papaya";
  126.  
  127. for (int i = 0; f1[i] != '\0'; i++)
  128. {
  129. fruit1->word[i] = f1[i];
  130. }
  131. fruit1->next = fruit2;
  132.  
  133. for (int i = 0; f2[i] != '\0'; i++)
  134. {
  135. fruit2->word[i] = f2[i];
  136. }
  137. fruit2->next = fruit3;
  138.  
  139. for (int i = 0; f3[i] != '\0'; i++)
  140. {
  141. fruit3->word[i] = f3[i];
  142. }
  143. fruit3->next = NULL;
  144.  
  145. printf("\n\nnow printing linked list of fruits: \n\n");
  146. printf("fruit1->word prints: %s\n",fruit1->word);
  147. printf("fruit2->word prints: %s\n",fruit2->word);
  148. printf("fruit3->word prints: %s\n\n",fruit3->word);
  149.  
  150. sllnode *band1 = malloc(sizeof(sllnode));
  151. sllnode *band2 = malloc(sizeof(sllnode));
  152. sllnode *band3 = malloc(sizeof(sllnode));
  153.  
  154. char *b1 = "Soundgarden"; // points to nirvana next
  155. char *b2 = "Nirvana"; // points to mudhoney next
  156. char *b3 = "Mudhoney";
  157.  
  158. for (int i = 0; b1[i] != '\0'; i++)
  159. {
  160. band1->word[i] = b1[i];
  161. }
  162. band1->next = band2;
  163.  
  164. for (int i = 0; b2[i] != '\0'; i++)
  165. {
  166. band2->word[i] = b2[i];
  167. }
  168. band2->next = band3;
  169. for (int i = 0; b3[i] != '\0'; i++)
  170. {
  171. band3->word[i] = b3[i];
  172. }
  173. band3->next = NULL;
  174.  
  175. printf("band1->word prints %s\n", band1->word);
  176. printf("band1->next->word prints %s\n", band1->next->word);
  177.  
  178. sllnode *prog1 = malloc(sizeof(sllnode));
  179. sllnode *prog2 = malloc(sizeof(sllnode));
  180. sllnode *prog3 = malloc(sizeof(sllnode));
  181.  
  182. char *p1 = "Photoshop";
  183. char *p2 = "Maya";
  184. char *p3 = "Zbrush";
  185.  
  186. // copy characters from pointers to word field
  187.  
  188. for (int i = 0; p1[i] != '\0'; i++)
  189. {
  190. prog1->word[i] = p1[i];
  191. }
  192. prog1->next = prog2;
  193.  
  194. for (int i = 0; p2[i] != '\0'; i++)
  195. {
  196. prog2->word[i] = p2[i];
  197. }
  198. prog2->next = prog3;
  199.  
  200. for (int i = 0; p3[i] != '\0'; i++)
  201. {
  202. prog3->word[i] = p3[i];
  203. }
  204. prog3->next = NULL;
  205.  
  206.  
  207. sllnode *table[3];
  208. table[0] = fruit1;
  209. table[1] = band1;
  210. table[2] = prog1;
  211.  
  212. for (int i = 0; i < 3; i++)
  213. {
  214. printf("table[%i]->word prints: %s\n",i,table[i]->word);
  215. printf("table[%i]->next->word prints: %s\n",i,table[i]->next->word);
  216. printf("table[%i]->next->next->word prints: %s\n",i,table[i]->next->next->word);
  217.  
  218. }
  219.  
  220.  
  221. for (int i = 0; i < 3; i++)
  222. {
  223. for (int j = 0; table[i]->word[j] != '\0'; j++)
  224. {
  225.  
  226. if (table[i]->word[j] == '\0')
  227. {
  228. printf("%c \n",table[i]->word[j]);
  229. }
  230. else
  231. {
  232. printf("%c \n", table[i]->word[j]);
  233. }
  234. //printf("table[%i]->word[%i] = %c\n", i, j, table[i]->word[j]);
  235. //printf("table[%i]->word[%i]->next->word[%i] = %c\n"i, i, j, table[i]->word[j]);
  236. //for (int k = 0; )
  237. }
  238. }
  239.  
  240. printf("\n\n");
  241.  
  242. printf("cursor word loop\n");
  243. for (int i = 0; i < 3; i++)
  244. {
  245. sllnode *cursor = table[i];
  246.  
  247. for (int j = 0; cursor != NULL; j++)
  248. {
  249. printf("%s ", cursor->word);
  250. cursor = cursor->next;
  251. }
  252. }
  253. printf("\n\n");
  254.  
  255.  
  256. bool result = find(table[2],"Maya");
  257. printf("now looking for a word with the find() function\n");
  258. printf("does the word maya appear in the table[2]?\n");
  259. printf("looking for the word Maya in table[2]\n");
  260. printf("find(table[2],Maya) prints: \n");
  261. printf("%d\n",find(table[2],"Maya"));
  262. printf("result prints:\n");
  263. if (result)
  264. {
  265. printf("Found!\n");
  266. }
  267. else
  268. printf("Not Found :(\n");
  269. //printf("find(table[2],Maya) prints: \n");
  270. //printf(find(table[2],"Maya"));
  271. /*
  272. for (int i = 0; table[i] != NULL; i++)
  273. {
  274. sllnode *cursor = table[i];
  275. for (int j = 0; cursor != NULL; j++)
  276. {
  277. if (find(cursor->word, "Maya"))
  278. {
  279. printf("the value of found is %d",found);
  280. //cursor = cursor->next;
  281. }
  282. else
  283. {
  284. cursor = cursor->next;
  285. }
  286. }
  287.  
  288. }
  289. */
  290. //printf("the value of found is %d", found);
  291.  
  292. //free(cursor);
  293.  
  294. //free(table);
  295. free(prog1);
  296. free(prog2);
  297. free(prog3);
  298. free(band1);
  299. free(band2);
  300. free(band3);
  301. free(fruit1);
  302. free(fruit2);
  303. free(fruit3);
  304. }
  305.  
Advertisement
Add Comment
Please, Sign In to add comment