求大神指点,为什么不能运行啊!!!(代码如下)
#include<iostream>usingnamespacestd;//定义节点classCNode{friendclassCList;public:CNode(){...
#include<iostream>
using namespace std;
//定义节点
class CNode
{
friend class CList;
public:
CNode(){next = NULL;}
CNode(int data);
private:
int data;
CNode* next;
};
CNode::CNode(int data)
{
this->data = data;
next = NULL;
}
//---------------------------------------
//定义链表
class CList
{
public:
void AddList(int* data,int n);
void SortList(CList* &L);
void CoutList();
void UnionList(CList* &La,CList* &Lb);
private:
CNode head;
};
//往链表中添加元素
void CList::AddList(int* data,int n)
{
int i=0;
for(i;i<n;i++)
{
//生成一个节点
CNode* p=new CNode(data[i]);
//插入到链表
p->next = head.next;
head.next = p;
}
}
//对链表中元素进行排序
void CList::SortList(CList* &L)
{
CNode* p=L->head.next;
while(p)
{
CNode* q=p->next;
while(p)
{
if(p->data>q->data)
{
int t;
t=p->data;
p->data=q->data;
q->data=t;
}
q=q->next;
}
p=p->next;
}
}
//输出链表
void CList::CoutList()
{
CNode* p =head.next;
while(p)
{
cout<<p->data<<endl;
p = p->next;
}
}
//合并链表(还没完成可以不用管)
void CList::UnionList(CList* &La,CList* &Lb)
{
CNode* p=La->head.next;
}
void main()
{
int a[]={1,2,3,4};
int b[]={4,5,2,1,5,7,8};
//分别用aa,bb得到a,b两个数组的元素个数
int aa=sizeof(a)/sizeof(int);
int bb=sizeof(b)/sizeof(int);
CList *La, *Lb;
La->AddList(a,aa);
Lb->AddList(b,bb);
cout<<"La链表为:"<<endl;
La->CoutList();
cout<<"Lb链表为:"<<endl;
Lb->CoutList();
} 展开
using namespace std;
//定义节点
class CNode
{
friend class CList;
public:
CNode(){next = NULL;}
CNode(int data);
private:
int data;
CNode* next;
};
CNode::CNode(int data)
{
this->data = data;
next = NULL;
}
//---------------------------------------
//定义链表
class CList
{
public:
void AddList(int* data,int n);
void SortList(CList* &L);
void CoutList();
void UnionList(CList* &La,CList* &Lb);
private:
CNode head;
};
//往链表中添加元素
void CList::AddList(int* data,int n)
{
int i=0;
for(i;i<n;i++)
{
//生成一个节点
CNode* p=new CNode(data[i]);
//插入到链表
p->next = head.next;
head.next = p;
}
}
//对链表中元素进行排序
void CList::SortList(CList* &L)
{
CNode* p=L->head.next;
while(p)
{
CNode* q=p->next;
while(p)
{
if(p->data>q->data)
{
int t;
t=p->data;
p->data=q->data;
q->data=t;
}
q=q->next;
}
p=p->next;
}
}
//输出链表
void CList::CoutList()
{
CNode* p =head.next;
while(p)
{
cout<<p->data<<endl;
p = p->next;
}
}
//合并链表(还没完成可以不用管)
void CList::UnionList(CList* &La,CList* &Lb)
{
CNode* p=La->head.next;
}
void main()
{
int a[]={1,2,3,4};
int b[]={4,5,2,1,5,7,8};
//分别用aa,bb得到a,b两个数组的元素个数
int aa=sizeof(a)/sizeof(int);
int bb=sizeof(b)/sizeof(int);
CList *La, *Lb;
La->AddList(a,aa);
Lb->AddList(b,bb);
cout<<"La链表为:"<<endl;
La->CoutList();
cout<<"Lb链表为:"<<endl;
Lb->CoutList();
} 展开
1个回答
展开全部
CList *La, *Lb; //这是两个野指针
La->AddList(a,aa);
Lb->AddList(b,bb);
这两个指针都没有给赋值怎么能用呢?
void main()
{
int a[]={1,2,3,4};
int b[]={4,5,2,1,5,7,8};
//分别用aa,bb得到a,b两个数组的元素个数
int aa=sizeof(a)/sizeof(int);
int bb=sizeof(b)/sizeof(int);
CList La, Lb; //不要定义成指针
La.AddList(a,aa);//改为.引用成员,下同
Lb.AddList(b,bb);
cout<<"La链表为:"<<endl;
La.CoutList();
cout<<"Lb链表为:"<<endl;
Lb.CoutList();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询