CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  新手乐园

!求助:链表类问题(链表中的元素是对象)

楼主MICHAEL164(MELODY)2005-08-04 09:35:12 在 C/C++ / 新手乐园 提问

一个关于链表类的问题(链表中的元素是对象)  
  我知道这段程序有问题,但就是不知错在哪里,望高手指点  
  小弟不胜感激!!!!  
  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

相关问题

  • 请教高手:如何从表单元素指针里分离出表单对象来?
  • 如何根据表单元素名获得该对象在表单或者文档中的索引?
  • 一个表单对象,例如text,它的上级元素[如表格TD]已隐藏,如果用JS判断到该表单对象已隐藏,急等
  • 对象表?
  • 删除CList链表元素出错,请帮忙
  • 元素表示的问题??
  • 表格中的元素
  • 棘手:SELECT 对象浮在DHTML元素之上
  • CTypedPtrList模板对象中,如何删除指定位置的元素??
  • 一个图层,我如何知道它包含多少个对象或元素

关键词

  • watch
  • cstudent
  • showcount
  • stuline
  • tail
  • mic
  • 链表
  • cout
  • next
  • 对象

得分解答快速导航

  • 帖主:MICHAEL164

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo