链表实现新生信息系统

#include<malloc.h>
#include<string.h>
#include<stdlib.h>

struct student{
    char name[100];
    int num;
    char sex;
    int age;
    char major;
    int classno;
    
    struct student *next;
};


struct node{
    student stu;
    struct node *next;      
};
struct node *head=NULL;


void add();
void dele();
void menu();
void change();
void select();





//增操作(信息录入操作)
void add(){
    struct node* q=(struct node*)malloc(sizeof(struct node));
    q->next = NULL;
    struct node* p=head;
    while(p->next!=NULL&&head->next!=NULL){
      p=p->next;
    }
    if(head==NULL){
    head=q;
    }
    else p->next=q;

    printf("请输入学生的名字");
    scanf("%s",&q->stu.name);

    printf("请输入学生的学号");
    scanf("%d",&q->stu.num);

    printf("请输入学生的年龄");
    scanf("%d",&q->stu.age);

    printf("请输入学生的性别");
    scanf("%s",&q->stu.sex);

    printf("请输入学生的专业");
    scanf("%s",&q->stu.major);
    
    printf("请输入学生的班级");
    scanf("%d",&q->stu.classno);
    
    printf("Congratulations!信息录入成功");
    system("pause");
    system("cls");
}





//查操作(信息查询操作) 
void select(){
    struct node* p=head;
    printf("请输入需要查找的学号: ");
    int x;
    scanf("%d",&x);
    while (1){
    if(p->stu.num==x){
            printf("姓名\t学号\t性别\t年龄\t专业\t班级\n");
            printf("%s\t%d\t%s\t%d\t%s\t%d\t\n",p->stu.name,p->stu.num,p->stu.sex,p->stu.age,p->stu.major,p->stu.classno);
            system("pause");
            system("cls");
            break;
    }
    if(p->stu.num==NULL){
            printf("sorry,查无此人!");
            break;
    }
    
    }
    p=p->next;
    
    
    
    
}


//改操作(信息修改操作) 
void change(){
    struct node* p=head;
    printf("请输入需要改的学生学号:");
    int x;
    scanf("%d",&x);
    while(1){
        if(p->stu.num==x){
            printf("%s\t %d\t %s\t %d\t %s\t %d\t",p->stu.name,p->stu.num,p->stu.sex,p->stu.age,p->stu.major,p->stu.classno);
            printf("*1姓名 *2学号 *3性别 *4年龄 *5专业 *6班级");
            printf("请输入你想修改的数据:");
            int y;
            scanf("%d",&y);
            switch(y){
                case 1:scanf("%s",p->stu.name);
                case 2:scanf("%d",p->stu.num);
                case 3:scanf("%s",p->stu.sex);
                case 4:scanf("%d",p->stu.age);
                case 5:scanf("%s",p->stu.major);
                case 6:scanf("%d",p->stu.classno);
            }
            printf("Congratulations!修改成功!");
            system("pause");
            system("cls");
            
            break;
        }
        p=p->next;
    }
    
    system("cls");
    
}

