ToTelwang
ToTelwang
采纳率100%
2019-10-26 11:29

如何正确创建一个单链表(学生表)?

已采纳

有大佬解释下代码运行出错的原因么,单链表初学者小白=

#include "stdio.h"
#include "stdlib.h"

#define OK 1
#define  ERROR 0

typedef int num;
typedef char *str;
typedef struct stu
{
    num code;
    num number;
    str name;
    num class;
    num grade;
    str department;
    struct stu *next;
}*stu;

stu student = NULL;
stu e = NULL;

int linkcreatehead(stu *phead, int n)
{
    stu p = NULL;
    int i;

    *phead = (stu)malloc(sizeof(stu));
    (*phead)->next = NULL;

    for (i = 0; i < n; i++)
    {
        p = (stu)malloc(sizeof(stu));

        p->code = (i + 1);
        printf("请输入第%d个学生的\n学号\n", (i + 1));
        scanf("%d", &p->number);
        printf("姓名\n");
        scanf("%s", &p->name);
        printf("班级\n");
        scanf("%d", &p->class);
        printf("年级\n");
        scanf("%d", &p->grade);
        printf("专业\n");
        scanf("%s", &p->department);

        p->next = (*phead)->next;
        (*phead)->next = p;
    }
    return OK;
}//头插函数

int linkcreateiail(stu *phead, int n)
{
    stu p, q;
    int i;

    *phead = (stu)malloc(sizeof(stu));
    q = (*phead);

    for (i = 0; i < n; i++)
    {
        p = (stu)malloc(sizeof(stu));

        p->code = (i + 1);
        printf("请输入第%d个学生的\n学号\n", (i + 1));
        scanf("%d", &p->number);
        printf("姓名\n");
        scanf("%s", &p->name);
        printf("班级\n");
        scanf("%d", &p->class);
        printf("年级\n");
        scanf("%d", &p->grade);
        printf("专业\n");
        scanf("%s", &p->department);

        q->next = p;
        q = p;
    }
    q->next = NULL;

    return OK;
}//尾插函数

int getelem(stu phead, int n, stu *e)
{
    stu p;
    int i = 1, j = n;

    p = phead->next;

    while (i < j)
    {
        p = p->next;
        ++i;
    }
    if (!p || j > i)
    {
        return ERROR;
    }

    (*e)->number = p->number;
    (*e)->code = p->code;
    (*e)->name = p->name;
    (*e)->class = p->class;
    (*e)->grade = p->grade;
    (*e)->department = p->department;
    (*e)->next = NULL;

    return OK;
}//读取

int linklistinsert(stu *phead, int n, stu *e)
{
    stu p, s;
    int i, j = n;

    p = (stu)malloc(sizeof(stu));
    p = (*phead)->next;
    i = 0;
    while (p && i < j)
    {
        p = p->next;
        i++;
    }
    if (!p || j > i)
    {
        return ERROR;
    }
    printf("11");
    s = (stu)malloc(sizeof(stu));
    s->number = (*e)->number;
    s->code = (p->code + 1);
    s->name = (*e)->name;
    s->class = (*e)->class;
    s->grade = (*e)->grade;
    s->department = (*e)->department;
    s->next = p->next;
    p->next = s;
    s = s->next;

    while (!s)
    {
        s->code = (s->code + 1);
        s = s->next;
    }

    return OK;
}//插入

int linklistdelete(stu *phead, int n, stu *e)
{
    stu p, r;
    int i = 1;

    p = *phead;
    while (p && !(p->number = n))
    {
        p = p->next;
        i++;
    }
    if (!p)
    {
        return ERROR;
    }

    r = p->next;
    p->next = r->next;

    (*e)->number = r->number;
    (*e)->code = r->code;
    (*e)->name = r->name;
    (*e)->class = r->class;
    (*e)->grade = r->grade;
    (*e)->department = r->department;

    free(r);

    return OK;
}//删除单结点

stu linkclear(stu *phead)
{
    stu p, r;

    p = *phead;

    if (p)
    {
        return ERROR;
    }

    while (!p)
    {
        r = p;
        p = p->next;
        free(r);
    }

    free(*phead);

    (*phead) = NULL;

    return *phead;
}//整表清空

