📄 clist.cpp
字号:
#include"StdAfx.h"
#include"clist.h"
#include<string.h>
#include<stdio.h>
clist::clist()
{
head=NULL;
length=0;
}
clist::clist(const clist &q)//拷贝构造函数
{
head=q.head;
length=q.length;
}
clist::~clist()//析构函数
{
if(head!=NULL)
delete head;
}
void clist::setheadlength(cstudent *ohead,int leng)//给链表的头结点和长度赋值
{
head=ohead;
length=leng;
}
cstudent *clist::gethead()//得到链表的头指针
{
return head;
}
int clist::getlength()//得到链表的长度
{
return length;
}
void clist::readfile(CString filename)//从文件中读取数据
{
FILE *fp;
fp=fopen(filename,"r");
cstudent *lattertemp,*temp,*note,*samehead,*ohead=head;//samehead用于检验链表中是否已存在该学号的学生
int flag=0;
int record;//记录被检验的学生的个数
char *name,*sex,*number,*major;
name=new char[15];
sex=new char;
number=new char[10];
major=new char[15];
double math,cpp,english;
if(fp!=NULL)
{
if(head!=NULL)
{
for(int i=0;i<length-1;i++)
ohead=ohead->next;//调到当前链表的结尾
note=ohead;//用于记录当前链表的结尾,验证时否从文件中读取了数据
while(1)
{
record=0;//每次都record把初始化为0
samehead=head;//切记每次都要让samehead指向头节点
flag=fscanf(fp,"%s %s %s %s %lf %lf %lf\n",name,sex,number,major,&math,&cpp,&english);
if(flag==EOF)
break;
temp=new cstudent(name,sex,number,major,math,cpp,english);
for(int j=0;j<length;j++)
{
if(strcmp(temp->getnumber(),samehead->getnumber())==0)
break;
else
{
samehead=samehead->next;
record++;
}
}
if(record<length)
continue;
ohead->next=temp;
ohead=ohead->next;
length++;
}
ohead->next=NULL;
if(note->next!=NULL)
AfxMessageBox("已从文件中读取了数据!");
else
AfxMessageBox("抱歉,文件中没有数据或已读!");
fclose(fp);
}
else
{
while(1)
{
record=0;
samehead=head;
flag=fscanf(fp,"%s %s %s %s %lf %lf %lf\n",name,sex,number,major,&math,&cpp,&english);
if(flag==EOF)
break;
temp=new cstudent(name,sex,number,major,math,cpp,english);
for(int m=0;m<length;m++)
{
if(strcmp(temp->getnumber(),samehead->getnumber())==0)
break;
else
{
samehead=samehead->next;
record++;
}
}
if(record<length)
continue;
if(length==0)
lattertemp=head=temp;
else
lattertemp->next=temp;
lattertemp=temp;
length++;
}
if(head!=NULL)
AfxMessageBox("已从文件中读取了数据!");
else
AfxMessageBox("抱歉,文件中没有数据或已读!");
fclose(fp);
}
}
else
AfxMessageBox("抱歉,找不到文件");
}
void clist::savelist(CString filename)//保存链表
{
FILE *fp;
cstudent *temp=head;
fp=fopen(filename,"w");
if(fp==NULL)
{
AfxMessageBox("抱歉,文件无法打开!");
return ;
}
do
{
fprintf(fp,"%s %s %s %s %4.1lf %4.1lf %4.1lf\n",temp->getname(),temp->getsex(),
temp->getnumber(),temp->getmajor(),temp->getmath(),temp->getcpp(),temp->getenglish());
temp=temp->next;
}while(temp!=NULL);
AfxMessageBox("保存成功!");
fclose(fp);
}
void clist::addtail(cstudent *temp)//在链表尾部增加结点
{
cstudent *last=head;
if(head==NULL)
{
AfxMessageBox("学生链表为空,您所添加的学生是第一个!");
head=temp;
length++;
temp->next=NULL;
}
else
{
while(last->next!=NULL)
last=last->next;
last->next=temp;
length++;
temp->next=NULL;
}
}
void clist::addhead(cstudent *temp)//在链表头部增加结点
{
if(head==NULL)
{
AfxMessageBox("学生链表为空,您所添加的学生是第一个!");
head=temp;
length++;
temp->next=NULL;
}
else
{
temp->next=head;
head=temp;
length++;
}
}
cstudent *clist::namesearch(char *name)//按姓名查询
{
cstudent*temp=head;
if(head==NULL)
{
AfxMessageBox("抱歉,没有学生数据,不能查询!");
return NULL;
}
else
{
while(strcmp(name,temp->getname())&&temp->next!=NULL)
{
temp=temp->next;
}
if(strcmp(name,temp->getname())==0)
return temp;//带回被查询到的学生的信息
else
{
AfxMessageBox("抱歉,此姓名的学生不存在!");
return NULL;
}
}
}
cstudent *clist::numbersearch(char *number)//按学号查询
{
cstudent*temp=head;
if(head==NULL)
{
AfxMessageBox("抱歉,链表是空的!");
return NULL;
}
else
{
while(strcmp(number,temp->getnumber())&&temp->next!=NULL)
{
temp=temp->next;
}
if(strcmp(number,temp->getnumber())==0)
return temp;//带回被查询到的学生的信息
else
{
AfxMessageBox("抱歉,此学号的学生不存在!");
return NULL;
}
}
}
void clist::namesort(int sortway)//按姓名排序
{
cstudent *temp,*lattertemp,*lasttemp,*emphead,*emptail;
cstudent *ohead=head;
emphead=new cstudent();
emptail=new cstudent();
emphead->next=head;
while(ohead->next!=NULL)
{ohead=ohead->next;}
ohead->next=emptail;
if(sortway==0)
{
for(int i=0;i<length-1;i++)
{
lasttemp=emphead;
lattertemp=emphead->next;
temp=lattertemp->next;
for(int j=i+1;j<length;j++)
{
if(strcmp(lattertemp->getname(),temp->getname())>0)
{
lasttemp->next=temp;
lattertemp->next=temp->next;
temp->next=lattertemp;
lasttemp=lasttemp->next;
temp=lattertemp->next;
}
else
{
lasttemp=lasttemp->next;
lattertemp=lattertemp->next;
temp=temp->next;
}
}
}
head=emphead->next;
ohead=head;
while(ohead->next->next!=NULL)
ohead=ohead->next;
ohead->next=NULL;
if(emphead!=NULL)
delete emphead;
if(emptail!=NULL)
delete emptail;
}
else
{
for(int i=0;i<length-1;i++)
{
lasttemp=emphead;
lattertemp=emphead->next;
temp=lattertemp->next;
for(int j=i+1;j<length;j++)
{
if(strcmp(lattertemp->getname(),temp->getname())<0)
{
lasttemp->next=temp;
lattertemp->next=temp->next;
temp->next=lattertemp;
lasttemp=lasttemp->next;
temp=lattertemp->next;
}
else
{
lasttemp=lasttemp->next;
lattertemp=lattertemp->next;
temp=temp->next;
}
}
}
head=emphead->next;
ohead=head;
while(ohead->next->next!=NULL)
ohead=ohead->next;
ohead->next=NULL;
if(emphead!=NULL)
delete emphead;
if(emptail!=NULL)
delete emptail;
}
}
void clist::numbersort(int sortway)//按学号排序
{
cstudent *temp,*lattertemp,*lasttemp,*emphead,*emptail;
cstudent *ohead=head;
emphead=new cstudent();
emptail=new cstudent();
emphead->next=head;
while(ohead->next!=NULL)
{ohead=ohead->next;}
ohead->next=emptail;
if(sortway==0)
{
for(int i=0;i<length-1;i++)
{
lasttemp=emphead;
lattertemp=emphead->next;
temp=lattertemp->next;
for(int j=i+1;j<length;j++)
{
if(strcmp(lattertemp->getnumber(),temp->getnumber())>0)
{
lasttemp->next=temp;
lattertemp->next=temp->next;
temp->next=lattertemp;
lasttemp=lasttemp->next;
temp=lattertemp->next;
}
else
{
lasttemp=lasttemp->next;
lattertemp=lattertemp->next;
temp=temp->next;
}
}
}
head=emphead->next;
ohead=head;
while(ohead->next->next!=NULL)
ohead=ohead->next;
ohead->next=NULL;
if(emphead!=NULL)
delete emphead;
if(emptail!=NULL)
delete emptail;
}
else
{
for(int i=0;i<length-1;i++)
{
lasttemp=emphead;
lattertemp=emphead->next;
temp=lattertemp->next;
for(int j=i+1;j<length;j++)
{
if(strcmp(lattertemp->getnumber(),temp->getnumber())<0)
{
lasttemp->next=temp;
lattertemp->next=temp->next;
temp->next=lattertemp;
lasttemp=lasttemp->next;
temp=lattertemp->next;
}
else
{
lasttemp=lasttemp->next;
lattertemp=lattertemp->next;
temp=temp->next;
}
}
}
head=emphead->next;
ohead=head;
while(ohead->next->next!=NULL)
ohead=ohead->next;
ohead->next=NULL;
if(emphead!=NULL)
delete emphead;
if(emptail!=NULL)
delete emptail;
}
}
void clist::allscoresort(int sortway)//按总成绩排序
{
cstudent *temp,*lattertemp,*lasttemp,*emphead,*emptail;
cstudent *ohead=head;
emphead=new cstudent();
emptail=new cstudent();
emphead->next=head;
while(ohead->next!=NULL)
{ohead=ohead->next;}
ohead->next=emptail;
if(sortway==0)
{
for(int i=0;i<length-1;i++)
{
lasttemp=emphead;
lattertemp=emphead->next;
temp=lattertemp->next;
for(int j=i+1;j<length;j++)
{
if(lattertemp->getallscore()>temp->getallscore())
{
lasttemp->next=temp;
lattertemp->next=temp->next;
temp->next=lattertemp;
lasttemp=lasttemp->next;
temp=lattertemp->next;
}
else
{
lasttemp=lasttemp->next;
lattertemp=lattertemp->next;
temp=temp->next;
}
}
}
head=emphead->next;
ohead=head;
while(ohead->next->next!=NULL)
ohead=ohead->next;
ohead->next=NULL;
if(emphead!=NULL)
delete emphead;
if(emptail!=NULL)
delete emptail;
}
else
{
for(int i=0;i<length-1;i++)
{
lasttemp=emphead;
lattertemp=emphead->next;
temp=lattertemp->next;
for(int j=i+1;j<length;j++)
{
if(lattertemp->getallscore()<temp->getallscore())
{
lasttemp->next=temp;
lattertemp->next=temp->next;
temp->next=lattertemp;
lasttemp=lasttemp->next;
temp=lattertemp->next;
}
else
{
lasttemp=lasttemp->next;
lattertemp=lattertemp->next;
temp=temp->next;
}
}
}
head=emphead->next;
ohead=head;
while(ohead->next->next!=NULL)
ohead=ohead->next;
ohead->next=NULL;
if(emphead!=NULL)
delete emphead;
if(emptail!=NULL)
delete emptail;
}
}
void clist::mathsort(int sortway)//按数学成绩排序
{
cstudent *temp,*lattertemp,*lasttemp,*emphead,*emptail;
cstudent *ohead=head;
emphead=new cstudent();
emptail=new cstudent();
emphead->next=head;
while(ohead->next!=NULL)
{ohead=ohead->next;}
ohead->next=emptail;
if(sortway==0)
{
for(int i=0;i<length-1;i++)
{
lasttemp=emphead;
lattertemp=emphead->next;
temp=lattertemp->next;
for(int j=i+1;j<length;j++)
{
if(lattertemp->getmath()>temp->getmath())
{
lasttemp->next=temp;
lattertemp->next=temp->next;
temp->next=lattertemp;
lasttemp=lasttemp->next;
temp=lattertemp->next;
}
else
{
lasttemp=lasttemp->next;
lattertemp=lattertemp->next;
temp=temp->next;
}
}
}
head=emphead->next;
ohead=head;
while(ohead->next->next!=NULL)
ohead=ohead->next;
ohead->next=NULL;
if(emphead!=NULL)
delete emphead;
if(emptail!=NULL)
delete emptail;
}
else
{
for(int i=0;i<length-1;i++)
{
lasttemp=emphead;
lattertemp=emphead->next;
temp=lattertemp->next;
for(int j=i+1;j<length;j++)
{
if(lattertemp->getmath()<temp->getmath())
{
lasttemp->next=temp;
lattertemp->next=temp->next;
temp->next=lattertemp;
lasttemp=lasttemp->next;
temp=lattertemp->next;
}
else
{
lasttemp=lasttemp->next;
lattertemp=lattertemp->next;
temp=temp->next;
}
}
}
head=emphead->next;
ohead=head;
while(ohead->next->next!=NULL)
ohead=ohead->next;
ohead->next=NULL;
if(emphead!=NULL)
delete emphead;
if(emptail!=NULL)
delete emptail;
}
}
void clist::cppsort(int sortway)//按C++成绩排序
{
cstudent *temp,*lattertemp,*lasttemp,*emphead,*emptail;
cstudent *ohead=head;
emphead=new cstudent();
emptail=new cstudent();
emphead->next=head;
while(ohead->next!=NULL)
{ohead=ohead->next;}
ohead->next=emptail;
if(sortway==0)
{
for(int i=0;i<length-1;i++)
{
lasttemp=emphead;
lattertemp=emphead->next;
temp=lattertemp->next;
for(int j=i+1;j<length;j++)
{
if(lattertemp->getcpp()>temp->getcpp())
{
lasttemp->next=temp;
lattertemp->next=temp->next;
temp->next=lattertemp;
lasttemp=lasttemp->next;
temp=lattertemp->next;
}
else
{
lasttemp=lasttemp->next;
lattertemp=lattertemp->next;
temp=temp->next;
}
}
}
head=emphead->next;
ohead=head;
while(ohead->next->next!=NULL)
ohead=ohead->next;
ohead->next=NULL;
if(emphead!=NULL)
delete emphead;
if(emptail!=NULL)
delete emptail;
}
else
{
for(int i=0;i<length-1;i++)
{
lasttemp=emphead;
lattertemp=emphead->next;
temp=lattertemp->next;
for(int j=i+1;j<length;j++)
{
if(lattertemp->getcpp()<temp->getcpp())
{
lasttemp->next=temp;
lattertemp->next=temp->next;
temp->next=lattertemp;
lasttemp=lasttemp->next;
temp=lattertemp->next;
}
else
{
lasttemp=lasttemp->next;
lattertemp=lattertemp->next;
temp=temp->next;
}
}
}
head=emphead->next;
ohead=head;
while(ohead->next->next!=NULL)
ohead=ohead->next;
ohead->next=NULL;
if(emphead!=NULL)
delete emphead;
if(emptail!=NULL)
delete emptail;
}
}
void clist::englishsort(int sortway)//按英语成绩排序
{
cstudent *temp,*lattertemp,*lasttemp,*emphead,*emptail;
cstudent *ohead=head;
emphead=new cstudent();
emptail=new cstudent();
emphead->next=head;
while(ohead->next!=NULL)
{ohead=ohead->next;}
ohead->next=emptail;
if(sortway==0)
{
for(int i=0;i<length-1;i++)
{
lasttemp=emphead;
lattertemp=emphead->next;
temp=lattertemp->next;
for(int j=i+1;j<length;j++)
{
if(lattertemp->getenglish()>temp->getenglish())
{
lasttemp->next=temp;
lattertemp->next=temp->next;
temp->next=lattertemp;
lasttemp=lasttemp->next;
temp=lattertemp->next;
}
else
{
lasttemp=lasttemp->next;
lattertemp=lattertemp->next;
temp=temp->next;
}
}
}
head=emphead->next;
ohead=head;
while(ohead->next->next!=NULL)
ohead=ohead->next;
ohead->next=NULL;
if(emphead!=NULL)
delete emphead;
if(emptail!=NULL)
delete emptail;
}
else
{
for(int i=0;i<length-1;i++)
{
lasttemp=emphead;
lattertemp=emphead->next;
temp=lattertemp->next;
for(int j=i+1;j<length;j++)
{
if(lattertemp->getenglish()<temp->getenglish())
{
lasttemp->next=temp;
lattertemp->next=temp->next;
temp->next=lattertemp;
lasttemp=lasttemp->next;
temp=lattertemp->next;
}
else
{
lasttemp=lasttemp->next;
lattertemp=lattertemp->next;
temp=temp->next;
}
}
}
head=emphead->next;
ohead=head;
while(ohead->next->next!=NULL)
ohead=ohead->next;
ohead->next=NULL;
if(emphead!=NULL)
delete emphead;
if(emptail!=NULL)
delete emptail;
}
}
void clist::deletenodname(char *name)//按姓名删除学生
{
cstudent *temp=head;
cstudent *lattertemp;
if(head==NULL)
AfxMessageBox("抱歉,链表是空的!");
else
{
while(strcmp(name,temp->getname())&&temp->next!=NULL)
{
lattertemp=temp;
temp=temp->next;
}
if(strcmp(name,temp->getname())==0)
{
if(head==temp)
{
head=temp->next;
delete temp;
}
else
{
lattertemp->next=temp->next;
delete temp;
}
length--;
AfxMessageBox("已成功删除");
}
else
AfxMessageBox("抱歉,您所要删除的学生姓名不存在!");
}
}
void clist::deletenodnumber(char *number)//按学号删除学生
{
cstudent *temp=head;
cstudent *lattertemp;
if(head==NULL)
AfxMessageBox("抱歉,链表是空的!");
else
{
AfxMessageBox("查找中,请稍后.....");
while(strcmp(number,temp->getnumber())&&temp->next!=NULL)
{
lattertemp=temp;
temp=temp->next;
}
if(strcmp(number,temp->getnumber())==0)
{
if(head==temp)
{
head=temp->next;
delete temp;
}
else
{
lattertemp->next=temp->next;
delete temp;
}
length--;
}
else
AfxMessageBox("抱歉,您所要删除的学生学号不存在");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -