Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <cs50.h>
- //#define LENGTH 26;
- typedef struct sllist
- {
- char word[26];
- struct sllist *next;
- }
- sllnode;
- const unsigned int N = 26;
- /*
- typdef struc sllist
- {
- value val;
- struct sllist *net;
- }
- sllnode;
- */
- bool find(sllnode *head, char *string);
- // *head is a pointer to the first element of a linked list
- // you will always want to keep the first element of the linked list
- // you might want to put it in a global variable
- // you always want to keep track of the first element of the linked list since they're all
- // chained together
- /*
- steps involved
- a. create a traversal pointer pointing to the list's head
- b. if the current node's val field is what we're looking for, report success.
- c. if not, set the traversal pointer to the next pointer in the list and go back to step b
- d. if you've reached the end of the list, report failure
- */
- bool find(sllnode *head, char *string)
- {
- sllnode *trav = head;
- bool found = true;
- for (int i = 0; trav->word[i] != '\0'; i++)
- {
- if (trav->word[i] != string[i])
- {
- found = false;
- }
- else
- {
- trav = trav->next;
- }
- //return found;
- }
- return found;
- /*
- if (trav->word == string)
- {
- return true;
- }
- if (trav->word != string)
- {
- trav = trav->next;
- }
- if (trav == NULL)
- {
- return false;
- }
- else
- return false;
- */
- }
- // i don't think this works as it's comparing two strings. must compare each char with loop
- // try using strcmp()
- // insert a new node into the linked list
- sllnode *insert(sllnode *head, char *string)
- {
- sllnode *tmp = head;
- sllnode *input = malloc(sizeof(sllnode));
- for (int i = 0; string[i] != '\0'; i++)
- {
- input->word[i] = string[i];
- }
- input->next = tmp;
- return input;
- // how to make sure I didn't run out of memory
- }
- /*
- steps involved
- a. dynamically allocate space for a new sllnode
- b. check to make sure we didn't run out of memory.
- c. populate and insert the node at the beginning of the linked list
- d. return a pointer to the new head of the linked list
- */
- 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[])
- {
- sllnode *fruit1 = malloc(sizeof(sllnode));
- sllnode *fruit2 = malloc(sizeof(sllnode));
- sllnode *fruit3 = malloc(sizeof(sllnode));
- 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);
- sllnode *band1 = malloc(sizeof(sllnode));
- sllnode *band2 = malloc(sizeof(sllnode));
- sllnode *band3 = malloc(sizeof(sllnode));
- 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);
- sllnode *prog1 = malloc(sizeof(sllnode));
- sllnode *prog2 = malloc(sizeof(sllnode));
- sllnode *prog3 = malloc(sizeof(sllnode));
- 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; p3[i] != '\0'; i++)
- {
- prog3->word[i] = p3[i];
- }
- prog3->next = NULL;
- //node *table[N] = malloc(N*sizeof(node));
- sllnode *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; )
- }
- }
- printf("\n\n");
- printf("cursor word loop\n");
- for (int i = 0; i < 3; i++)
- {
- sllnode *cursor = table[i];
- //for (int j = 0; cursor->next != NULL; j++)
- for (int j = 0; cursor != NULL; j++)
- {
- printf("%s ", cursor->word);
- cursor = cursor->next;
- }
- }
- printf("\n\n");
- printf("now looking for a word with the find() function\n");
- printf("does the word maya appear in the table[2]?\n");
- printf("looking for the word Maya in table[2]\n");
- printf("find(table[2],Maya) prints: \n");
- 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