65,210
社区成员
发帖
与我相关
我的任务
分享
析构函数再改一下
StuNode::~StuNode()
{
Student *p = head, *q;
while (p != NULL)
{
q = p;
p = p->next;
delete q;
}
}
#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;
}