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;
- // this will take more bytes of memory
- // than a singly linked list
- // don't use if you don't want to delete elements
- // why can't you call it dllnode initially?
- /*
- singly linked lists are easy to add to, but hard to remove from
- doubly linked lists resolve this issue
- singly linked lists exted the ability to collect and organize data,
- however they can only ever move in one direction through the list
- this makes it difficult to delete a node
- a doubly linked list allows moving forward and backward
- through the list, all by adding one extra pointer to our
- struct definition
- */
- bool find(dllnode *head, char *string);
- 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];
- val += c;
- /*
- 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;
- }
- 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;
- }
- 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 delpoint(dllnode *head)
- {
- dllnode *cursor = head;
- dllnode *tmp = cursor->next;
- while (cursor != NULL)
- {
- //dllnode *tmp = cursor;
- free(tmp)
- }
- }
- steps involved
- a. if you've reached a null pointer, stop
- b. delete the rest of the list..... how to delete? in a loop? set the value to null? empty?
- c. free the current node
- recursion
- delete everyone else
- then delete me
- in order to work with linked lists effectively, there are a number of operations to understand
- 1. create a linked list when it doesn't already exist
- 2. search through a linked list to find an element
- 3.insert a new node into the linked list.
- 4. delete a single element from a linked list
- this is not easy as you need to keep two pointers
- doubly linked lists resolve this issue
- 5. delete an entire linked list
- */
- void destroy(dllnode *head)
- {
- dllnode *cursor = head;// create a pointer, "cursor", and point it to the same place as head
- while (cursor != NULL)
- {
- dllnode *temp = cursor->next;// create a pointer and point it to the same place as cursor->next
- //free(temp);//free the memory that cursor points to
- // prints Soundgarden `
- free(cursor);
- //prints # `#, - `-
- //free the memory that cursor points to
- cursor = temp;// set cursor to point to the same place as temp
- //free(cursor);
- }
- //free(cursor);
- //free(head);
- //dllnode *elim = head;
- //dllnode *cursor = elim->next;
- /*
- head points to the first node of the linked list
- head and elim point to the first node in the linked list
- they hold the address of that node, but they are not that node
- elim is also a local variable to the function so setting it to
- NULL doesn't affect the actual original head pointer that is in main
- erasing the address doesn't demolish the building, you just
- won't know where it is. setting elim == NULL doesn't magically demolish
- the building. the building is still there, the address is now gone
- if you want to release the memory used by a node,
- you have to use free to say that you don't need it anymore.
- because the pointer is separate from the thing it points to
- calling free on that pointer does not set it to NULL
- the pointer will still be pointing to the memory
- you freed, it just won't be in memory anymore
- to free a linked list:
- 1. Create a pointer "cursor" and point it to the same place as head
- 2. as long as cursor is not NULL:
- a. create a pointer "temp" and point it to the same place as
- "cursor->next"
- b. free the memory that "cursor" points to
- c. set "cursor" to point to the same place as tmp
- */
- //while (elim->next != NULL)
- //while (elim != NULL)
- //while (cursor != NULL)
- //{
- //cursor = cursor->next;
- //if (cursor == NULL)
- //dllnode *cursor = elim->next;
- //elim = elim->next;
- //if (elim != NULL)
- //elim = elim->next;
- /*
- if (elim->next == NULL)
- {
- elim = NULL;
- //elim = elim->next;
- }
- else
- {
- elim = elim->next;
- }
- */
- //}
- return;
- /*
- dllnode *eliminate = head;
- while (eliminate != NULL)
- {
- eliminate = NULL;
- eliminate = eliminate->next;
- //eliminate = NULL;
- free(eliminate);
- }
- */
- }
- 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";
- /*
- 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;
- printf("\n\nnow printing linked list of fruits: \n\n");
- printf("fruit1->word prints: %s\n",fruit1->word);
- printf("fruit1->next->word prints: %s\n",fruit1->next->word);
- printf("fruit1->prev->word prints: %s\n",fruit1->prev->word);
- printf("fruit2->word prints: %s\n",fruit2->word);
- printf("fruit2->next->word prints: %s\n",fruit2->next->word);
- printf("fruit2->prev->word prints: %s\n",fruit2->prev->word);
- printf("fruit3->next->word prints: %s\n\n",fruit3->next->word);
- printf("fruit3->word prints: %s\n\n",fruit3->word);
- printf("fruit3->prev->word prints: %s\n\n",fruit3->prev->word);
- 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";
- printf("strcpytest: \n");
- printf("band1->word prints %s\n",band1->word);
- printf("applying strcpy: \n");
- 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;
- 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);
- }
- 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("\n\n");
- printf("cursor word loop forwards\n");
- for (int i = 0; i < 3; i++) // how do i know how big table is?
- {
- dllnode *cursor = table[i];
- for (int j = 0; cursor != NULL; j++)
- {
- printf("%s ", cursor->word);
- cursor = cursor->next;
- }
- }
- printf("\n\n");
- printf("cursor word loop backwards\n");
- for (int i = 2; i >= 0; i--) // how do i know how big table is?
- {
- dllnode *cursor = table[i]->next->next;
- for (int j = 0; cursor != NULL; j++)
- {
- printf("%s ", cursor->word);
- cursor = cursor->prev;
- }
- }
- printf("\n\n");
- printf("testing the find function");
- bool resultm = find(table[2],"Maya");
- 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],resultm) prints: \n");
- printf("%d\n",resultm);
- printf("resultm prints:\n");
- if (resultm)
- {
- printf("Found!\n\n");
- }
- else
- printf("Not Found :(\n\n");
- printf("searching table[1] for papaya\n");
- bool resultp = find(table[0], "papaya");
- if (resultp)
- {
- printf("papaya found!\n");
- }
- else
- printf("papaya not found :(");
- bool results = find(table[1], "stairs");
- if (results)
- {
- printf("stairs found!\n");
- }
- else
- printf("stairs not found :(\n");
- printf("the words in table[1] are:\n\n");
- dllnode *cursor = table[1];
- for (int j = 0; cursor != NULL; j++)
- {
- printf("%s ", cursor->word);
- cursor = cursor->next;
- }
- printf("\n");
- printf("does the word boobs appear in table[1]?\n");
- bool resultb = find(table[1], "boobs");
- if (resultb)
- {
- printf("boobs found!\n");
- }
- else
- printf("boobs not found :(\n");
- printf("testing the insert function:\n");
- printf("the words in table[0] before insert() are:\n\n");
- dllnode *traverse = table[0];
- for (int j = 0; traverse != NULL; j++)
- {
- printf("%s ", traverse->word);
- traverse = traverse->next;
- }
- printf("\n\n");
- char *insword = "hereiam!";
- char *outword = table[0]->next->word;
- table[0] = insert(table[0], insword);
- printf("the words in table[0] after insert() are:\n\n");
- traverse = table[0];
- for (int j = 0; traverse != NULL; j++)
- {
- printf("%s ", traverse->word);
- traverse = traverse->next;
- }
- printf("\n\n");
- printf("testing the erase function:\n");
- printf("trying to erase the node stored at table[0]->next:\n");
- erase(table[0]->next);
- printf("the words in table[0] after erase() are: \n");
- /*
- delete is a key word and shouldn't be used?
- prints hereiam! avocado papaya
- apple has been erased
- */
- traverse = table[0];
- for (int j = 0; traverse != NULL; j++)
- {
- printf("%s ", traverse->word);
- traverse = traverse->next;
- }
- printf("\n\n");
- printf("testing the destroy() function: \n");
- printf("the words in table[1], before the destroy() function\n");
- //travel = table[1];
- for (dllnode *travel = table[1];travel != NULL; travel = travel->next)
- {
- printf("%s ",travel->word);
- }
- printf("\n");
- printf("the words in table[1], after the destroy() function\n");
- destroy(table[1]);
- for (dllnode *travel = table[1];travel != NULL; travel = travel->next)
- {
- printf("%s ",travel->word);
- }
- printf("\n");
- printf("all the words each each node of table before destroy print: \n");
- //for (int i = 0, dllnode *travel = table[i];travel != NULL, table[i] != NULL;i++, travel = travel->next)
- //for (int i = 0, dllnode *travel = table[i]; travel != NULL; travel = travel->next,i++)
- for (int i = 0; table[i] != NULL; 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");
- 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