【问题标题】:Can't append a node to the end of my linked list in C无法将节点附加到 C 中我的链表的末尾
【发布时间】:2018-09-03 16:17:49
【问题描述】:

我创建了一个链表,我可以在其中在每个节点上存储一个字符串和一个 int。我可以在列表顶部添加节点,也可以删除它们,但是在列表末尾附加节点时遇到了一些问题。

我当前的函数append会将节点设置在列表的末尾,但是在我追加一个节点并尝试追加另一个节点后,我会得到segmentation fault,就像程序当已经有另一个 last 时,无法附加一个新的 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);
   } 
}

【问题讨论】:

  • 永远不会 typedef(数据)指针。这会混淆您的代码,不会节省任何输入(实际上您必须输入更多),并且一旦您使用增强的 C 功能,如 const 正确性,它会使事情变得更加复杂(一旦您对 C 有点熟练,您就会注意到)。
  • 我用来学习的同一本书报告了很多使用typedef的数据结构示例,这太疯狂了,应该更清楚
  • 我没有说不要typedefstruct。但我同意,那里有很多蹩脚的书。其中很多是为没有提供const 的K&R C 编写的,而其他限定符(如volatile)通常不会在裸机软件之外使用。但是,正如我用更多的知识写的那样,您应该能够弄清楚这种不当行为的问题所在 - 除了混淆之外。在那之前:最好是明确的而不是隐含的。
  • 这里有一个问题:'newPtr->data= malloc(strlen(value));' - 它是一个短字符,(NUL 终止符)。
  • 键入指针并没有那么糟糕,只要它是显而易见的,例如'ListNodePtr'或'pListNode',(当然,正确使用:)。

标签: c data-structures linked-list


【解决方案1】:

嗯,你这里有很多错误。不确定是什么导致了问题,但请尝试解决此问题:

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

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));

这是我现在看到的,不知道它是否能解决分段错误。这个错误是怎么发生的?你只用这个方法吗?还是您正在对数据进行各种操作?也许问题出在其他地方。无论如何,我会再看一下,看看我是否发现了其他任何东西。

【讨论】:

  • 我会发布整个程序,但它真的很长。不管怎样,谢谢你的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 2018-10-16
  • 2014-02-02
  • 2013-11-13
相关资源
最近更新 更多