| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 956 人关注过本帖
标题:创建一个学生链表,进行链表的插入、删除、查找操作
只看楼主 加入收藏
ch1776164108
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-3-31
收藏
 问题点数:0 回复次数:1 
创建一个学生链表,进行链表的插入、删除、查找操作
#include <iostream>
#include <string>
using namespace std;
struct student
{
    int ID;       //顺序
    long number;//学号
    string name;//学生姓名
    string sex; //性别
    int age; //年龄
    float score; //成绩
    student *next;
};
student *head;
student *Create()   //创建链表:初始化(当学号为 '0'时停止)
{
    student *p1;
    student *p2;
    p1=new student;
    cin>>p1->number>>p1->name>>p1->sex>>p1->age>>p1->score;
    head=NULL;
    p2=p1;
    while(p1->number!=0)
    {
        if(head==NULL)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=new student;
        cin>>p1->number>>p1->name>>p1->sex>>p1->age>>p1->score;
    }
    p2->next=NULL;
    delete p1;
    return(head);
}
int Length(student *head)   //计算学生总数
{
    int length=0;
    while(head)
    {
        length++;
        head=head->next;
    }
    return length;
}
void Search(student *head,long key)//按学号查找学生信息
{
    student *p;
    if(head==NULL)
    {
        cout<<endl<<"空表,不能查找。"<<endl;
        return ;
    }
    if(head->number==key)
    {
        cout<<"你查找的学生的信息为:"<<endl;
        cout<<"\t"<<"学号"<<"\t"<<"学生姓名"<<"\t"
         <<"性别""\t"<<"年龄"<<"\t"<<"成绩"<<endl;
        cout<<"\t"<<head->number<<"\t"<<head->name<<"\t\t"
        <<head->sex<<"\t"<<head->age<<"\t"<<head->score<<endl;
        return ;
    }
    for(p=head;p->next;p=p->next)
    {
        if(p->next->number==key)
        {
            cout<<"你查找的学生的学生信息为:\n";
            cout<<"\t"<<"学号"<<"\t"<<"学生姓名"<<"\t"
               <<"性别"<<"\t"<<"年龄"<<"\t"<<"成绩"<<endl;
            cout<<"\t"<<p->next->number<<"\t"<<p->next->name<<"\t\t"
               <<p->next->sex<<"\t"<<p->next->age<<"\t"<<p->next->score<<endl;
            return ;
        }
    }
    cout<<"\n\t没有学号为"<<" "<<key<<" 的学生。\n";
}
void Insert(student *&head,student *stu)  //插入操作
{
    if(head==NULL)
    {
        head=stu;
        stu->next=NULL;
        cout<<endl<<"插入成功";
        return ;
    }
    if(head->number>stu->number)
    {
        stu->next=head;
        head=stu;
        cout<<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<<"插入成功";
}
void Delete(student* &head,long number)//删除操作
{
student *p1;
if(!head)
{
    cout<<"空表,不能进行删除操作。"<<endl;
    return ;
}
if(head->number==number)
{
    p1=head;
    head=p1->next;
    delete p1;
    cout<<number<<" 号学生信息已经被删除。";
    return ;
}
for(student *p2=head;p2->next;p2=p2->next)
{
    if(p2->next->number==number)
    {
     p1=p2->next;
     p2->next=p1->next;
     delete p1;
     cout<<number<<" 号学生信息已经被删除。"<<endl;
     return ;
    }
}
cout<<number<<" 不能找到。\n";
return ;
}
void Display(student *head)             //显示链表信息
{
    if(head==NULL)
    {
        cout<<"空表,不能输出"<<endl;
        return ;
    }
    cout<<"\t"<<"顺序"<<"\t"<<"学号"<<"\t"<<"学生姓名"<<"\t"
       <<"性别"<<"\t"<<"年龄"<<"\t"<<"成绩"<<endl;
    int ID=1;
    while(head)
    {
        head->ID=ID;
    cout<<"\t"<<head->ID<<"\t"<<head->number<<"\t"<<head->name<<"\t\t"
    <<head->sex<<"\t"<<head->age<<"\t"<<head->score<<endl;

        head=head->next;
        ID++;
    }
}
void main()
{
    long number,key;
    char c;
    char choice;
    cout<<"操作序号1.由学号查询学生的信息2.插入一名学生信息。3.删除一名学生信息。";
    cout<<endl<<"请输入学生链表学生的学号、学生姓名、性别、年龄、成绩(学号为'0'时结束)"<<endl;      
    head=Create();                        //初始化学生成绩表
    cout<<endl<<Length(head)<<"名学生的信息:\n";
    Display(head);
    for(;;)//for_start
    {
     loop:cout<<"\n是否执行操作(Y/N):";
     cin>>c;
     if(c=='y'||c=='Y')//if_start
     {
         cout<<"操作序号1.由学号查询学生的信息2.插入一名学生信息。3.删除一名学生信息。";
         cout<<endl<<"请选择( 1 、2、 3 ):";     //选择一个序号并执行操作
         cin>>choice;
         switch(choice)//switch_start
         {
         case '1':                        //查找操作
             cout<<"\n请输入你要查找的学号:";
             cin>>key;
             if(head==NULL)
             {
                 cout<<"\n空表,不能查找\n";
                 goto loop ;
             }
             Search(head,key);
             break;
         case '2':                        //插入操作
             student *in;
             in=new student;
             cout<<"\n输入你要插入的学号、学生姓名、性别、年龄、成绩:";
             cin>>in->number>>in->name>>in->sex>>in->age>>in->score;
             Insert(head,in);
             cout<<endl<<Length(head)<<"名学生的信息:\n";
             Display(head);
             break;
         case '3':                        //删除操作
             cout<<"\n请输入你要删除的学生的学号:";
             cin>>number;
             Delete(head,number);
             cout<<endl<<"\t"<<Length(head)<<"名学生的信息:\n";
             Display(head);
             break;
         default:
             cout<<"\n\t\t Error Selection .\n";
             break;
         }//End switch
     }//End if
     else break;
    }//End for
}//end main
搜索更多相关主题的帖子: head name include Create number 
2012-03-31 11:22
天涯海角sxl
Rank: 2
等 级:论坛游民
帖 子:20
专家分:20
注 册:2012-2-20
收藏
得分:0 
2012-03-31 12:46
快速回复:创建一个学生链表,进行链表的插入、删除、查找操作
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019196 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved