My doubly linked list implementation is as follows that each node holds an array of four values
#define EMPTYNODE 0
struct node {
short data[4]; // pay attention
struct node* next;
struct node* prev;
};
typedef struct node nodeQ_t;
typedef enum{
LIST_FALSE = 0,
LIST_TRUE = 1,
} status_t;
nodeQ_t* createNode(short values[4]){
nodeQ_t* node = (nodeQ_t*)malloc(sizeof(node));
for(int i=0; i < 4; i++){
node->data[i] = values[i];
}
node->next = EMPTYNODE;
node->prev = EMPTYNODE;
return node;
}
now I am trying to write append function in a way that I supply it head and a node created in createNode function so that it would append it to the list.... but it creates a segmentation fault...
status_t appendNode(nodeQ_t* head, nodeQ_t* newNode){
if(head == EMPTYNODE || newNode == EMPTYNODE){
return LIST_FALSE;
};
nodeQ_t* currentNode = head;
while(currentNode != EMPTYNODE){
if(currentNode->next == EMPTYNODE){ //it means that current node is tail
currentNode->next = newNode; //segmenttion fault arises at exactly this line
newNode->prev = currentNode;
}
currentNode = currentNode->next;
}
return LIST_TRUE;
}
please let me know what is the reason for that... for your reference my main function is
int main(){
short array[4] = {1,2,3,4};
nodeQ_t* head = createNode(array);
printList(head);
short array2[4] = {5,6,7,8};
nodeQ_t* newNode = createNode(array2);
appendNode(head, newNode);
printList(head);
return 0;
}
if you need any further information or explanation for anything please do let me know
breakout of yourwhileloop once insertion succeeds.(nodeQ_t*)malloc(sizeof(node)). That should besizeof(*node).nodeis a pointer so you're allocating bytes for the size of the pointer, not the size of the structnodepoints to.mallocisn't a pointer. I'm sayingsizeof(node)is allocating just 4 or 8 bytes (whatever the size of a pointer is) when the correct thing to do is allocate*nodebytes, the size of the struct. See @Johnny Mopp's answer below.nodehere.