Question: Hi I need to make an implementation for double linked list in C. However when I try any, I get a Segmentaiton Fault error in
Hi I need to make an implementation for double linked list in C. However when I try any, I get a Segmentaiton Fault error in my terminal. Here is the skeleton code, I need to have a method that does not cause this error.
| #include | |
| #include | |
| #include | |
| #include "dlist.h" | |
| // Prepend a value in front of the double list. | |
| int dlist_prepend(struct dlist** headp, struct dlist** endp, int value) | |
| { | |
| // Error handling. | |
| if (headp == NULL || endp == NULL) { | |
| return 0; | |
| } | |
| if (*headp == NULL) { | |
| // The list is empty. | |
| assert (*endp == NULL); | |
| *headp = (struct dlist*) malloc(sizeof(struct dlist)); | |
| (*headp)->value = value; | |
| (*headp)->prev = NULL; | |
| (*headp)->next = NULL; | |
| *endp = *headp; | |
| } else { | |
| // Add your code here. | |
| } | |
| return 1; | |
| } | |
| // Append a value in the end of the double list. | |
| int dlist_append(struct dlist** headp, struct dlist** endp, int value) | |
| { | |
| // Error handling. | |
| if (headp == NULL || endp == NULL) { | |
| return 0; | |
| } | |
| if (*headp == NULL) { | |
| // The list is empty. | |
| assert (*endp == NULL); | |
| *headp = (struct dlist*) malloc(sizeof(struct dlist)); | |
| (*headp)->value = value; | |
| (*headp)->prev = NULL; | |
| (*headp)->next = NULL; | |
| *endp = *headp; | |
| } else { | |
| // Add your code here. | |
| } | |
| return 0; | |
| } |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
