我不知道为什么我会在这里得到这个段错误。我正在尝试获取所有其他节点,并将其放入新列表中。编辑:这就是我最终得到的结果,但我仍然得到了一个段错误
template <class T>
List<T> List<T>::mixSplit()
{
List<T> newList;
newList.length=0;
for (int count=0;count<2;count++)
newList.head=newList.head->next;
newList.tail=tail;
newList.head->prev=NULL;
newList.tail->next=NULL;
return newList;
}发布于 2011-10-06 08:27:08
在第一次迭代时
for (int count=0;count<1;count++)
newList.head=newList.head->next;...newList.head is NULL...so使用newList.head->next是个坏主意。
我建议你正常地迭代当前列表(即current = head; while(current) ...),在循环中增加一个计数器来跟踪列表中的当前位置,每当循环计数器为偶数或0 (counter % 2 == 0或(counter & 1) == 0)时,就在新列表上使用标准的‘list add’函数来附加一个新节点。
https://stackoverflow.com/questions/7669204
复制相似问题