无法将节点追加到C中我的链表的末尾

杰克022

我创建了一个链表,可以在每个节点上存储一个字符串和一个整数。我可以在列表的顶部添加节点,也可以删除它们,但是我在列表末尾添加了一些节点。

我当前的函数append会将节点设置为列表的末尾,但是在我追加一个节点并尝试追加另一个节点之后,我会得到segmentation fault,好像程序已经存在另一个节点无法追加新的节点一样last,我目前正在尝试对其进行调试,但找不到错误的确切行。

// 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);
   } 
}
萨加斯

好吧,这里有很多错误。不知道是什么原因导致的问题,但请尝试解决此问题:

如果不一致,为什么要使用同义词?

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

ListNode *last = *sPtr; 

为什么+1?

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;

您将覆盖发送给该方法的节点的值。可能打算使用lastNode

现在您需要+1

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

这就是我现在所看到的,不知道它是否可以解决细分错误。这个错误是怎么发生的?您仅使用此方法吗?还是您正在对数据进行各种处理?也许问题出在哪里。无论如何,我会再看一下,看看是否还有其他东西。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章