2011-10-06 113 views
1

我不知道爲什麼即時獲取此段錯誤在這裏。我正在嘗試將其他節點放在新列表中。 編輯:這是我結束了,但我仍然得到一個segfaultSegfault鏈接列表操作

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; 
} 
+2

歡迎來到Stack Overflow。請記住提出所有有用的答案,包括對其他人提出的問題。並記住要「檢查」(接受)最能回答你自己問題的答案。 –

+0

這是幹什麼用的? 'for(int count = 0; count <1; count ++)' – Dani

+1

這裏有很多額外的問題,代碼根本就沒有做你想做的事情。例如,該循環只運行一次,所以它不需要在那裏。 – darvids0n

回答

1

for (int count=0;count<1;count++) 
    newList.head=newList.head->next; 

第一次迭代... newList.headNULL ...所以使用newList.head->next是一個壞理念。

我建議你遍歷當前列表中相當正常(即current = head; while(current) ...),計數器遞增的循環中,以跟蹤列表中的當前位置,並且每當循環計數器爲偶數或0(counter % 2 == 0(counter & 1) == 0 )使用新列表上的標準「列表添加」功能添加新節點。