定义链表中的节点
1、之前企图 char *name; gets(name);之后改为char name[20]还是有点小问题,最后还是用的scanf这个老朋友:scanf("%s",name);
2、原来cout函数字符串也能直接输出
3、基本功实在是不咋扎实,有很多理解偏差,比如到写链表了我还在寻思着 next是个什么…
后来才猛的发现是我在节点里定义的,没有实际指向,要自己赋值,然后在我这里以到last为结束,对last并没有实际赋值,但我看到有一个帖子用的是next->nullptr;
#include <string.h>
#include <iostream.h>
#include <stdio.h>
class cLink
{
friend class cList;
cLink *next;
char name[20];
char sex;
char birth[20];
int num;
float score1,score2,score3,sum;
public:
void input(void)
{
cout << "请依次输入学生姓名,学号,性别(M or F),出生年月日(****\\**\\**)和三门课成绩\n";
scanf("%s",name);
cin >> num >> sex >> birth >> score1 >> score2 >> score3;
sum = score1 + score3 + score2;
}
void disp(void)
{
cout << "姓名:"<< name <<" 学号:"<< num <<" 性别:"<< sex <<" 出生年月日:"<< birth <<endl;
cout <<"score1:"<< score1 <<" score2:"<< score2 <<" score3:" << score3 << endl;
}
} ;
然后定义链表:
1、第一次写的是这个:cList &Append(const cLink &x)(教材上抄来的)…
在后来的主函数中测试的时候,写过两个节点对象:Clist stu1,stu2;
对这两个对象分别赋值、插入链表,程序没问题。
但在后来写swich语句中,因为只有一个对象反复用,就发现插一个可以,插到第二个就不行了。翻到上面插入函数看,估计是“&”的问题,果然删了就好了。
2、Delete函数也是要注意多种情况:空队列,找不着,只有一个学生,正常的大多数情况。
3、 void sumax_min(void)函数:
cLink *now = first;
cLink *min_1 = first;//本来这个没写,寻思着一会也会赋值的
cLink *max_1 = first;
但实际上必然是不行的,因为有可能压根人就不会进到下面的if语句中…(小心得:运行出现乱码,没有赋值的可能性很大!)
class cList
{
cLink *first;
cLink *last;
public:
cList(void)
{
first = last = new cLink;
}
cList &Append(const cLink x)
{
cLink *ptr = last;
*ptr = x;
last = new cLink;
ptr->next = last;
return(* this);
}
void find(const char *x)
{
cLink *now = first;
cLink *ptr = now;
if (first == last)
cout << "该学生管理系统为空\n";
else
{
while(strcmp(now->name,x))
{
ptr = now;
now = now -> next;
if (now == last)
break;
}
if(now == last)
cout << "没有该学生!\n";
else
{
cout << "该学生为:\n";
now -> disp();
}
}
}
void find(const int x)
{
cLink *now = first;
cLink *ptr;
if(first == last)
cout << "该学生系统为空\n";
else
{
while(now->num != x)
{
ptr = now;
now = now -> next;
if(now == last)
break;
}
if(now == last)
cout << "没有该学生!";
else
{
cout << "该学生为:\n";
now->disp();
}
}
}
cList &Delete(const char *x)
{
cLink *now = first;
cLink *ptr = now;
if(first == last)
cout << "该学生管理系统为空!\n";
else
{
while(strcmp(now->name,x))
{
ptr = now;
now = now -> next;
if (now == last)
break;
}
if(now == last)
cout << "没有该学生";
else if(ptr->next != last)
{
ptr->next = now->next;
delete(now);
cout << "已删除该学生\n";
}
else
{
delete(now);
cout << "已删除该学生\n";
first = last;
}
}
return(* this);
}
cList &Delete(const int x)
{
cLink *now = first;
cLink *ptr = now;
if(last = first)
cout << "该学生系统为空!\n";
else
{
while(now->num != x)
{
ptr = now;
now = now -> next;
if (now == last)
break;
}
if(now == last)
cout << "没有该学生";
else if(ptr->next != last)
{
ptr->next = now->next;
cout << "已删除该学生\n";
delete(now);
}
else
{
delete(now);
cout << "已删除该学生\n";
first = last;
}
}
return(* this);
}
void sumax_min(void)
{
if(first == last)
cout << "该学生管理系统为空\n";
else
{
float max = first -> sum;
float min = max;
cLink *now = first;
cLink *min_1 = first;
cLink *max_1 = first;
while(now != last)
{
if(now->sum > max)
{
max = now->sum;
max_1 = now;
}
else if(now->sum < min)
{
min = now->sum;
min_1 = now;
}
now = now->next;
}
cout << "总分最高的学生信息为:\n";
max_1->disp();
cout << "总分最低的学生信息为:\n";
min_1->disp();
}
}
int count(void)
{
int i;
cLink *now = first;
for(i = 0; now != last ;i++)
now = now->next;
cout << "学生个数为:" << i << endl;
return i;
}
};
定义主函数:
int main()
{
cout << "|*------------------学生信息管理系统--------------------*|" << endl;
cout << "| 输入1:插入节点 |" << endl;
cout << "| 输入2:删除节点(姓名) |" << endl;
cout << "| 输入3:删除节点(学号) |" << endl;
cout << "| 输入4:查找节点(姓名) |" << endl;
cout << "| 输入5:查找并显示总成绩最高和最低的学生信息 |" << endl;
cout << "| 输入6:统计链表中的学生人数 |" << endl;
cout << "| 输入7:对链表节点按总成绩从高到低排序 |" << endl;
cout << "| 输入8:退出系统 |" << endl;
cout << "|*------------------------------------------------------*|" << endl;
cLink stu1,stu2;
cList mang;
int i,x;
char y[20];
cout << "您选择的服务是:";
cin >> i;
while(i<8 && i>0)
{
switch(i)
{
case 1:
stu1.input();
mang.Append(stu1);
break;
case 2:
cout << "\n请输入要删除的学生的姓名:";
scanf("%s",y);
mang.Delete(y);
break;
case 3:
cout << "\n请输入要删除的学生的学号:";
cin >> x;
mang.Delete(x);
break;
case 4:
cout<<"\n请输入要查找的学生的姓名:";
scanf("%s",y);
mang.find(y);
break;
case 5:
cout << "\n请输入要查找的学生的学号:";
cin >> x;
mang.find(x);
break;
case 6:
mang.sumax_min();
break;
case 7:
mang.count();
}
cout << "\n可继续选择服务序号:" ;
cin >> i;
}
cout << "程序执行完毕!\n";
return 0;
}
总结:跟着程序走一走,总能发现问题的