int main()
{
    printf("\t=========================================================================================\n");
    printf("\t*\t\t\t\t\t\t\t\t\t\t\t*\n");
    printf("\t*\t\t\t\t\t\t\t\t\t\t\t*\n");
    printf("\t*\t\t\t\t\t\t\t\t\t\t\t*\n");
    printf("\t*\t\t\t\t欢迎进入学生学籍管理系统\t\t\t\t*\n");
    printf("\t*\t\t\t\t\t\t\t\t\t\t\t*\n");
    printf("\t*\t\t\t\t\t\t\t\t\t\t\t*\n");
    printf("\t*\t\t\t\t\t\t\t\t\t\t\t*\n");
    printf("\t=========================================================================================\n");

    do
    {
        int jud1;
        int jud2;
        int jud3;
        int numberofstu = (int)malloc(sizeof(int));
        int number = 0;
        int confirm = (int)malloc(sizeof(int));

        if (student == NULL)
        {
            printf("请输入您要执行的操作:(1)创建一个学生表;(0)退出系统\n");
            jud1 = (int)malloc(sizeof(int));
            scanf("%d", &jud1);
            if (jud1 == 1)
            {
                printf("您是要如何创建学生表呢?(1)倒序(2)顺序\n");
                scanf("%d", &jud2);
                printf("请输入您的学生总数:\n");
                scanf("%d", &numberofstu);
                if (jud2 == 1)
                {
                    linkcreatehead(&student, numberofstu);
                    printf("头插函数运行成功!\n");
                }
                else if (jud2 == 2)
                {
                    linkcreateiail(&student, numberofstu);
                    printf("尾插函数运行成功!\n");
                }
                else
                {
                    printf("操作参数错误,系统崩溃,正在退出---\n");
                    return ERROR;
                }
            }
            else if (jud1 == 0)
            {
                printf("正在退出系统,请稍后---\n");
                return ERROR;
            }
            else
            {
                printf("操作参数错误,系统崩溃,正在退出---\n");
                return ERROR;
            }
        }
        else if (student != NULL)
        {
            e = (stu)malloc(sizeof(stu));
            e = NULL;
            jud3 = (int)malloc(sizeof(int));

            printf("请输入您当前要执行的操作:(1)插入一个新的学生信息;(2)删除一个学生的信息;\n(3)读取一个学生的信息;(4)清空当前学生表;(0)退出系统\n");
            scanf("%d", &jud3);

            if (jud3 == 1)
            {
                printf("请输入要插入的位置为第几个:\n");
                scanf("%d", &numberofstu);
                printf("请输入要插入的学生信息\n");
                printf("学号\n");
                scanf("%d", &e->number);
                printf("姓名\n");
                scanf("%s", &e->name);
                printf("班级\n");
                scanf("%d", &e->class);
                printf("年级\n");
                scanf("%d", &e->grade);
                printf("专业\n");
                scanf("%s", &e->department);

                linklistinsert(&student, numberofstu, &e);

                printf("函数运行成功!插入成功!\n");
            }
            if (jud3 == 2)
            {
                printf("请输入要删除的学生学号:\n");
                scanf("%d", number);

                linklistdelete(&student, number, &e);

                printf("函数运行成功!删除成功!\n");
            }
            if (jud3 == 3)
            {
                printf("请输入要读取的学生序号:\n");
                scanf("%d", &number);

                getelem(student, number, &e);

                printf("编号:%d\t学号:%d\t姓名:%s\n\t年级:%d班级:%d\t专业:%s\n", e->code, e->number, e->name, e->grade, e->class, e->department);
            }
            if (jud3 == 4)
            {
                printf("您确定要删除学生表吗?(请输入“1”来确认)\n");
                scanf("%d", &confirm);
                if (confirm == 1)
                {
                    student = linkclear(&student);
                    printf("学生表信息清空成功!\n");
                }
                else
                {
                    printf("看来您没有确认哦\n");
                }
            }
            if (jud3 == 0)
            {
                printf("正在退出系统,请稍后---\n");
                return ERROR;
            }
        }
    } while (1);

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答