使用结构体实现学生信息管理系统

来源:互联网 发布:bi数据平台 编辑:程序博客网 时间:2024/05/22 11:57
#include"stdio.h"#include"malloc.h"#define format "%d %s %s %d"struct student{    int code;    char name[10];    char sex[3];    int age;    struct student *next;};//初始化头节点struct student *init(){    struct student *head = NULL;    head = (struct student *)malloc(sizeof(struct student));    if(head){        printf("内存初始化成功!!\n");        head->next=NULL;        return head;    }else{        printf("内存初始化失败!!\n");        return NULL;    }}//插入int insert(struct student  *head){    struct student *p=head;    struct student *p1;    while(p->next!=NULL){//所指的地址不为ull,就向后移动        p=p->next;    }    p1=(struct student *)malloc(sizeof(struct student ));    if(p1){        printf("新节点开辟成功!\n");        printf("请输入学号、姓名、性别、年龄:");        scanf(format,&(p1->code),p1->name,p1->sex,&(p1->age));        p1->next=NULL;//最后一个节点地址域设为ull        p->next=p1;//接上        return 1;    }else{        printf("新节点开辟 失败!\n");        return 0;    }}//查询void  print(struct student *head){    struct student *p=head;    while(p->next!=NULL){            p=p->next;            printf(format,p->code,p->name,p->sex,p->age);            printf("\n");    }}//修改int update(struct student *head,int code){    struct student *p=head;    while(p->next!=NULL){//遍历,匹配出对应的code,则输出        p=p->next;        if(p->code==code){            printf("请输入修改后的值:");            scanf("%s %s %d",p->name,p->sex,&(p->age));            return 1;        }        if(p->next==NULL){//到了最后一个节点,看他的指针域为NULl,表示没有该人            printf("没有该人!");            return 0;        }    }}//删除int del(struct student *head,int code){    struct student *p1=head;    struct student *p2 = head;    while(p1->next!=NULL){        p2=p1->next;        if(p2->code==code){                if(p2->next==NULL){//最后一个                    p1->next=NULL;                    free(p2);                }else{                    p1->next=p1->next->next;                    free(p2);                }                return 1;        }        if(p2->next==NULL){//            printf("没有该人!");            return 0;        }        p1=p1->next;    }}void destroy(struct student *head){    struct student *p=head;    struct student *q=p->next;    while(q->next!=NULL){            free(p);            printf("已释放!");            p=q;            q=q->next;    }    free(p);    printf("已释放!");}main(){    int flag = 1;    struct student *head=init();    while(flag){        printf("1---学生信息添加\n");        printf("2---学生信息查询\n");        printf("3---学生信息修改\n");        printf("4---学生信息删除\n");        printf("5---学生信息退出\n");        int sel;        printf("请输入你的选择:");        scanf("%d",&sel);        switch(sel){        case 1:        {            int f= insert(head);            if(f){                printf("添加成功!\n");            }else{                printf("添加失败!\n");            }        }            break;        case 2:            {                print(head);            }            break;        case 3:            {                printf("请输入code:");                int code ,f1;                scanf("%d",&code);                f1= update(head,code);                if(f1){                    printf("修改成功!\n");                }else{                    printf("修改失败!\n");                }            }            break;        case 4:            {                printf("请输入删除的code:");                int code ,f2;                scanf("%d",&code);                f2= del(head,code);                if(f2){                    printf("删除成功!\n");                }else{                    printf("删除失败!\n");                }            }            break;        case 5:            destroy(head);            flag=0;            break;        default:            printf("输入有误!");        }    }}

1 0
原创粉丝点击