m0_45172303 2021-09-16 17:41 采纳率: 100%
浏览 84
已结题

引发了异常:读取访问权限冲突

写一个管理学生成绩的链表,引发了访问异常冲突,不知道是怎么回事。。

img


#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#define LEN sizeof(struct student)   //student结构的大小
struct student* creat();  //创建链表
void print(struct student* head); //打印链表
struct student* del(struct student* head, int num);  //删除链表节点
struct student* add(struct student stu_1, struct student* head);           //插入链表节点
//struct student *add(int num, float score, struct student *head);           //插入链表节点

struct student
{
    int num;
    float score;
    struct student* next;
};
int n;                   //全局变量,用来记录存放了多少数据
void main()
{
    int num;
    float score;
    struct student stu_1;
    struct student* stu, * stu_2;
    stu = creat();
    print(stu);


    printf("\n\n");

#if(0)
    printf("输入想要删除的数据的学号:");
    scanf("%d", &num);
    stu = del(stu, num);
    print(stu);
    //    system("pause");
#endif
#if(1)
    printf("输入想要增加的数据的学号和成绩:");
    scanf("%d%f", &stu_1.num, &stu_1.score);
    stu_1.next = NULL;
#endif
    stu_2 = add(stu_1, stu);
    print(stu_2);

}

struct student* creat()
{
    struct student* head;
    struct student* p1, * p2;
    p1 = p2 = (struct student*)malloc(LEN); //LEN是student结构的大小
    printf("Please enter the num :");
    scanf("%d", &p1->num);
    printf("Please enter the score:");
    scanf("%f", &p1->score);


    head = NULL;
    n = 0;
#if(0)
    if (n == 0)
    {
        head = p1;
    }
#endif    
    while (p1->num)
    {
        n++;
#if(1)
        if (n == 1)
        {
            head = p1;
        }
#endif

        //    {
        p2->next = p1;
        p2 = p1;

        //    }
        p1 = (struct student*)malloc(LEN);

        printf("Please enter the num :");
        scanf("%d", &p1->num);
        printf("Please enter the score:");
        scanf("%f", &p1->score);
    }
    p2->next = NULL;
    return head;
}

void print(struct student* head)
{
#if(1)
    struct student* stu_3;
    stu_3 = head;
#endif
    printf("There are %d records !\n", n);
    while (stu_3)
    {
        printf("学号为%d的学生的成绩是: %f\n", stu_3->num, stu_3->score);
        stu_3 = stu_3->next;
    }
}
#if(0)
void print()
{}
#endif

struct student* del(struct student* head, int num)
{

    struct student* p1, * p2;
    p1 = p2 = head;

    if (p1)
    {

        while (p1->next != NULL && p1->num != num)
        {

            p2 = p1;
            p1 = p1->next;
        }
        if (head->num == num)
        {
            head = p1->next;
            n--;
        }

        else if (p1->num == num)

        {
            p2->next = p1->next;
            n--;
        }
        else
        {
            printf("未找到要删除的数据!\n");
        }

        return head;
    }
    else
    {
        printf("这是一个空表!\n");
    }




}

struct student* add(struct student stu_1, struct student* head)
{
    struct student* p0, * p1, * p2;
    p0 = NULL;
#if(0)
    struct student stu;
    stu.num = num;
    stu.score = score;
#endif
    p0 = &stu_1;
    p2 = p1 = head;

#if(0)
    stu.num = num;
    stu.score = score;
    p0 = &stu;
#endif
    if (p1)
    {
        while (p0->num > p1->num && p1->next != NULL)
        {
            p2 = p1;
            p1 = p1->next;
        }
        if (p0->num <= head->num)
        {
            p0->next = head;
            head = p0;
        }
        else if (p0->num <= p1->num)
        {
            p0->next = p1;
            p2->next = p0;
        }
        else
        {
            p0->next = NULL;
            p1->next = p0;
        }

    }
    else
    {
        printf("这是一个空表!");
        p0->next = NULL;
        head = p0;
    }
    n++;
    return head;
}


想要在表头插入数据发生错误。

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2021-09-16 17:55
    关注

    问题在与add函数,参数stu_1是个值参,你把函数内的变量加入到链表,但函数结束后,这个函数内部变量stu_1就被系统回收了。再次使用就报错了。
    考虑把参数改为 struct student &stu_1,这样是引用传递,main里的stu_1变量一直存在
    不过还是会有隐患,因为stu_1在main中的作用似乎是用来输入新节点数据的,新节点建议每次都用malloc新分配空间,不要多个用途

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月24日
  • 已采纳回答 9月16日
  • 修改了问题 9月16日
  • 修改了问题 9月16日
  • 展开全部

悬赏问题

  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法