【发布时间】:2012-05-06 14:54:27
【问题描述】:
我正在研究单链表,其中我遇到了添加函数的问题,该函数将整数添加到排序方式中。但是我的程序不断崩溃。我整晚都在工作,但我找不到问题所在。有人对此有任何想法吗?
谢谢
template<typename T>
class SLList
{
private:
struct Node
{
Node(const T& val= T(), Node* next_ptr=NULL) : data(val), next(next_ptr) { }
// data members
T data;
Node *next;
};
template<typename T>
void SLList<T>::add(const T& val)
{
if (find(val)==false)
{
Node *curr= head;
Node* prev=NULL;
if(head->next==NULL)
{
cout<<"head";
Node *tmp=new Node(val,head->next);
head->next=tmp;
return;
}
else
{
while(curr->data < val && curr!=NULL)
{
curr=curr->next;
prev=curr;
cout<<"add";
}
Node *tmp=new Node(val, prev->next);
//head->next=tmp;
}
}
} `
【问题讨论】:
-
如果你整晚都在工作,我相信你不介意告诉我们它在哪里崩溃,或者显示 Node 的代码。
-
struct Node { // 构造函数 Node(const T& val= T(), Node* next_ptr=NULL) : data(val), next(next_ptr) { } // 数据成员 T data;节点 *next; };公共:SLList(); SLList(const SLList & lst2); ~SLList();常量 SLList& 运算符=(常量 SLList & rhs); int size() 常量;无效打印()常量; bool find(const T& val) const;无效添加(常量 T& val);无效删除(常量 T& val);私人:节点*头; };
-
节点是复制构造函数,它在while循环中崩溃。
-
请将代码添加到问题中(点击编辑)。
-
我添加了代码。你能查一下吗?
标签: c++ sorting linked-list add