-2
template <class T>
List<T>::~List()
{
ListItem<T> *temp,*temp1;
temp=head;
while (temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
delete [] temp1;
}
}
/* This class just holds a single data item. */
template <class T>
struct ListItem
{
vector<string> words;
T value;
ListItem<T> *next;
ListItem<T> *prev;
ListItem(T theVal)
{
this->value = theVal;
this->next = NULL;
this->prev = NULL;
}
};
template <class T>
void List<T>::insertAtHead(T item)
{
ListItem<T> *node=new ListItem<T>(item);
if (head==NULL)
{
head=node;
}
else
{
node->next=head;
head->prev=node;
head=node;
}
}
int main()
{
List<int> Mylist;
for (int i=20;i>0;--i)
{
Mylist.insertSorted(i);
}
cout<<Mylist.getTail()->value<<endl;
system("PAUSE");
return 0;
我使用類做一個鏈表和template.When我運行此代碼,它運行良好,但是當我按下一個鍵退出,下面的錯誤出現:「main.exe已停止工作」 我正在使用Dev C++。我添加了析構函數code.I沒有看到任何問題。
超過此無關,與你的鏈表,以及一切與'系統(「暫停」) ;' – StoryTeller 2013-02-18 11:12:03
我刪除了系統(「暫停」),但仍然出現同樣的錯誤 – User14229754 2013-02-18 11:20:24
在銷燬MyList時可能有問題。在調試器中運行以查看它崩潰的位置。 (順便說一句:你爲什麼包括'insertAtHead',你不使用它的代碼?) – molbdnilo 2013-02-18 11:29:32