链表的创建、添加、删除、修改、查询(C++)

//--------------------------------------------------------------------------------------------
//---------------------------------- 学生成绩管理 --------------------------------------------
// 软件说明:
//        如果稍加改动,可以用来管理学生的考试成绩。(根据实际情况:学号、姓名、科目、分数、总分
//      、排名 等等。) 限于测试程序时输入的简单性,这里就不加改进了......
// 软件功能:
// 1. 由学号查询学生的成绩。
// 2. 插入一名学生信息。
// 3. 删除一名学生信息。
// 4. 输出不及格学生的成绩。
// 5. 修改学生的成绩。
// 6. 求学生的平均成绩。
// 7. 查看某一分数段的学生成绩。
// 8. 显示表中的学生信息。(按学号排列)
// 9. 按学生的成绩排序。 (由高到底)
// 0. 将数据保存到文件[清空链表]。(D:\data.txt)
#include <iostream>
#include <iomanip>
#include <fstream.h>


struct student
{
long number; //学号
float score; //分数
int ID;        //顺序
int Order;     //名次(按成绩)
student *next;
};
student *head;

//Inlitialize a list (End while the student's number is '0')
//---------------创建链表:初始化---------
student *Create()
{
student *p;
student *end;
p=new student;
cin>>p->number>>p->score;
head=NULL;
end=p;
while(p->number!=0)
{
    if(head==NULL)
     head=p;
    else
     end->next=p;
     end=p;
    p=new student;
    cin>>p->number>>p->score;
}
end->next=NULL;
delete p;
return(head);
}

//Delete a student's information from the list----删除操作----
void Delete(struct student* &head,long number)
{
student *p;
if(!head)
{
    cout<<endl<<"空表。不能进行删除操作。"<<endl;
    return ;
}
if(head->number==number)
{
    p=head;
    head=p->next;

    delete p;
    cout<<endl<<number<<" 已经被删除。\n";
    return ;
}
for(student *end=head;end->next;end=end->next)
{
    if(end->next->number==number)
    {
     p=end->next;
     end->next=p->next;
     delete p;
     cout<<endl<<number<<" 已经被删除。"<<endl;
     return ;
    }
}
cout<<number<<" 不能找到。\n";
return ;
}
//显示链表信息
void Display(student *head)
{
if(head==NULL)
{
    cerr<<"空表,不能输出..."<<endl;
    return ;
}
cout<<setw(20)<<"顺序"<<setw(20)<<"学号"<<setw(20)<<"成绩"<<endl;
int ID=1;
while(head)
{
    head->ID=ID;
    cout<<setw(20)<<head->ID<<setw(20)<<head->number<<setw(20)<<head->score<<endl;
    head=head->next;
    ID++;
}

}

//Insert a student's information into the list-------插入操作-------
void Insert(student *&head,student *stu)
{
if(head==NULL)
{
    head=stu;
    stu->next=NULL;
    cout<<endl<<"The command completed successfully..."<<endl;
    return ;
}
if(head->number>stu->number)
{
    stu->next=head;
    head=stu;
    cout<<endl<<"The command completed successfully..."<<endl;
    return ;
}
student *p=head;

while(p->next&&p->next->number<stu->number)
    p=p->next;
stu->next=p->next;
p->next=stu;
cout<<endl<<"The command completed successfully..."<<endl;
}

//Display the total number od students----学生总数-----
int Length(student *head)
{
int length=0;
while(head)
{
    length++;
    head=head->next;
}
return length;
}

//Display the average score-----显示平均成绩----
float Average(student *head)
{
int n=Length(head);//Get the total number of students
float sum=0,aver;

for(int i=1;i<=n;i++)
{

    sum=sum+head->score;
    head=head->next;

}
aver=sum/n;
return aver;

}

//search for a student's score across his identify number---按学号查找成绩---
void Search(student *head,long key)
{
student *p;
if(head==NULL)
{
    cout<<endl<<"空表,不能查找。"<<endl;
    return ;
}
if(head->number==key)
{
    cout<<"你查找的学号、成绩为:"<<endl;
    cout<<head->number<<"\t"<<head->score<<endl;
    cout<<endl<<"The command completed successfully..."<<endl;
    return ;

}

for(p=head;p->next;p=p->next)
{
    if(p->next->number==key)
    {
     cout<<"\t\t\t你查找的学生的学号、成绩为:\n";
     cout<<"\t\t\t"<<p->next->number<<"\t"<<p->next->score<<endl;
     cout<<endl<<"The command completed successfully..."<<endl;
     return ;
    }

}
cout<<"\n\t没有学号为"<<" "<<key<<" 的学生。\n";
}

//Alter a student's score across his ID-----按学号修改学生的成绩------
void Alter(student *head,long number,float score)
{
student *p;
if(head==NULL)
{
    cout<<"\n\t空表,不能修改。\n";
    return ;
}
if(head->number==number)
{
    head->score=score;
    cout<<endl<<"The command completed successfully..."<<endl;
    return ;
}
for(p=head;p->next;p=p->next)
{
    if(p->next->number==number)
    {
     p->next->score=score;
     cout<<endl<<"The command completed successfully..."<<endl;
     return ;
    }
}
cout<<"\n\t没有学号为 "<<number<<" 的学生。\n";
}

//Maximum and minimum score-----------学生成绩的最高分和最低分----------
void Maxmin(student *head)
{
long maximum,minimum;
float max,min;
if(!head)
{//空表,报错。
    cout<<"\nlist null.Can not find the maximum score.\n";
    return ;
}
for(max=min=head->score,maximum=minimum=head->number;head->next;head=head->next)
{

    if(head->next->score>max)
    {
     max=head->next->score;
     maximum=head->next->number;
    }
    else if(head->next->score<min)
    {
     min=head->next->score;
     minimum=head->next->number;
    }
}
cout<<"\n\t\t学号"<<"\t分数";
cout<<"\n\t最高分: "<<maximum<<"\t"<<max;
cout<<"\n\t最低分: "<<minimum<<"\t"<<min;
}

//Order by score----------- 起泡法排序-------------
student OderByScore(student *&head)
{
student *h,*p,*q,*r,*s;
int i;
int n;
n=Length(head);
for(i=0;i<n;i++)
{
    h=p=new student;
    p->next=head; //p-------->head
    while(p->next)
    { //p-------->
     q=p->next; //r--------->q
     r=p; //------->q-------->q->next
     while(q->next)
     {
      if(q->score<q->next->score)//->r--------->q----->
       r=q;
      q=q->next;
     }
     if(r!=p)
     {
      s=r->next; ///-->r------>s------>
      r->next=s->next;
      s->next=p->next;
      p->next=s;
     }
     p=p->next;
    }
    head=h->next;
    delete h;
}
return(*head);
}

//按学号排列学生(同上)
student OderByID(student *&head)
{
student *h1,*p,*q,*r,*s;
int i;
int n;
n=Length(head);
for(i=0;i<n;i++)
{
    h1=p=new student;
    p->next=head; //p-------->head
    while(p->next)
    { //p-------->
     q=p->next; //r--------->q
     r=p; //------->q-------->q->next
     while(q->next)
     {
      if(q->number>q->next->number)//->r--------->q----->
       r=q;
      q=q->next;
     }
     if(r!=p)
     {
      s=r->next; ///-->r------>s------>
      r->next=s->next;
       s->next=p->next;
      p->next=s;
     }
     p=p->next;
    }
    head=h1->next;
    delete h1;
    }
return(*head);
}
//Display the students whose's score below 60-----输出不及格的学生成绩-----
void BetweenAnd(student *head)
{
if(head==NULL)
{
    cerr<<"空表,不能执行操作..."<<endl;
    return ;
}
else
{
    while(head)
    {
     if(head->score<60)
     {
      cout<<setw(20)<<head->ID<<setw(20)<<head->number<<setw(20)<<head->score<<endl;
      head=head->next;
     }
     else
      head=head->next;
    }
}
}
//Oder By Score ----------按成绩排列-------
student DisplayByScores(student *head)
{
int i=1;
OderByScore(head);

while(head)
{
    head->Order=i++;
    cout<<setw(20)<<head->Order<<setw(20)<<head->number<<setw(20)<<head->score<<endl;
    head=head->next;
}
return(*head);
}
void SaveToFile(student *&head)
{
ofstream OutFile("data.txt");
if(OutFile.fail())
{//如果不能打开或创建文件,报错。
    cerr<<endl<<"-------Error: 不能打开或创建文件。"<<endl;
    return ;
}
//打开成功
OutFile<<setw(20)<<"编号"<<setw(20)<<"学号"<<setw(20)<<"分数"<<endl;
int i=1;
while(head)
{
    OutFile<<setw(20)<<i++<<setw(20)<<head->number<<setw(20)<<head->score<<endl;
    head=head->next;
}
cout<<endl<<"The command completed successfully..."<<endl;
OutFile.close();

}

