您的位置:首页 > 数据库

c链表构建学生档案数据库

2017-12-09 14:19 190 查看
#include <iostream>

#include<stdlib.h>

#include <cstring>

using namespace std;

typedef struct node

{ int number;

char name[20] ;

float score;

struct node *next;

}*LinkList;

 node*q;

LinkList head=(node*)malloc(sizeof(node));

node*p=head;

void add();

void menu();

void Search();

void Delete();

void sort(); 

int n=0;

void menu()

{int n;

cout<<"-----------请输入数字选择功能:-----------\n----------------1.添加----------------\n----------------2.查找----------------\n----------------3.删除----------------\n----------------4.排序----------------"<<endl;

cin>>n;

if(n==1)

add();

else if(n==2) 

Search();

else if(n==3) Delete();

else if(n==4) sort(); 

else cout<<"error"<<endl; menu();

}

void add(){cout<<"请输入要插入的信息" <<endl; 

q=head; 

q=(node*)malloc(sizeof(node));

cout<<"学号"<<endl;

cin>>q->number;

cout<<"姓名" <<endl;

cin>>q->name;

cout<<"成绩"<<endl;

cin>>q->score;

p->next=q;

q->next=NULL;

p=q;

n=n+1;

cout<<"n="<<n<<endl;

menu();

}

void Search()

{int m;cout<<" 请要查找的输入学号"<<endl;

cin>>m;

      node *Pointer;

      Pointer=head;int j=0;

      while(Pointer!=NULL)

      {

          if(Pointer->number==m)

          {

            cout<<"你的姓名是"<<Pointer->name<<endl;

            cout<<"你的成绩是"<<Pointer->score<<endl;

            j=1;

            break;

            

          }
  

             Pointer=Pointer->next;

      }

      if(j!=1)cout<<"查无此人"<<endl;;

      

       menu();  

}

void Delete()

{int m;cout<<" 请要删除的输入学号"<<endl;

cin>>m;

      node *Pointer;

      Pointer=head;

      while(Pointer!=NULL)

      {

          

         if(Pointer->next==NULL)

         {free(Pointer);

            

            break;

         }

        if(Pointer->next->number==m)

          {

            Pointer->next=Pointer->next->next;

            free(Pointer->next);

            break;

         }

        else cout<<"错误"<<endl; 
Pointer=Pointer->next;

     }

     n=n-1;

           

      menu()  ;

}

void sort(){cout<<" 排序"<<endl;

      node* p=NULL;  

    node* q=NULL;  

    int temp;int m; char bae[20] ;

    for(p=head;p!=NULL;p=p->next)  

    {  

        for(q=p->next;q!=NULL;q=q->next)  

        {  

            if(p->score<q->score)  

            {  

                temp=p->score;  

                p->score=q->score;  

                q->score=temp; 

                

                m=p->number;  

                p->number=q->number;  

                q->number=m; 
strcpy(bae,p->name);
strcpy(p->name,q->name);
strcpy(q->name,bae);

                

            }  

        }  

    } node* w;

    int k=1; 

    for(w=head;w->next!=NULL;w=w->next)  

 {cout<<"----------------名次" <<k<<"----------------"<<endl;

 cout<<"姓名"<<w->name<<endl;

 cout<<"学号"<<w->number<<endl;

 cout<<"成绩"<<w->score<<endl;k=k+1;}menu()  ;



int main() {menu();
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: