Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- //#define LENGTH 26;
- typedef struct node
- {
- char word[26];
- struct node *next;
- }
- node;
- const unsigned int N = 26;
- //node *table[26];
- /*
- int astr(char *str)
- {
- }
- */
- unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
- {
- char c;
- int val = 0;
- //int cval;
- for (int i = 0; word[i] != '\0'; i++)
- {
- c = word[i];
- if (isalpha(c))
- {
- if (isupper(c))
- {
- //c = word[i]
- val += c - 65;
- }
- if (islower(c))
- {
- val += c - 97;
- }
- if (c == 13)
- {
- continue;
- }
- }
- else
- continue;
- }
- return val%26;
- }
- int main(int argc, char *argv[])
- {
- node *fruit1 = malloc(sizeof(node));
- node *fruit2 = malloc(sizeof(node));
- node *fruit3 = malloc(sizeof(node));
- char *f1 = "apple";
- char *f2 = "avocado";
- char *f3 = "papaya";
- for (int i = 0; f1[i] != '\0'; i++)
- {
- fruit1->word[i] = f1[i];
- }
- fruit1->next = fruit2;
- for (int i = 0; f2[i] != '\0'; i++)
- {
- fruit2->word[i] = f2[i];
- }
- fruit2->next = fruit3;
- for (int i = 0; f3[i] != '\0'; i++)
- {
- fruit3->word[i] = f3[i];
- }
- fruit3->next = NULL;
- printf("\n\nnow printing linked list of fruits: \n\n");
- printf("fruit1->word prints: %s\n",fruit1->word);
- printf("fruit2->word prints: %s\n",fruit2->word);
- printf("fruit3->word prints: %s\n\n",fruit3->word);
- node *band1 = malloc(sizeof(node));
- node *band2 = malloc(sizeof(node));
- node *band3 = malloc(sizeof(node));
- char *b1 = "Soundgarden"; // points to nirvana next
- char *b2 = "Nirvana"; // points to mudhoney next
- char *b3 = "Mudhoney";
- for (int i = 0; b1[i] != '\0'; i++)
- {
- band1->word[i] = b1[i];
- }
- band1->next = band2;
- for (int i = 0; b2[i] != '\0'; i++)
- {
- band2->word[i] = b2[i];
- }
- band2->next = band3;
- for (int i = 0; b3[i] != '\0'; i++)
- {
- band3->word[i] = b3[i];
- }
- band3->next = NULL;
- printf("band1->word prints %s\n", band1->word);
- printf("band1->next->word prints %s\n", band1->next->word);
- node *prog1 = malloc(sizeof(node));
- node *prog2 = malloc(sizeof(node));
- node *prog3 = malloc(sizeof(node));
- char *p1 = "Photoshop";
- char *p2 = "Maya";
- char *p3 = "Zbrush";
- // copy characters from pointers to word field
- for (int i = 0; p1[i] != '\0'; i++)
- {
- prog1->word[i] = p1[i];
- }
- prog1->next = prog2;
- for (int i = 0; p2[i] != '\0'; i++)
- {
- prog2->word[i] = p2[i];
- }
- prog2->next = prog3;
- for (int i = 0; p2[i] != '\0'; i++)
- {
- prog3->word[i] = p3[i];
- }
- prog3->next = NULL;
- //node *table[N] = malloc(N*sizeof(node));
- node *table[3];
- table[0] = fruit1;
- table[1] = band1;
- table[2] = prog1;
- //printf("table[0]->word prints %s\n",table[0]->word);
- //for (int i = 0; table[i] != NULL; i++)
- for (int i = 0; i < 3; i++)
- {
- printf("table[%i]->word prints: %s\n",i,table[i]->word);
- printf("table[%i]->next->word prints: %s\n",i,table[i]->next->word);
- printf("table[%i]->next->next->word prints: %s\n",i,table[i]->next->next->word);
- //printf("table[%i]->word->next->word prints: %s\n",i,table[i]->word->next->word);
- //printf("table[%i]->word->next->word->next->word prints: %s\n",i,table[i]->word->next->word->next->word);
- //table[i]->word = table[i]->word->next
- }
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; table[i]->word[j] != '\0'; j++)
- {
- if (table[i]->word[j] == '\0')
- {
- printf("%c \n",table[i]->word[j]);
- }
- else
- {
- printf("%c \n", table[i]->word[j]);
- }
- //printf("table[%i]->word[%i] = %c\n", i, j, table[i]->word[j]);
- //printf("table[%i]->word[%i]->next->word[%i] = %c\n"i, i, j, table[i]->word[j]);
- //for (int k = 0; )
- }
- }
- /*
- for (int i = 0; i < 3; i++)
- {
- node *cursor = table[i];
- for (int j = 0; table[i]->next != NULL; j++)
- {
- printf();
- }
- }
- for (int i = 0; i < 3; i++)
- {
- node *cursor = table[i];
- for (int j = 0; table[i]->word[j] != '\0'; j++)
- {
- if (cursor->word[j] == '\0')
- {
- //printf(" ");
- cursor = cursor->next;
- printf("\n");
- }
- else
- {
- printf("cursor->word = %s",cursor->word);
- //printf("cursor->word[%i] = %c",j,cursor->word[j]);
- cursor = cursor->next;
- }
- printf("\n");
- }
- }
- */
- printf("cursor word loop\n");
- for (int i = 0; i < 3; i++)
- {
- node *cursor = table[i];
- for (int j = 0; cursor->next != NULL; j++)
- {
- printf("%s ", cursor->word);
- cursor = cursor->next;
- }
- }
- printf("\n");
- //node *cursor = table[1];
- //printf("the words stored in the node stored in table 1 are:\n");
- /*
- printf("the words in table are \n");
- for (int i = 0; table[i]->next != NULL; i++)
- {
- node *cursor = table[i];
- //for (int j = 0; cursor->next != NULL; j++)
- for (int j = 0; cursor->word[j] !='\0';j++)
- {
- //printf("cursor->word prints: %s\n",cursor->word);
- printf("%c ",cursor->word[j]);
- //cursor = cursor->next;
- if (cursor->next == NULL)
- {
- break;
- }
- else
- {
- cursor = cursor->next;
- }
- }
- printf("\n");
- }
- printf("\n");
- */
- //free(cursor);
- //free(table);
- free(prog1);
- free(prog2);
- free(prog3);
- free(band1);
- free(band2);
- free(band3);
- free(fruit1);
- free(fruit2);
- free(fruit3);
- }
Advertisement
Add Comment
Please, Sign In to add comment