Definition of the linked list node
1, before attempting char * name; gets (name) ; then to char name [20] or a little, finally using this old scanf: scanf ( "% s", name);
2, the original string cout function can be directly output
3, it is not ye solid basic skills, there are many differences in understanding, for example, to write a list I'm still wondering what is next ...
later I found shoved in the definition of the node , there is no real point to their own assignment, and then I was last here at the end of last assignment was not real, but I saw a post using a 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;
}
} ;
Then define the list:
1 for the first time this is written: cList & Append (const cLink & x) ( on materials copied to) ...
when tested in the later primary function, and has written two node object: Clist stu1, stu2 ;
these two objects are assigned in the linked list, the program is no problem.
But later wrote swich statement, because only one object repeatedly with a plug can be found, into the second to die. Turn to insert the above functions, it is estimated that "&" problem, really deleted just fine.
2, Delete function is also to pay attention to a variety of situations: an empty queue, could not find, only one student, normal in most cases.
3, void sumax_min (void) function:
CLINK * now = First;
CLINK * MIN_1 = First; // Actually, this did not write, wondering for a while will be assigned
cLink * max_1 = first;
but in fact inevitable does not work, because there may be people will not fundamentally into the following if statement ... (small experience: running garbled, very likely no assignment!)
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;
}
};
The definition of the main function:
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;
}
Summary: follow the program around and always find problems
as well as higher useful than reading a book ... But the book is more useful complete machine Kankan