Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started