m0_63549002 2021-12-14 10:12 采纳率: 75%
浏览 52
已结题

c语言 链表读取访问权限出错

我想写一个单链表,然后用节点内部数据排序输出、
然后就出现了这个问题。

img

这个是我的代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define LEN sizeof(struct Teacher)
struct Teacher
{
    char name[20];
    int number;
    struct Teacher* next;
};

int n;
struct Teacher* creat()
{
    struct Teacher* p1 = NULL;
    struct Teacher* p2 = NULL; 
    struct Teacher *head = NULL;
    p1 = p2 = new struct Teacher;
    scanf_s("%s%d", p1->name, sizeof(p1->name), &p1->number);
    while (p1->number != 0)
    {
        n = n + 1;
        if (n == 1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        p1 = new struct Teacher;
        scanf_s("%s%d", p1->name, sizeof(p1->name), &p1->number);
    }
    p2 = NULL;
    return head;
}

void print_teacher(struct Teacher* head)                 //输出信息列表
{
    struct Teacher* p;
    p = head;
    if (head != NULL)
    {
        while (p != NULL)
        {
            printf("%s   %d\n", p->name, p->number);
            p = p->next;
        }
    }
}

void sort_teacher1(struct Teacher* head)                          //教师工号排序(升序)
{
    if (head == NULL)
        return ;
    else
    {
        struct Teacher* p1 = head;
        struct Teacher* p2 = NULL;
        while (p1->next != NULL)
        {
            p2 = p1->next;
            if (p1->number >p2->number)
            {
                Teacher t;
                t = *p1;
                t.next = p2->next;
                p2->next = p1->next;
                *p1 = *p2;
                *p2 = t;
            }
            p1 = p1->next;
        }
    }
    
}


int main()
{
    struct Teacher* head;
    head = creat();
    sort_teacher1(head);
    print_teacher(head);
    return 0;
}

  • 写回答

2条回答 默认 最新

  • 关注

    create函数有问题,最后一个节点的next指针没有指向NULL
    n = n + 1;这里使用n的时候,n没有初始化
    create函数修改如下:

    #include <stdio.h>
    struct Teacher
    {
        char name[20];
        int number;
        struct Teacher* next;
    };
    int n;
    
    struct Teacher* creat()
    {
        struct Teacher* p1 = NULL;
        struct Teacher* p2 = NULL; 
        struct Teacher *head = NULL;
        p1 = p2 = new struct Teacher;
        n = 0; //修改1:初始化n
        scanf_s("%s%d", p1->name, sizeof(p1->name), &p1->number);
        while (p1->number != 0)
        {
            n = n + 1;
            if (n == 1)
            {
                head = p1;
                p2 = head; //修改2:给p2赋值
            }
            else
                p2->next = p1;
            p2 = p1;
            p1 = new struct Teacher;
            scanf_s("%s%d", p1->name, sizeof(p1->name), &p1->number);
        }
        p2->next = NULL; //修改3,不是p2 = NULL
        //最后的p1没用,需要释放空间
        delete p1; p1=0;
        return head;
    }
    
    
    
    struct Teacher* sort_teacher1(struct Teacher *head)   //排序
    {
        if(NULL == head)    //若链表为空则不用排序
        {
            return NULL;
        }
        if(NULL == head->next)  //若链表中只有一个数,则不用比较
        {
            printf("min is head\n");
            return head;
        }
        struct Teacher *min_pre = NULL;
        struct Teacher *min = head;
        struct Teacher *tmp = head;
        struct Teacher *new_list = NULL;   
        struct Teacher *tail_sort = NULL;
    
        while(head)
        {
            min = head;
            tmp = head;
            while(tmp->next)
            {
                if(min->number > tmp->next->number)
                {
                    min = tmp->next;
                    min_pre = tmp;
                }
                tmp = tmp->next;
            }
            if(min == head)
            {
                head = head->next;
            }
            else
            {
                min_pre->next = min->next;
                min->next = NULL;
            }
            if(NULL == new_list)    //按照尾插将最小的数组成新的链表
            {
                tail_sort = min;
                new_list = tail_sort;
            }
            else
            {
                tail_sort->next = min;
                tail_sort = min;
            }
        }
        return new_list;
    }
    
    
    void print_teacher(struct Teacher* head)                 //输出信息列表
    {
        struct Teacher* p;
        p = head;
        if (head != NULL)
        {
            while (p != NULL)
            {
                printf("%s   %d\n", p->name, p->number);
                p = p->next;
            }
        }
    }
    
    int main()
    {
        struct Teacher* head;
        head = creat();
        head = sort_teacher1(head);
        print_teacher(head);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月22日
  • 已采纳回答 12月14日
  • 创建了问题 12月14日

悬赏问题

  • ¥500 高有偿提问!求优化设计微信小程序
  • ¥15 matlab在安装时报错 无法找到入口 无法定位程序输入点
  • ¥15 收益高的广告联盟有哪些
  • ¥15 Android Studio webview 的使用问题, 播放器横屏全屏
  • ¥15 删掉jdk后重新下载,Java web所需要的eclipse无法使用
  • ¥15 uniapp正式环境中通过webapi将本地数据推送到设备出现的跨域问题
  • ¥15 xui建立节点,显示错误
  • ¥15 关于#单片机#的问题:开始、复位、十进制的功能可以实现,但是切换八进制的功能无法实现(按下按键也没有效果),把初始状态调成八进制,也是八进制可以实现但是切换到十进制不行(相关搜索:汇编语言|计数器)
  • ¥15 VINS-Mono或Fusion中feature_manager中estimated_depth是特征的深度还是逆深度?
  • ¥15 谷歌浏览器如何备份抖音网页数据