Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <string.h>
- #include <stdio.h>
- #include <cs50.h>
- #include <ctype.h>
- #define LENGTH 45
- typedef struct linkedList {
- char word[LENGTH + 1];
- struct linkedList *next;
- }
- list;
- struct linkedList* create (string newWord) { //for creating a newElement that can enter our dictionary hash table.
- eprintf("create has been called successfully!\n");
- list *newElement = malloc(sizeof(newWord));
- if (newElement == NULL) { //check for failure
- printf("fail \n");
- }
- eprintf("no problems w/creating space for the newElement!\n");
- strcpy(newElement->word, newWord);
- newElement->next = NULL; //nextPointer is null for safe keeping!
- eprintf("newWord should have been assigned, as well as the element! I hope?\n");
- return newElement;
- }
- int hash (string newWord) {
- int l = strlen(newWord);
- for(int i = 0; i < l; i++) {
- if (isalpha(newWord[i]) == 0) {
- eprintf("fail!\n");
- return -1;
- }
- }
- int index = newWord[0] - 97;
- return index;
- }
- void insert (struct linkedList *newElement, struct linkedList *arrayAreaHeader) {
- if (arrayAreaHeader == NULL) {
- arrayAreaHeader = malloc(sizeof(list));
- arrayAreaHeader = newElement;
- eprintf("your new element has become the first array \n");
- } else {
- // check if NEXT is free, recursively; how do I do that? -> then I can assign the pointer value of Element to NEXT! ALT: add as head and make changes... faster as well!
- for (list *temp = arrayAreaHeader; temp != NULL; temp = temp->next) {
- if (temp->next == NULL) {
- temp->next = newElement; break;
- }
- }
- }
- }
- int main () {
- // TODO
- list *myDict[26] = { NULL };
- FILE *file = fopen("small", "r");
- if (file == NULL)
- {
- fprintf(stderr, "ah darn");
- }
- char tempWord[30];
- fscanf(file, "%s", tempWord);
- eprintf("%s first\n", tempWord);
- list *newElement = create(tempWord);
- eprintf("%s\n", newElement->word);
- int numero = hash(tempWord);
- eprintf("do we get this far? \n");
- insert(newElement, myDict[numero]);
- eprintf("yo we made it: %s\n", myDict[numero]->word);
- fclose(file);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement