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

#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;
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值