求解链表问题!!

knorry089 2012-03-31 11:05:42
#include"iostream"
#include<string>
#include<fstream>
#include<sstream>
#include<iomanip>
using namespace std;

class student{
public:int id;
string name;
student *next;

student(int nid=0,string nname="noname");
} ;
student::student(int nid,string nname){id=nid;name=nname;}

class stunode{
student *head;
public:
stunode();
void linsert();
bool ldelete(int nid);
void show();
void doview();
};
stunode::stunode(){head=NULL;}

void stunode::linsert()
{student *p=new student();
cout<<"请输入学号:";
cin>>p->id;
cout<<"请输入姓名:";
cin>>p->name;

if(!head)
{head=p;}
else {
p->next=head;
head=p;
}

}
void stunode::show()
{student *p=head;
cout<<setw(10)/*设置输出宽度*/<<"学号"<<setw(8)<<"姓名"<<endl;

while(p){
cout<<setw(10)<<p->id<<setw(8)<<p->name<<endl;
p=p->next;
}
doview();
}
void stunode::doview()
{while(1){cout<<"1inter"<<"!!"<<"2show"<<"!!"<<"0exit"<<endl;
char n;
cin>>n;
switch(n)
{
case '1':
linsert();break;
case '2':
show();break;

}
}
}

main()
{stunode s;
s.doview();
}


当选择2 显示结果的时候,显示完后会出现错误,,初学 ,不会调试,求大师指点!!
...全文
82 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
knorry089 2012-04-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:

内存泄露伤不起
C/C++ code

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;

class Student
{
public:
int id;
……
[/Quote]准备有文件,不影响编译很多都没有去掉,还是谢谢,,以后一点严谨,
knorry089 2012-04-01
  • 打赏
  • 举报
回复
多谢各位,,又进步一点点了
kobemadi 2012-04-01
  • 打赏
  • 举报
回复
1楼的好规范啊.
PANHL97 2012-04-01
  • 打赏
  • 举报
回复
void stunode::linsert()
{student *p=new student();
cout<<"请输入学号:";
cin>>p->id;
cout<<"请输入姓名:";
cin>>p->name;

if(!head)
{head=p;
head->next=NULL;
_______________
没这个导致你用SHOW时链表取到最后一个时出错
}
else {
p->next=head;
head=p;
}

}
summer_insects 2012-03-31
  • 打赏
  • 举报
回复

析构函数再改一下
StuNode::~StuNode()
{
Student *p = head, *q;
while (p != NULL)
{
q = p;
p = p->next;
delete q;
}
}
summer_insects 2012-03-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

内存泄露伤不起
C/C++ code

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;

class Student
{
public:
int id;
string name;
Stu……
[/Quote]

其实头文件只要
#include <iostream>
#include <string>
#include <iomanip>
就可以了
summer_insects 2012-03-31
  • 打赏
  • 举报
回复
内存泄露伤不起

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;

class Student
{
public:
int id;
string name;
Student *next;

Student(int nid = 0, string nname = "noname", Student *pnext = NULL):
id(nid), name(nname), next(pnext) { }
};

class StuNode
{
Student *head;
public:
StuNode() { head = NULL; }
~StuNode();
void linsert();
bool ldelete(int nid);
void show();
void doview();
};

StuNode::~StuNode()
{
Student *p = head, *q;
while (p->next != NULL)
{
q = p;
p = p->next;
delete q;
}
}

void StuNode::linsert()
{
Student *p = new Student();
cout << "请输入学号:";
cin >> p->id;
cout << "请输入姓名:";
cin >> p->name;

p->next = head;
head = p;
}

void StuNode::show()
{
Student *p = head;
cout << setw(10) << "学号" << setw(8) << "姓名" << endl;

while (p)
{
cout << setw(10) << p->id << setw(8) << p->name << endl;
p = p->next;
}
}

void StuNode::doview()
{
char n;

while (1)
{
cout << "1inter" << "!!" << "2show" << "!!" << "0exit" << endl;
cin >> n;

switch(n)
{
case '1':
linsert();
break;
case '2':
show();
break;
case '0':
return;
default:
break;
}
}
}

int main()
{
StuNode s;
s.doview();
return 0;
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