//删操作(信息删除操作)
void dele(){    
    struct node*p=head->next;
    struct node*q;
    printf("请输入你想删除的学生学号");
    int z;
    scanf("%d",&z);
    q=p;
    while(1){
     if(p->stu.num==z){
        q->next=p->next;
        free(p);
        break;
      }
      else{
          p=p->next;
      }
    
     if(p->stu.num==NULL){
    printf("Very Sorry!你输入的学号不存在!"); 
    break;
    }
    system("cls");
}



 
void menu(){
    printf("\t=============================\n");
     printf("\t*Welcome to新生信息管理系统*\n" );         
    printf("\t=============================\n");
    printf("\t   *1)信息录入              \n");        
    printf("\t   *2)信息修改              \n");        
    printf("\t   *3)信息删除              \n");        
    printf("\t   *4)信息查询              \n");    
    printf("\t=============================\n");
}
 
 
int main(){
    while(1){
    
    menu();
    printf("请输入你的选择:");
    int x;
    scanf("%d",&x);
    switch(x){
        case 1:
        add();
        break;//信息录入
        case 2:
        change();    
        break;//信息修改
        case 3:
        dele();
        break;//信息删除
        case 4:
        select();
        break;//信息查询 
        default:
        printf("你输入的数字有误error!!\n");
        }
        system("cls";)
    }
}


报错

img

修改如下,供参考:

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
struct student {
    char name[100];
    int num;
    char sex;
    int age;
    char major[32];
    int classno;
    struct student* next;
};

struct node {
    student stu;
    struct node* next;
};
struct node* head = NULL;

void add();
void Dele();
void menu();
void change();
void select();


//增操作(信息录入操作)
void add()
{
    struct node* q = (struct node*)malloc(sizeof(struct node));
    q->next = NULL;
    struct node* p = head;
    if (head == NULL) {
        head  = q;
    }
    else {
        while (p->next != NULL) { //while (p->next != NULL && head->next != NULL) {
            p = p->next;
        }
        p->next = q;
    }
    printf("请输入学生的名字:");
    getchar();
    scanf("%s", q->stu.name);  //scanf("%s", &q->stu.name);
    printf("请输入学生的学号:");
    scanf("%d", &q->stu.num);
    printf("请输入学生的年龄:");
    scanf("%d", &q->stu.age);
    printf("请输入学生的性别(男:F,女:M):");
    getchar();
    scanf("%c", &q->stu.sex);  //scanf("%s", &q->stu.sex); 
    printf("请输入学生的专业:");
    getchar();
    scanf("%s", q->stu.major); //scanf("%s", &q->stu.major);
    printf("请输入学生的班级:");
    scanf("%d", &q->stu.classno);
    printf("Congratulations!信息录入成功");
    system("pause");
    system("cls");
}

void Dele()
{
    struct node* p = head;
    struct node* q = NULL, * t;
    printf("请输入你想删除的学生学号");
    int z;
    scanf("%d", &z);
    while (p) {
        if (p->stu.num == z) {
            t = p;
            if (t == head) {
                if (t->next != NULL)
                    head = t->next;
                else
                    head = NULL;
            }
            else {
                if (t->next == NULL)
                    q->next = NULL;
                else
                    q->next = t->next;
            }
            free(t);
            break;
        }
        q = p;
        p = p->next;
    }
    if (p == NULL) {
        printf("Very Sorry!你输入的学号不存在!");
    }
    system("pause");
    system("cls");
}
//查操作(信息查询操作) 
void select()
{
    struct node* p = head;
    printf("请输入需要查找的学号: ");
    int x;
    scanf("%d", &x);
    while (p) {
        if (p->stu.num == x) {
            printf("姓名\t学号\t性别\t年龄\t专业\t班级\n");
            printf("%s\t%d\t%c\t%d\t%s\t%d\t\n", p->stu.name, p->stu.num, p->stu.sex,
                                             p->stu.age, p->stu.major, p->stu.classno);
            break;
        }
        p = p->next;
    }
    if (p == NULL)
        printf("sorry,查无此人!");
    system("pause");
    system("cls");
}

//改操作(信息修改操作) 
void change()
{
    struct node* p = head;
    printf("请输入需要改的学生学号:");
    int x;
    scanf("%d", &x);
    while (p) {
        if (p->stu.num == x) {
            printf("%s\t%d\t%c\t%d\t%s\t%d\n", p->stu.name, p->stu.num, p->stu.sex,
                                                 p->stu.age, p->stu.major, p->stu.classno);
            printf("*1姓名 *2学号 *3性别 *4年龄 *5专业 *6班级\n");
            printf("请输入你想修改的数据:");
            int y;
            scanf("%d", &y);
            switch (y) {
                  case 1:scanf("%s", p->stu.name); break;
                  case 2:scanf("%d", &p->stu.num); break;
                  case 3:getchar(); scanf("%c", &p->stu.sex); break;
                  case 4:scanf("%d", &p->stu.age); break;
                  case 5:scanf("%s", p->stu.major); break;
                  case 6:scanf("%d", &p->stu.classno); break;
                 default:y = 0; break;
            }
            if (y != 0)
                printf("Congratulations!修改成功!");
            break;
        }
        p = p->next;
    }
    if (p == NULL)
        printf("sorry,查无此人!");
    system("pause");
    system("cls");
}
//删操作(信息删除操作)

void menu()
{
    printf("\t=============================\n");
    printf("\t*Welcome to新生信息管理系统*\n");
    printf("\t=============================\n");
    printf("\t   *1)信息录入              \n");
    printf("\t   *2)信息修改              \n");
    printf("\t   *3)信息删除              \n");
    printf("\t   *4)信息查询              \n");
    printf("\t=============================\n");
}
void print()
{
    struct node* p = head;
    while (p)
    {
        printf("%s\t%d\t%c\t%d\t%s\t%d\n", p->stu.name, p->stu.num, p->stu.sex,
                                            p->stu.age, p->stu.major, p->stu.classno);
        p = p->next;
    }
    system("pause");
}
int main()
{
    while (1) {
        menu();
        printf("请输入你的选择:");
        int x;
        scanf("%d", &x);
        switch (x) {
        case 1:
            add();
            break;//信息录入
        case 2:
            change();
            break;//信息修改
        case 3:
            Dele();
            break;//信息删除
        case 4:
            select();
            break;//信息查询
        default:
            printf("你输入的数字有误error!!\n");
        }
        system("cls");
    }
}