【问题标题】:Segfault while reversing a link list with a give size反转给定大小的链表时出现段错误
【发布时间】:2014-09-28 20:44:01
【问题描述】:

我有一个任务是编写一个反转函数来反转一个最多包含 n 个块的双向链表。我首先从 forloop 中的大小获取端点,然后将 tartpoint 和端点发送到外部函数以反转它们。该外部函数成功地反转了头部和尾部,但是我在尝试反转给定大小时出现了段错误。我需要一些关于出了什么问题的帮助?

反向函数;

/**
 * Helper function to reverse a sequence of linked memory inside a List,
 * starting at startPoint and ending at endPoint. You are responsible for
 * updating startPoint and endPoint to point to the new starting and ending
 * points of the rearranged sequence of linked memory in question.
 *
 * @param startPoint A pointer reference to the first node in the sequence
 *  to be reversed. 
 * @param endPoint A pointer reference to the last node in the sequence to
 *  be reversed.
 */
template <class T>
void List<T>::reverse( ListNode * & startPoint, ListNode * & endPoint )
{
    if( (startPoint == NULL) || (endPoint == NULL) || (startPoint == endPoint))
    {    return;     }                                                                
    ListNode * curr = startPoint;
    ListNode * nexter = NULL;
    ListNode * prever = endPoint->next;             
    while( curr != NULL)
    { 
       nexter = curr->next;
       curr->next = prever;
       prever = curr;
       curr = nexter;
      prever->prev = curr;
  }

  // now swap start and end pts
   nexter = startPoint;
   startPoint = endPoint;
   endPoint = nexter;

}

现在给定sze的反向函数,应该使用上面的函数;

/**
* Reverses blocks of size n in the current List. You should use your
* reverse( ListNode * &, ListNode * & ) helper function in this method!
*
* @param n The size of the blocks in the List to be reversed.
*/
template <class T>
void List<T>::reverseNth( int n )
 {
 if(n == 0)
   return;

  ListNode * startPoint = head;
  ListNode * endPoint = head;
  ListNode * save = NULL;

  for(int i = 0; i< n; i++)                           // need to get endpoint at n
  {
      endPoint = endPoint->next;

   }    
 reverse(startPoint, endPoint);

}

gdb 输出一些奇怪的东西,可能是因为之后处理图像的函数失败了;

 Program received signal SIGINT, Interrupt.
 0x000000000040dcab in __distance<List<RGBAPixel>::ListIterator> (__first=..., __last=...) at      /class/cs225/llvm/include/c++/v1/iterator:488
 488        for (; __first != __last; ++__first)
(gdb) q
A debugging session is active.

 Inferior 1 [process 31022] will be killed.

【问题讨论】:

  • endPoint = endPoint-&gt;next; 如果你到达列表的末尾,你最终会取消引用一个空指针。
  • 您是否尝试查看回溯 (bt)?
  • 我输入了一段时间(endpoint-> != NULL),但程序仍然进入无限循环。回溯将我带到我们不应该接触的其他功能
  • 由于您的列表是双向链接的,因此您需要做的就是在列表中遍历每个节点的 nextprev 字段中的值。然后通过交换startPointendPoint 来完成。
  • 这就是我在顶级函数中所做的

标签: c++ function pointers linked-list segmentation-fault


【解决方案1】:

我需要将其放入回复中,因为我不知道是否可以在评论中进行格式化代码。

也就是说,你在这方面工作太努力了:

ListNode * curr = startPoint;
while( curr != NULL)
{
    ListNode * temp = curr->next;
    curr->next = curr->prev;
    curr->prev = temp;
    curr = temp;
}

应该可以带你去你想去的地方。

【讨论】:

  • 但这根本没有使用“n”,n函数是问题所在。
  • 您可以使用 while(curr != NULL && i
猜你喜欢
  • 2011-10-22
  • 2013-05-03
  • 2017-12-12
  • 1970-01-01
  • 1970-01-01
  • 2019-03-28
  • 1970-01-01
  • 2021-12-31
  • 2015-05-18
相关资源
最近更新 更多