!求助:链表类问题(链表中的元素是对象)
一个关于链表类的问题(链表中的元素是对象)
我知道这段程序有问题,但就是不知错在哪里,望高手指点
小弟不胜感激!!!!
1、对象的定义:
class CStudent
{
public:
char name[20];
int num;
static int count;
CStudent *next;
CStudent()
{
count++;
}
~CStudent()
{
count--;
}
void ShowCount();
friend void Input(CStudent& a);
friend void Output(CStudent& b);
};
int CStudent::count=0;
void CStudent::ShowCount()
{
cout<<CStudent::count<<endl;
}
void Input(CStudent& a)
{
cout<<"input name"<<endl;
cin>>a.name;
cout<<"input num"<<endl;
cin>>a.num;
cout<<endl;
}
void Output(CStudent& b)
{
cout<<b.name<<" "<<b.num<<endl;
}
2、以以上对象为元素的链表类:
class StuLine
{
public:
StuLine();
~StuLine();
void Insert(CStudent *a);
void Watch();
private:
CStudent *head;
CStudent *tail;
};
StuLine::StuLine()
{
head=new CStudent;
tail=new CStudent;
head->next=tail;
}
StuLine::~StuLine()
{
delete head;
delete tail;
cout<<"line deleted!"<<endl;
}
void StuLine::Insert(CStudent *a)
{
tail=a;
tail=tail->next;
tail->next=0;
}
void StuLine::Watch()
{
CStudent *a;
a=head;
a=a->next;
for(;a!=0;a=a->next)
{
//cout<<a->ShowCount()<<endl;
cout<<a->num<<" "<<a->name<<endl;
}
}
3、测试函数(主函数)
int main(int argc, char* argv[])
{
CStudent *mic;
mic=new CStudent;
mic->ShowCount();
Input(*mic);
StuLine line;
line.Insert(mic);
line.Watch();
return 0;
}
我知道这段程序有问题,但就是不知错在哪里,望高手指点
问题点数:20、回复次数:8Top
1 楼fireman_lh()回复于 2005-08-04 09:48:03 得分 0
cin>>a.name;
可以这样对数组字符串写入的吗?
看起来怪怪的Top
2 楼junguo(junguo)回复于 2005-08-04 09:57:44 得分 0
给你改过了!
StuLine::StuLine()
{
head=new CStudent;
//tail=new CStudent;
tail = head;
head->next=0;
}
以下是完整的代码!
#include <iostream>
using namespace std;
class CStudent
{
public:
char name[20];
int num;
static int count;
CStudent *next;
CStudent()
{
count++;
}
~CStudent()
{
count--;
}
void ShowCount();
friend void Input(CStudent& a);
friend void Output(CStudent& b);
};
int CStudent::count=0;
void CStudent::ShowCount()
{
cout<<CStudent::count<<endl;
}
void Input(CStudent& a)
{
cout<<"input name"<<endl;
cin>>a.name;
cout<<"input num"<<endl;
cin>>a.num;
cout<<endl;
}
void Output(CStudent& b)
{
cout<<b.name<<" "<<b.num<<endl;
}
class StuLine
{
public:
StuLine();
~StuLine();
void Insert(CStudent *a);
void Watch();
private:
CStudent *head;
CStudent *tail;
};
StuLine::StuLine()
{
head=new CStudent;
//tail=new CStudent;
tail = head;
head->next=0;
}
StuLine::~StuLine()
{
delete head;
delete tail;
cout<<"line deleted!"<<endl;
}
void StuLine::Insert(CStudent *a)
{
tail->next=a;
tail=tail->next;
tail->next=0;
}
void StuLine::Watch()
{
CStudent *a;
a=head;
a=a->next;
for(;a!=0;a=a->next)
{
//cout<<a->ShowCount()<<endl;
cout<<a->num<<" "<<a->name<<endl;
}
}
int main(int argc, char* argv[])
{
CStudent *mic;
mic=new CStudent;
mic->ShowCount();
Input(*mic);
StuLine line;
line.Insert(mic);
line.Watch();
system("pause");
return 0;
}
Top
3 楼qingfenglz()回复于 2005-08-04 10:01:33 得分 0
class CStudent
{
public:
char name[20];
int num;
static int count;
CStudent *next=0;
CStudent()
{
count++;
}
~CStudent()
{
count--;
}
void ShowCount();
friend void Input(CStudent& a);
friend void Output(CStudent& b);
};
int CStudent::count=0;
void CStudent::ShowCount()
{
cout<<CStudent::count<<endl;
}
void Input(CStudent& a)
{
cout<<"input name"<<endl;
cin>>a.name;
cout<<"input num"<<endl;
cin>>a.num;
cout<<endl;
}
void Output(CStudent& b)
{
cout<<b.name<<" "<<b.num<<endl;
}
2、以以上对象为元素的链表类:
class StuLine
{
public:
StuLine();
~StuLine();
void Insert(CStudent *a);
void Watch();
private:
CStudent *head;
CStudent *tail=0;
};
StuLine::StuLine()
{
head=new CStudent;
tail=head;
}
StuLine::~StuLine()
{
delete head;
delete tail;
cout<<"line deleted!"<<endl;
}
void StuLine::Insert(CStudent *a)
{
tail->next=a;
tail=tail->next;
}
void StuLine::Watch()
{
CStudent *a;
a=head;
a=a->next;
for(;a!=0;a=a->next)
{
//cout<<a->ShowCount()<<endl;
cout<<a->num<<" "<<a->name<<endl;
}
}
3、测试函数(主函数)
int main(int argc, char* argv[])
{
CStudent *mic;
mic=new CStudent;
mic->ShowCount();
Input(*mic);
StuLine line;
line.Insert(mic);
line.Watch();
return 0;
}
Top
4 楼qingfenglz()回复于 2005-08-04 10:31:18 得分 0
还应该在最后加一句。
delete mic;Top
5 楼MICHAEL164(MELODY)回复于 2005-08-04 16:50:24 得分 0
using namespace std;这句什么意思?
system("pause");这句呢?
还是不明白
似乎没什么改动呀Top
6 楼MICHAEL164(MELODY)回复于 2005-08-04 17:05:48 得分 0
而且那两个地方都编译报错Top
7 楼MICHAEL164(MELODY)回复于 2005-08-04 17:07:22 得分 0
真是痛苦Top
8 楼jixingzhong(瞌睡虫·星辰)回复于 2005-08-05 19:39:30 得分 0
system("pause");这句呢?
-------------------------
调用 system 命令
暂停画面 (查看输出结果) 任意键继续 ...Top