//Function main
void main()
{
long number,key;
char c;
char choice;

//Welcome information
cout<<"\t\t\t\tBy zhigf@126.com \n\t\tCopyright"
    <<" Ambitious·Administrator All Rights Reserved\n"
    <<"--------------------------------Test Edition------------------------------------\n"
    <<"***********************************欢迎使用软件*********************************\n";
//功能描述
cout<<"\n软件功能:\n"
    <<"1.由学号查询学生的成绩                             |╬╬╬╬╬╬╬|\n"
    <<"2.插入一名学生信息。                               |╬            ╬|\n"
    <<"3.删除一名学生信息。                               |╬ 版权所有 ╬|\n"
    <<"4.输出不及格学生的成绩。                           |╬ 不得抄袭 ╬|\n"
    <<"5.查看最高分与最低分。                             |╬ 如有雷同 ╬|\n"
    <<"6.求学生的平均成绩。                               |╬ 实属荣幸 ╬|\n"
    <<"7.修改学生的成绩。                                 |╬ 若要转载 ╬|\n"
    <<"8.显示表中的学生信息。(按学号排列)                 |╬ 注明出处 ╬|\n"
    <<"9.按学生的成绩排序(由高到底) 。                    |╬            ╬|\n"
    <<"0.保存到文件并清空链表(data.txt)。                 |╬╬╬╬╬╬╬|\n";

for(int t=1;t<=80;t++)
    cout<<"*";
cout<<endl;

//初始化学生成绩表
cout<<"请输入学号、成绩(学号为'0'时结束):";
for(int i=1;i<=80;i++)
    cout<<"_";
cout<<endl;
head=Create();
cout<<endl<<"\n\t"<<Length(head)<<"名学生的信息:\n";
Display(head);
//execute this software if the user enter 'y' or 'Y'
for(;;)//for_start
{
loop:cout<<"\n是否执行操作(Y/N):\n";
    cin>>c;
    if(c=='y'||c=='Y')//if_start
    {
     //Function of this sotfware
     cout<<"\n软件功能:\n"
      <<"1.由学号查询学生的成绩。\n"
      <<"2.插入一名学生信息。\n"
      <<"3.删除一名学生信息。\n"
      <<"4.输出不及格学生的成绩。\n"
      <<"5.查看最高分与最低分。\n"
      <<"6.求学生的平均成绩。\n"
      <<"7.修改学生的成绩。\n"
      <<"8.显示表中的学生信息。(按学号排列)\n"
      <<"9.按学生的成绩排序(由高到底)\n"
      <<"0.将数据保存到文件并清空链表。("<<"data.txt)\n";
    //select one integer and execute the command
    cout<<"\n请选择( 1 、2、 3 、4、5 、6 、7、8 或 9 ):\n";
    cin>>choice;
    switch(choice)//switch_start
    {
     //search for score
     case '1':
       cout<<"\n请输入你要查找的学号:\n";
       cin>>key;
       if(head==NULL)
       {
        cout<<"\n\t空表,不能查找...\n";
        goto loop ;
       }
       Search(head,key);
       break;
     //Insert into the list
     case '2':
       student *in;
       in=new student;
       cout<<"\n输入你要插入的学号、成绩:\n";
       cin>>in->number>>in->score;
       Insert(head,in);
       cout<<endl<<"\n\t"<<Length(head)<<"名学生的信息:\n";
       Display(head);
       break;
     //Delete a student's information
     case '3':
       cout<<"\n请输入你要删除的学号:\n";
       cin>>number;
       Delete(head,number);
       cout<<endl<<"\n\t"<<Length(head)<<"名学生的信息:\n";
       Display(head);
       break;
     //Display the students whose score between start and end
     case '4':
       BetweenAnd(head);
       break;
     //Max and min score int the list
     case '5':
       Maxmin(head);
       break;
     //Get the average score
     case '6':
       if(head==NULL)
       {
        cout<<"\n\t空表,不能输出平均成绩...\n";
        goto loop ;
       }
       cout<<endl<<"\n\t"<<Length(head)<<"名学生的平均成绩: \n"<<"\t"<<Average(head)<<endl;
       break;
     //Alter the student's score
     case '7':
       long number;
       float score;
       cout<<"\n请输入你要修改的学生的学号及其成绩:\n";
       cin>>number>>score;
       Alter(head,number,score);
       cout<<endl<<"\n\t"<<Length(head)<<"名学生的信息:\n";
       Display(head);
       break;
     //Display By ID
     case '8':
       if(head==NULL)
       {
        cout<<"\n\t空表,不能进行排序...\n";
        goto loop ;
       }
       OderByID(head);
       cout<<endl<<"\n\t"<<Length(head)<<"名学生的信息:\n";
       Display(head);
       break;
     //Display by Scores
     case '9':
       if(head==NULL)
       {
        cout<<"\n\t空表,不能进行排序...\n";
        goto loop ;
       }
       OderByScore(head);
       Display(head);
       break;

     //Save to files (D:\data.txt)
     case '0':
       if(head==NULL)
       {
        cout<<"\n\t空表,不能进行保存...\n";
        goto loop;
       }
       SaveToFile(head);
       break;
     //Error infromation
     default:
       cout<<"\n\t\t-------- Error Selection ------.\n";
       break;

     }//End switch
    }//End if
    else break;
}//End for
return 0;
}
收藏  | 打印  | 字体:  -缩小  放大+    
[ x ] 请正确填写下面信息


是否保存此网页快照 是否公开此收藏

查看全部评论(115)我来说两句