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>
- #include <string.h>
- #include <stdbool.h>
- typedef struct dllist
- #define LENGTH 26
- {
- char word[26];
- struct dllist *next;
- struct dllist *prev;
- }
- dllnode;
- bool find(dllnode *head, char *string);
- unsigned int hash(const char *word);
- bool load(const char *dictionary)
- {
- char word[LENGTH + 1]; // an array of characters
- FILE *dictr = fopen(dictionary,"r"); // a pointer to a file in read mode
- if (dictr == NULL) // if there's nothing there
- {
- return false;
- }
- char wordnow[LENGTH+1]; // create an array
- //node *myword = NULL;
- while (fscanf(dictr,"%s", word) != EOF)
- //scan the file stored in dictr and store the string in word
- {
- dllnode *myword = malloc(sizeof(dllnode));
- if (myword == NULL)
- {
- return false;
- }
- strcpy(myword->word, word);
- // copy word into the string word
- // int the word field
- // of the myword node
- }
- // malloc?
- // int *dictr = malloc(3*sizeof(int));
- //store the pointer to a space of memory
- //called myword at the table index of the hash value of word
- // walkthrough instructions
- /*
- for each word you will
- create a new node that will
- store that word inside the hash function
- */
- //fscanf(dictr,"%s",word);
- /*
- scan words from the pointer to the file dict and send them as a string to word
- */
- return true;
- }
- unsigned int hash(const char *word)//const indicates it can't be changed
- {
- char c;
- int val = 0;
- //int cval;
- for (int i = 0; word[i] != '\0'; i++)
- {
- if (i < 3)
- {
- c = word[i];
- val += c;
- }
- else
- {
- continue;
- }
- }
- return val%26;
- }
- bool find(dllnode *head, char *string)
- {
- dllnode *trav = head;
- bool found = false;
- for (int i = 0; trav != NULL; i++)
- {
- for (int j = 0; trav->word[j] != '\0'; j++)
- {
- if (trav->word[j] == string[j]) // this returns true if any of the characters match
- {
- found = true;
- //continue;?
- }
- else
- found = false;
- }
- trav=trav->next;
- }
- return found;
- }
- //unsigned int size(void)
- unsigned int size(dllnode *innode)
- {
- dllnode *nownode = innode;
- int count = 0;
- int i = 0;
- while (nownode != NULL)
- {
- count++;
- i++;
- nownode = nownode->next;
- }
- return count;
- }
- // size table[1] returns 2... why not 3?
- //because you were askining of nownode-> is not NULL
- /*
- return the number of words in the dictionary
- you might iterate over every linked list in
- the hashtable counting the number of
- nodes in the linked list
- or
- as you are loading the hash table
- you could keep track of the number
- of words loaded so farin the linked list and return
- the value in the size function
- */
- dllnode *insert(dllnode *head, char *string)
- {
- //dllnode *tmp = head;
- dllnode *input = malloc(sizeof(dllnode));
- for (int i = 0; string[i] != '\0'; i++)
- {
- input->word[i] = string[i];
- }
- //input->next = head;
- //input->prev = NULL;
- head->prev = input;
- input->prev = NULL;
- input->next = head;
- return input;
- }
- void erase(dllnode *target)
- {
- target->prev->next = target->next;
- target->next->prev = target->prev;
- free(target);
- }
- void destroy(dllnode *head)
- {
- dllnode *cursor = head;// create a pointer, "cursor", and point it to the same place as head
- //while (cursor != NULL)
- while (cursor->next != NULL)
- // the different between destroy and unload is cursor->next
- {
- dllnode *tmp = cursor;
- cursor = cursor->next;
- free(tmp);
- }
- return;
- }
- //bool unload(void)
- bool unload(dllnode *nownode)
- {
- //int i = 0;
- dllnode *cursor = nownode;
- //while (cursor->next != NULL)
- while (cursor != NULL)
- {
- dllnode *tmp = cursor;
- cursor = cursor->next;
- free(tmp);
- //i++;
- }
- return false;
- }
- int main(int argc, char *argv[])
- {
- dllnode *fruit1 = malloc(sizeof(dllnode));
- dllnode *fruit2 = malloc(sizeof(dllnode));
- dllnode *fruit3 = malloc(sizeof(dllnode));
- char *f1 = "apple";
- char *f2 = "avocado";
- char *f3 = "papaya";
- char *f4 = "avocado";
- bool cmp = strcmp(f4, f3);
- //printf("comparing strings:\n");
- //printf("string f2 prints: %s\n", f2);
- //printf("string f4 prints: %s\n", f4);
- //printf("strcmp(f4, f2) prints: ");
- //printf("%d ",strcmp(f4,f2));
- //if(strcmp(f4, f2) == 0)
- //{
- //printf("Match!\n");
- //}
- //else
- //{
- //printf("strings don't match:( \n");
- //}
- /*
- for (int i = 0; f1[i] != '\0'; i++)
- {
- fruit1->word[i] = f1[i];
- }
- */
- //printf("fruit1->word prints %s\n", fruit1->word);
- //printf("applying strcpy: \n");
- strcpy(fruit1->word,f1);
- fruit1->next = fruit2;
- fruit1->prev = NULL;
- strcpy(fruit2->word,f2);
- fruit2->next = fruit3;
- fruit2->prev = fruit1;
- strcpy(fruit3->word, f3);
- fruit3->next = NULL;
- fruit3->prev = fruit2;
- dllnode *band1 = malloc(sizeof(dllnode));
- dllnode *band2 = malloc(sizeof(dllnode));
- dllnode *band3 = malloc(sizeof(dllnode));
- char *b2 = "Nirvana"; // points to mudhoney next
- char *b3 = "Mudhoney";
- strcpy(band1->word,"Soundgarden"/*b1*/);
- band1->next = band2;
- band1->prev = NULL;
- //printf("now band1->word prints %s\n",band1->word);
- //printf("band2->word prints %s\n", band2->word);
- //printf("applying strcpy: \n");
- strcpy(band2->word,b2);
- band2->next = band3;
- band2->prev = band1;
- //printf("band3->word prints %s\n", band3->word);
- //printf("applying strcpy: \n");
- strcpy(band3->word,b3);
- band3->next = NULL;
- band3->prev = band2;
- //printf("band1->word prints %s\n", band1->word);
- //printf("band1->next->word prints %s\n", band1->next->word);
- dllnode *prog1 = malloc(sizeof(dllnode));
- dllnode *prog2 = malloc(sizeof(dllnode));
- dllnode *prog3 = malloc(sizeof(dllnode));
- char *p1 = "Photoshop";
- char *p2 = "Maya";
- char *p3 = "Zbrush";
- strcpy(prog1->word, p1);
- prog1->next = prog2;
- prog1->prev = NULL;
- strcpy(prog2->word, p2);
- prog2->next = prog3;
- prog2->prev = prog1;
- strcpy(prog3->word, p3);
- prog3->next = NULL;
- prog3->prev = prog2;
- //for the speller assignment you will want to have N = 26*3 compartments
- dllnode *table[3];
- table[0] = fruit1;
- table[1] = band1;
- table[2] = prog1;
- printf("all the words each each node of table before unload print: \n");
- for (int i = 0; i < 3; i++)
- {
- dllnode *travel = table[i];
- while (travel != NULL)
- //while (travel->next != NULL)
- {
- printf("%s ",travel->word);
- travel = travel->next;
- }
- //printf("all the words each each node of table print: ");
- }
- printf("\n\n");
- printf("all the words in table after unload: \n");
- printf("unloading table:\n");
- for (int i = 0; i < 3; i++)
- {
- unload(table[i]);
- }
- printf("printing table after unload\n");
- for (int i = 0; i < 3; i++)
- {
- dllnode *travel = table[i];
- while (travel != NULL)
- {
- printf("%s ",travel->word);
- travel = travel->next;
- }
- }
- printf("\n");
- printf("testing the load function to load a dictionary into word: \n");
- FILE *dictr = fopen("large","r");
- char rletter;
- int i = 0;
- while ((rletter = fgetc(dictr)) != EOF)
- {
- char *rword = NULL;
- while (rletter != '\0')
- {
- rword[i] = rletter;
- i++;
- }
- printf("rword now prints: \n");
- printf("%s\n",rword);
- printf("\n");
- }
- fclose(dictr);
- printf("\n");
- free(prog1);
- free(prog2);
- free(prog3);
- free(band1);
- free(band2);
- free(band3);
- free(fruit1);
- free(fruit2);
- free(fruit3);
- //free(input);
- }
Advertisement
Add Comment
Please, Sign In to add comment