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>
- typedef struct dllist
- {
- char word[26];
- struct dllist *next;
- struct dllist *prev;
- }
- dllnode;
- bool find(dllnode *head, char *string);
- unsigned int hash(const char *word)
- {
- 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->next != NULL)
- {
- count++;
- i++;
- nownode = nownode->next;
- }
- return count;
- }
- // size table[1] returns 2... why not 3?
- /*
- 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 true;
- }
- 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;
- 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("the size of the doubly linked list at table[1] is: \n");
- printf("%i\n",size(table[1]));
- //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