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;
- }
- //int hword = hash(word);
- //table[hash(word)] = myword;
- //myword->next = table[hash(word)];// should point to the original linked list
- strcpy(myword->word, word);
- /*
- copy the string in the word field of myword into word
- */
- }
- // 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(dllnode *innode)
- {
- dllnode *nownode = innode;
- int count = 0;
- int i = 0;
- while (nownode != NULL)
- {
- count++;
- i++;
- nownode = nownode->next;
- }
- return count;
- }
- 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);
- 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;
- strcpy(band2->word,b2);
- band2->next = band3;
- band2->prev = band1;
- strcpy(band3->word,b3);
- band3->next = NULL;
- band3->prev = band2;
- 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");
- char word[26];
- FILE *dictr = fopen("large","read");
- fread(word,sizeof(char), 8007, dictr);// print 5000 characters
- printf("word now prints:\n");
- printf("%s\n", word);
- //fread(word,sizeof(char), 1000, dictr); prints seg fault
- //fread(word,sizeof(char), 8002, dictr); prints seg fault
- //fread(word,sizeof(char), 8003, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 8004, dictr); prints seg fault
- //fread(word,sizeof(char), 8005, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 8006, dictr); prints seg fault
- //fread(word,sizeof(char), 8007, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 8008, dictr); prints seg fault
- //fread(word,sizeof(char), 8009, dictr); prints seg fault
- //fread(word,sizeof(char), 8010, dictr); prints seg fault
- //fread(word,sizeof(char), 1001, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 700, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 799, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 8999, dictr); prints seg fault
- //fread(word,sizeof(char), 9001, dictr); prints seg fault
- //fread(word,sizeof(char), 9000, dictr); prints seg fault
- //fread(word,sizeof(char), 7998, dictr); print seg fault
- //fread(word,sizeof(char), 7997, dictr); print seg fault
- //fread(word,sizeof(char), 8452, dictr); print seg fault
- //fread(word,sizeof(char), 12000, dictr); print seg fault
- //fread(word,sizeof(char), 8000, dictr); print seg fault
- //fread(word,sizeof(char), 8001, dictr); doesnt print seg fault
- //fread(word,sizeof(char), 8002, dictr); print seg fault
- //fread(word,sizeof(char), 1999, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 2000, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 3000, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 4000, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 4652, dictr); prints seg fault
- //fread(word,6*sizeof(char), 1000, dictr); prints seg fault
- //fread(word,5*sizeof(char), 1000, dictr);// characters doesn't print seg fault
- //fread(word,4*sizeof(char), 1000, dictr); prints seg fault
- //fread(word,2*sizeof(char), 1000, dictr); doesn't give seg fault..prints 2000 chars...is '\0' included?
- //fread(word,15*sizeof(char), 1000, dictr); prints seg fault
- //fread(word,20*sizeof(char), 1000, dictr);// prints seg fault
- //fread(word,sizeof(char), 999, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 1000, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 3457, dictr); doesn't print seg fault
- //fread(word,sizeof(char), 10000, dictr); prints seg fault
- //fread(word,sizeof(char), 10001, dictr); prints seg fault
- //fread(word,sizeof(char), 10002, dictr); prints seg fault
- //fread(word,sizeof(char), 10003, dictr); prints seg fault
- /*
- printf("the characters in word are:\n");
- for (int i = 0; word[i] != '\0';i++)
- {
- printf("%c ",word[i]);
- }
- */
- printf("\n");
- free(prog1);
- free(prog2);
- free(prog3);
- free(band1);
- free(band2);
- free(band3);
- free(fruit1);
- free(fruit2);
- free(fruit3);
- //free(input);
- }
- //bool load(const char *dictionary)
- //{
- //FILE *dictr = fopen(dictionary,"r");
- //}
- /*
- take as agrument the dictionary and load
- it into a hash table
- take char *dictionary.. the dictionary file you will
- open and read from in order to load all the data
- into the hash table
- it will return a boolean value, true if you
- were successful at loading all the data into the
- hash table, and false if there was some kind
- of memory error, or if you run out of memory to allocate
- or a file can't be opened
- when you open the file and store all the words, they
- will be stored at a hash table.
- a hash table is an array of linked lists
- to determine which of the linked lists to insert
- the word is based on a hash function.
- a function that will take a string and output an integer
- which will correspond to which of the linked lists
- you want to place the word into
- char int N = 1;
- node *tab
- */
Advertisement
Add Comment
Please, Sign In to add comment