以下代码是LZ花两三个小时仔细斟酌的结果,用到的知识点有freopen()的使用,异常处理,仅供参考.如有纰漏,欢迎指正.
#include <iostream>
using namespace std;
//定义一个错误信息,用于抛出异常
char *err = "not find.";
//定义学生结点信息
struct node
{
char name[30];
char id[20];
unsigned short age;
char sex;
//指向下一个结点的指针
struct node *next;
};
//定义学生类
class Student
{
public:
//在构造函数中创建一个头结点
Student()
{
head = new node;
head->next = NULL;
}
void Create(int n);
bool Insert(char *stuID);
bool Delete(char *stuID);
node* Query(char *stuID);
int Length() const;
void Display();
//析构时删除头结点
~Student()
{
delete head;
}
private:
struct node *head;
};
//头插法,先插入的元素在后面
/*
void Student::Create(int n)
{
unsigned short stu_age;
char stu_name[30];
char stu_id[20];
char stu_sex;
node *p;
for(int i = 0; i < n; ++i)
{
p = new node;
cout << "input the "<< i + 1 << "th " << "student's name,id,sex,age:" << endl;
cin >> stu_name >> stu_id >> stu_sex >> stu_age;
strcpy(p->name ,stu_name);
strcpy(p->id,stu_id);
p->sex = stu_sex;
p->age = stu_age;
p->next = head->next;
head->next = p;
}
}
*/
//尾插法 先插入的元素在前面
void Student::Create(int n)
{
char stu_name[30];
char stu_id[20];
char stu_sex;
unsigned short stu_age;
node *p,*q;//需要一个辅助指针q
q = head;
for(int i = 0; i < n; ++i)
{
p = new node;
cout << "input the "<< i + 1 << "th " << "student's name,id,sex,age:" << endl;
cin >> stu_name >> stu_id >> stu_sex >> stu_age;
strcpy(p->name,stu_name);
strcpy(p->id,stu_id);
p->age = stu_age;
p->sex = stu_sex;
p->next = q->next;
q->next = p;
q = p;
}
}
//输出学生结点信息
void Student::Display()
{
node *p = head->next;
while(p != NULL)
{
cout << p->name << '\t' << p->id << '\t' << p->sex << '\t' << p->age << endl;
p = p->next;
}
}
int Student::Length() const
{
int cnt = 0;
node *p = head->next;
while(p != NULL)
{
cnt++;
p = p->next;
}
return cnt;
}
//若未打到指定学号对应的学生信息返回NULL,否则返回学生信息所在的地址
node * Student::Query(char *stuID)
{
node *p = head->next;
while(strcmp(p->id,stuID) != 0 && p->next != NULL)
p = p->next;
if(p->next == NULL)
return NULL;
else
return p;
}
//插入在指定学号学生的后面,找不到直接抛出异常
bool Student::Insert(char *stuID)
{
node *p;
if((p = Query(stuID)) == NULL)
//return false;
throw err;
else
{
node *q = new node;
char stu_name[30];
char stu_id[20];
char stu_sex;
unsigned short stu_age;
cout << "input the student's name,id,sex,age:" << endl;
cin >> stu_name >> stu_id >> stu_sex >> stu_age;
strcpy(q->name,stu_name);
strcpy(q->id,stu_id);
q->age = stu_age;
q->sex = stu_sex;
q->next = p->next;
p->next = q;
return true;
}
}
//删除指定学号的学生
bool Student::Delete(char *stuID)
{
node *p,*q;
p = head;
q = p->next;
//若在查的学生学号与指定学号不同,并且下一个指针不为空,则右移指针p,q
while(strcmp(q->id,stuID) != 0 && q->next != NULL)
{
p = p->next;
q = q->next;
}
//若到最后发现没有找到指定学号的学生信息,则抛出异常.其实只需要返回false就够了
//用异常主要是想复习一下.
if(strcmp(q->id,stuID) != 0 && q->next == NULL)
//return false;
throw 1;
//找到指定学号的学生信息后删除之
else
{
p->next = q->next;
delete q;
q = 0;
return true;
}
}
//测试用例
int main()
{
//用手工方式重复输入了N次数据,实在累了,于是想到用文件读取的方式,太方便了.嘿嘿.
freopen("data.txt","r",stdin);
Student stu;
stu.Create(2);
stu.Display();
try
{
stu.Insert("0910312101");
stu.Display();
cout << "After delete..." << endl;
stu.Delete("0910312103");
stu.Display();
stu.Delete("0910312104");
stu.Display();
}
catch(char *&err)
{
cout << err << endl;
}
catch(...)
{
cout << "unknown error." << endl;
}
fclose(stdin);
return 0;
}