Jack022
Jack022

Reputation: 1257

Can't append a node to the end of my linked list in C

I created a linked list where i can store a string and an int on each node. I can add nodes at the top of the list and i can delete them, but i'm having some troubes appending a node at the end of the list.

My current function append will set the node to be at the end of the list, but after i append one node and i try to append another one, i will get segmentation fault, as if the program can't append a new last when there is already another one, i'm currently trying to debug it but i can't find the exact line/s with the error.

// self-referential structure                       
struct listNode {                                      
   char *data; // each listNode contains a character
   int num;
   struct listNode *nextPtr; // pointer to next node
}; 


typedef struct listNode ListNode; // synonym for struct listNode
typedef ListNode *ListNodePtr; // synonym for ListNode*

void append(ListNodePtr *sPtr, char *value, int valore)
 {  /* 1. allocate node */

    ListNodePtr lastNode = malloc(sizeof(ListNode)+1);

    ListNode *last = *sPtr; 

    /* 2. put in the data  */
    last->data= malloc(strlen(value));
    strcpy(last->data, value);
    last->num = valore;

    /* 3. This new node is going to be the last node, so make next 
          of it as NULL*/
    lastNode->nextPtr = NULL;

    /* 4. If the Linked List is empty, then make the new node as head */
    if (*sPtr == NULL)
    {
       *sPtr = lastNode;
       return;
    }  

    /* 5. Else traverse till the last node */
    while (last->nextPtr != NULL)
        last = last->nextPtr;

    /* 6. Change the next of last node */
    last->nextPtr = lastNode;

 }

// insert a new value into the list in sorted order
void insert(ListNodePtr *sPtr, char *value, int valore)
{ 
   ListNodePtr newPtr = malloc(sizeof(ListNode)+1); // create node

   if (newPtr != NULL) { // is space available
      newPtr->data= malloc(strlen(value));
      strcpy(newPtr->data, value);
      newPtr->num = valore;
      newPtr->nextPtr = NULL; // node does not link to another node
      ListNodePtr previousPtr = NULL;
      ListNodePtr currentPtr = *sPtr;
      // loop to find the correct location in the list       
      while (currentPtr != NULL && value > currentPtr->data) {
         previousPtr = currentPtr; // walk to ...               
         currentPtr = currentPtr->nextPtr; // ... next node 
      }                                          
      // insert new node at beginning of list
      if (previousPtr == NULL) { 
         newPtr->nextPtr = *sPtr;
         *sPtr = newPtr;
      } 
      else { // insert new node between previousPtr and currentPtr
         previousPtr->nextPtr = newPtr;
         newPtr->nextPtr = currentPtr;
      } 
   } 
   else {
      printf("%s not inserted. No memory available.\n", value);
   } 
}

Upvotes: 1

Views: 330

Answers (1)

sagi
sagi

Reputation: 40481

Well, you have a bunch of mistakes here. Not sure what's causing the issue but try to fix this:

Why use synonym if you're not consistent ?

ListNodePtr lastNode = malloc(sizeof(ListNode)+1);

ListNode *last = *sPtr; 

Why +1?

ListNodePtr lastNode = malloc(sizeof(ListNode)+1)

This:

ListNode *last = *sPtr; 

/* 2. put in the data  */
last->data= malloc(strlen(value));
strcpy(last->data, value);
last->num = valore;

You overwrite the values of the node that was sent to this method. Probably intended to use lastNode

Now you need +1

last->data= malloc(strlen(value));

This is what I see for now, dont know if its gonna fix the segmentation fault. How is this error happening? Are you only using this method? or are you doing all kind of manipulation on the data? Maybe the problem is else where . Anyway, I'll have another look and see if I spot anything else.

Upvotes: 1

Related Questions