64,646
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
class Node
{
friend class List;
public:
Node():data(0),next(0){}
Node(int n):data(n),next(NULL){}
Node(Node&node)
{
this->data=node.data;
this->next=node.next;
}
Node&operator=(Node&node){this->data=node.data;
this->next=node.next;}
private:
int data;
Node* next;
};
#include"Node.h"
#include<iostream>
using namespace std;
class List
{
public:
List():firstNode(0){}
void createList(List&);
private:
Node *firstNode;
};
#include"LinkList.h"
void List::createList(List&list)
{
int inum=3;
cout<<"请输入数字:";
// cin>>inum;
Node *current=new Node(0);
current=list.firstNode; //使用这种方式赋值,运行到 current->next=newNode;程序中断,
//而使用list.firstNode=current 赋值,可以顺利执行。
// =current;
//firstNode=current;
for(int i=0;i!=inum;++i)
{
Node *newNode=new Node();
newNode->data=i;
// newNode->next=firstNode;
current->next=newNode;
current=newNode;
// firstNode=newNode;
}
}
void List::createList(List&list)
{
int inum=3;
cout<<"请输入数字:";
// cin>>inum;
Node *current=new Node(0);
//firstNode初始化为0,这句就是current=NULL,然后current都为NULL了,
//你下边还current->next当然挂掉。。
//而list.firstNode=current就是让list的首指针指向你新new的Node,当然就没问题。
current=list.firstNode;
for(int i=0;i!=inum;++i)
{
Node *newNode=new Node();
newNode->data=i;
// newNode->next=firstNode;
current->next=newNode;
current=newNode;
// firstNode=newNode;
}
}
void List::createList(List&list)
{
assert(list.firstNode);
//
.....
}