操作系统作业1

类似于写一个学生信息管理系统,里面有一些问题还没有解决,以后有时间再改一下,不过程序能把老师的要求全部跑出来,对于清内存方面还是有一些问题,这里就没贴了

#include
#include
#include
struct student{
    int sno;
    char name[10];
    int sage;
    int sclass;
    struct student *next;
};  
struct student *create_h(){
    struct student *head;
    head=(struct student*)malloc(sizeof(struct student));
    if(head==NULL){
        printf("分配头节点失败");
        return NULL;
    }
    head->next=NULL;
    return head;
}
void search(struct student *head){//查找函数 
    int num;
    printf("输入你想查找的学生学号:");
    scanf("%d",&num);
    struct student *p;
    p=head->next;
    while(p!=NULL&&p->sno!=num){
        p=p->next;
    }
    if(p==NULL){
        printf("没找到这样的学生\n");
    }
    if(p->sno==num){

        printf("学号:%d 姓名:%s 年龄:%d 班级号:%d\n",p->sno,p->name,p->sage,p->sclass);  
    }
}

void add(struct student *head){//添加函数 
    struct student *p=head,*s;
    s=(struct student*)malloc(sizeof(struct student));
    printf("输入你想添加学生的信息:学号,姓名,年龄,班级号\n");
    scanf("%d %s %d %d",&s->sno,s->name,&s->sage,&s->sclass);
    while(p->next!=NULL&&s->sno>=p->next->sno){
        p=p->next;
    }
    if(p->next==NULL){
        p->next=s;
        s->next=NULL;
    }
    else{
        p->next=s;
        s->next=p->next;
    }
    printf("学号:%d\n姓名:%s\n年龄:%d\n班级:%d\n",s->sno,s->name,s->sage,s->sclass);

}

void H_printf(struct student *head){//打印数据 
    struct student *p=head->next;
    while(p!=NULL){
        printf("%d %s %d %d\n",p->sno,p->name,p->sage,p->sclass);
        p=p->next;
    }
}

void modify(struct student *head){//改变数据 
    struct student *p;
    int num,age,cl;
    char na[10];
    printf("输入你想修改学生的学号:");
    scanf("%d",&num);
    while(p!=NULL&&p->sno!=num){
        p=p->next;
    } 
    if(p==NULL){
        printf("没找到你想修改的学生信息\n");
    }
    if(p->sno==num){
        printf("输入你想修改学生的信息:\n");
        scanf("%d %s %d %d",&p->sno,p->name,&p->sage,&p->sclass);
        printf("修改后学生信息:\n学号:%d\n姓名:%s\n年龄:%d\n班级号:%d\n",p->sno,p->name,p->sage,p->sclass);
        printf("修改成功\n");
    }
}

void delete1(struct student *head){
    struct student *p=head,*s=head;
    int num,flag=1;
    printf("输入你想删除的学生学号:\n");
    scanf("%d",&num);
    while(p!=NULL&&p->sno!=num){
        p=p->next;
        if(flag==0){
            s=s->next;
        }
        flag=0;
    }
    if(p==NULL){
        printf("没找到你想删除的学生\n");
    }
    if(p->sno==num){
        s->next=p->next;
        free(p);
        printf("删除成功!!!");
    }
}
int main(){
    struct student *head,*p,*ptail;
    head=create_h();
    int n;
    int num;

    while(1){
        printf("选择你想进行的操作:\n1.查找\n2.打印\n3.添加\n4.修改\n5.删除\n");
        scanf("%d",&n);
        switch(n){
        case 1:
            search(head);
            break;
        case 2:
            H_printf(head);
            break;
        case 3:
            add(head);
            break;
        case 4:
            modify(head);
            break;  
        case 5:
            delete1(head);
            break;

        default:
            break;  
        }
    }
    return 0;
} 

你可能感兴趣的:(刷题)