joseleeph

Untitled

Oct 27th, 2020
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. //#define LENGTH 26;
  6.  
  7. typedef struct node
  8. {
  9. char word[26];
  10. struct node *next;
  11. }
  12. node;
  13. const unsigned int N = 26;
  14. //node *table[26];
  15.  
  16. /*
  17. int astr(char *str)
  18. {
  19.  
  20. }
  21. */
  22.  
  23. unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
  24. {
  25.  
  26. char c;
  27. int val = 0;
  28. //int cval;
  29. for (int i = 0; word[i] != '\0'; i++)
  30. {
  31. c = word[i];
  32. if (isalpha(c))
  33. {
  34. if (isupper(c))
  35. {
  36. //c = word[i]
  37. val += c - 65;
  38. }
  39. if (islower(c))
  40. {
  41. val += c - 97;
  42. }
  43. if (c == 13)
  44. {
  45. continue;
  46. }
  47. }
  48. else
  49. continue;
  50.  
  51. }
  52. return val%26;
  53. }
  54. int main(int argc, char *argv[])
  55. {
  56.  
  57. node *fruit1 = malloc(sizeof(node));
  58. node *fruit2 = malloc(sizeof(node));
  59. node *fruit3 = malloc(sizeof(node));
  60.  
  61. char *f1 = "apple";
  62. char *f2 = "avocado";
  63. char *f3 = "papaya";
  64.  
  65. for (int i = 0; f1[i] != '\0'; i++)
  66. {
  67. fruit1->word[i] = f1[i];
  68. }
  69. fruit1->next = fruit2;
  70.  
  71. for (int i = 0; f2[i] != '\0'; i++)
  72. {
  73. fruit2->word[i] = f2[i];
  74. }
  75. fruit2->next = fruit3;
  76.  
  77. for (int i = 0; f3[i] != '\0'; i++)
  78. {
  79. fruit3->word[i] = f3[i];
  80. }
  81. fruit3->next = NULL;
  82.  
  83. printf("\n\nnow printing linked list of fruits: \n\n");
  84. printf("fruit1->word prints: %s\n",fruit1->word);
  85. printf("fruit2->word prints: %s\n",fruit2->word);
  86. printf("fruit3->word prints: %s\n\n",fruit3->word);
  87.  
  88. node *band1 = malloc(sizeof(node));
  89. node *band2 = malloc(sizeof(node));
  90. node *band3 = malloc(sizeof(node));
  91.  
  92. char *b1 = "Soundgarden"; // points to nirvana next
  93. char *b2 = "Nirvana"; // points to mudhoney next
  94. char *b3 = "Mudhoney";
  95.  
  96. for (int i = 0; b1[i] != '\0'; i++)
  97. {
  98. band1->word[i] = b1[i];
  99. }
  100. band1->next = band2;
  101.  
  102. for (int i = 0; b2[i] != '\0'; i++)
  103. {
  104. band2->word[i] = b2[i];
  105. }
  106. band2->next = band3;
  107. for (int i = 0; b3[i] != '\0'; i++)
  108. {
  109. band3->word[i] = b3[i];
  110. }
  111. band3->next = NULL;
  112.  
  113. printf("band1->word prints %s\n", band1->word);
  114. printf("band1->next->word prints %s\n", band1->next->word);
  115.  
  116. node *prog1 = malloc(sizeof(node));
  117. node *prog2 = malloc(sizeof(node));
  118. node *prog3 = malloc(sizeof(node));
  119.  
  120. char *p1 = "Photoshop";
  121. char *p2 = "Maya";
  122. char *p3 = "Zbrush";
  123.  
  124. // copy characters from pointers to word field
  125.  
  126. for (int i = 0; p1[i] != '\0'; i++)
  127. {
  128. prog1->word[i] = p1[i];
  129. }
  130. prog1->next = prog2;
  131.  
  132. for (int i = 0; p2[i] != '\0'; i++)
  133. {
  134. prog2->word[i] = p2[i];
  135. }
  136. prog2->next = prog3;
  137.  
  138. for (int i = 0; p2[i] != '\0'; i++)
  139. {
  140. prog3->word[i] = p3[i];
  141. }
  142. prog3->next = NULL;
  143.  
  144.  
  145.  
  146. //node *table[N] = malloc(N*sizeof(node));
  147.  
  148. node *table[3];
  149. table[0] = fruit1;
  150. table[1] = band1;
  151. table[2] = prog1;
  152.  
  153. //printf("table[0]->word prints %s\n",table[0]->word);
  154.  
  155. //for (int i = 0; table[i] != NULL; i++)
  156. for (int i = 0; i < 3; i++)
  157. {
  158. printf("table[%i]->word prints: %s\n",i,table[i]->word);
  159. printf("table[%i]->next->word prints: %s\n",i,table[i]->next->word);
  160. printf("table[%i]->next->next->word prints: %s\n",i,table[i]->next->next->word);
  161. //printf("table[%i]->word->next->word prints: %s\n",i,table[i]->word->next->word);
  162. //printf("table[%i]->word->next->word->next->word prints: %s\n",i,table[i]->word->next->word->next->word);
  163. //table[i]->word = table[i]->word->next
  164. }
  165.  
  166.  
  167. for (int i = 0; i < 3; i++)
  168. {
  169. for (int j = 0; table[i]->word[j] != '\0'; j++)
  170. {
  171.  
  172. if (table[i]->word[j] == '\0')
  173. {
  174. printf("%c \n",table[i]->word[j]);
  175. }
  176. else
  177. {
  178. printf("%c \n", table[i]->word[j]);
  179. }
  180. //printf("table[%i]->word[%i] = %c\n", i, j, table[i]->word[j]);
  181. //printf("table[%i]->word[%i]->next->word[%i] = %c\n"i, i, j, table[i]->word[j]);
  182. //for (int k = 0; )
  183. }
  184. }
  185. /*
  186. for (int i = 0; i < 3; i++)
  187. {
  188. node *cursor = table[i];
  189. for (int j = 0; table[i]->next != NULL; j++)
  190. {
  191. printf();
  192. }
  193. }
  194.  
  195.  
  196. for (int i = 0; i < 3; i++)
  197. {
  198. node *cursor = table[i];
  199. for (int j = 0; table[i]->word[j] != '\0'; j++)
  200. {
  201. if (cursor->word[j] == '\0')
  202. {
  203. //printf(" ");
  204. cursor = cursor->next;
  205. printf("\n");
  206. }
  207. else
  208. {
  209. printf("cursor->word = %s",cursor->word);
  210. //printf("cursor->word[%i] = %c",j,cursor->word[j]);
  211. cursor = cursor->next;
  212. }
  213. printf("\n");
  214. }
  215. }
  216. */
  217. printf("cursor word loop\n");
  218. for (int i = 0; i < 3; i++)
  219. {
  220. node *cursor = table[i];
  221. for (int j = 0; cursor->next != NULL; j++)
  222. {
  223. printf("%s ", cursor->word);
  224. cursor = cursor->next;
  225. }
  226. }
  227. printf("\n");
  228.  
  229. //node *cursor = table[1];
  230.  
  231.  
  232. //printf("the words stored in the node stored in table 1 are:\n");
  233. /*
  234. printf("the words in table are \n");
  235. for (int i = 0; table[i]->next != NULL; i++)
  236. {
  237. node *cursor = table[i];
  238. //for (int j = 0; cursor->next != NULL; j++)
  239. for (int j = 0; cursor->word[j] !='\0';j++)
  240. {
  241. //printf("cursor->word prints: %s\n",cursor->word);
  242. printf("%c ",cursor->word[j]);
  243. //cursor = cursor->next;
  244. if (cursor->next == NULL)
  245. {
  246. break;
  247. }
  248. else
  249. {
  250. cursor = cursor->next;
  251. }
  252.  
  253. }
  254. printf("\n");
  255. }
  256. printf("\n");
  257.  
  258. */
  259. //free(cursor);
  260.  
  261. //free(table);
  262. free(prog1);
  263. free(prog2);
  264. free(prog3);
  265. free(band1);
  266. free(band2);
  267. free(band3);
  268. free(fruit1);
  269. free(fruit2);
  270. free(fruit3);
  271. }
  272.  
Advertisement
Add Comment
Please, Sign In to add comment