正在努力学习的小白袁 2022-10-04 10:58 采纳率: 86.8%
浏览 45
已结题

C语言数据结构单向循环链表小会遇到报错的问题

我创建的是单向循环链表,但是在我销毁的过程中,遇到了报错,我不知道是什么原因
一下三张图分别是销毁代码,内存监视,报错说明

img

img

img


源代码

#include <stdio.h>
#include<stdlib.h>
typedef int Newtype;
//单向循环链表
struct Node {
    Newtype date;
    struct Node* next;
};
void Node_init(Node* head) {
    head->next = head;
}//创建链表
int Node_count(Node* head) {
    int count = 0;
    struct Node* pc = head->next;
    if (head->next == head) {
        return 0;
    }
    for (pc; pc != head; pc = pc->next) {
        count++;
    }
    return count;

};//统计个数
void Node_head_insert(Node* head) {
    Newtype n;
    printf("请输入头插的值:");
    scanf_s("%d", &n);
    struct Node* Newdate = (struct Node*)malloc(sizeof(struct Node));
    Newdate->date = n;
    Newdate->next = NULL;
    struct Node* pc = head->next;
    head->next = Newdate;
    Newdate->next = pc;
    printf("已头插!\n");
}//头插节点
void Node_head_del(Node* head) {
    struct Node*pc = head->next;
    head->next = head->next->next;
    free(pc);
    if (head->next == head) {
        free(head);
        head = NULL;
        return;
    }
    pc = NULL;
    printf("头删成功!\n");
}//头删节点
void Node_tail_insert(Node* head) {
    Newtype n;
    printf("请输尾插的值:");
    scanf_s("%d", &n);
    struct Node* Newdate = (struct Node*)malloc(sizeof(struct Node));
    Newdate->date = n;
    Newdate->next = NULL;

    if (head->next == head) {
        head->next = Newdate;
        Newdate->next = head;
    }
    else {
        struct Node* pc = head->next;
        for (pc; pc->next != head; pc = pc->next);
        pc->next = Newdate;
        Newdate->next = head;
    }
    printf("尾插成功!\n");
};//尾插节点
void Node_tail_del(Node* head) {
    struct Node* pre, *pc;
    pre = head;
    pc = head->next;
    for (pc, pre; pc->next != head; pc = pc->next, pre = pre->next);
    if (head->next == head) {
        free(head);
        head = NULL;
        return;
    }
    pre->next = head;
    pc->next = NULL;
    free(pc);
    printf("已尾删!\n");
}//尾删节点
void Node_middle_insert(Node* head) {
    if (head == NULL)exit(-1);
    int pos;
    printf("请输入插入的位置:");
    scanf_s("%d", &pos);
    int n = Node_count(head);
    if (pos == 1) {
        Node_head_insert(head);
    }
    else if (pos > n) {
        Node_tail_insert(head);
    }
    else if (1 < pos&&pos <= n) {
        struct Node* pre, *pc;
        pre = head;
        pc = head->next;
        for (int i = 1; i < pos; i++) {
            pre = pc;
            pc = pc->next;
        }
        struct Node* Newdate = (struct Node*)malloc(sizeof(struct Node));
        printf("请输入增加的值:");
        scanf_s("%d", &(Newdate->date));
        Newdate->next = NULL;
        
        pre->next = Newdate;
        Newdate->next = pc;

        printf("插入成功!\n");
    }
    else {
        printf("位置错误!\n");
    }
}//中间插入节点
void Node_middle_del(Node* head) {
    int pos;
    printf("请输入要输出的位置:");
    scanf_s("%d", &pos);
    struct Node* pre, *pc;
    pre = head;
    pc = head->next;
    for (int i = 1; i < pos; i++) {
        pre = pc;
        pc = pc->next;
    }
    pre->next = pc->next;
    pc->next = NULL;
    free(pc);
    printf("已删除!\n");
}//中间删除节点
void Node_destory(Node* head) {
    struct Node* pre, *pc,*item;
    item=pre = head;
    pc = head->next;
    while (true) {
        if (pc != head) {
            item = pre;
            item->next=NULL;
            pre = pc;
            pc = pc->next;
            free(item);
        }
        else {
            free(pre);
            printf("已销毁!\n");
            return;
        }
    }
}//销毁节点
void Node_print(Node* head) {
    if (head == NULL)exit(-1);
    struct Node* pc = head->next;
    while (pc!= head) {
        printf("%d ", pc->date);
        pc = pc->next;
    }
    printf("\n");
}//打印节点
int main() {
    struct Node head;
    Node_init(&head);
    Node_tail_insert(&head);
    Node_tail_insert(&head);
    Node_print(&head);
    Node_head_insert(&head);
    Node_head_insert(&head);
    Node_print(&head);
    Node_tail_del(&head);
    Node_head_del(&head);
    Node_print(&head);
    Node_middle_insert(&head);
    Node_print(&head);
    Node_middle_del(&head);
    Node_print(&head);
    Node_destory(&head);
    return 0;
}

没有注释哈
太懒注释了,我是复习的时候写的

  • 写回答

5条回答 默认 最新

  • 浪客 2022-10-04 11:11
    关注

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 快乐鹦鹉 2022-10-04 11:13
    关注

    循环的第一次,你就将head给free掉了啊。然后循环的最后一次,你又free掉head,肯定报错的

    评论
  • qzjhjxj 2022-10-04 13:08
    关注

    修改如下,供参考:

    void Node_destory(Node *head)
    {
        struct Node *pre, *pc;
        pre = head;
        pc = head->next;
        while (1){
            if (pc){ //修改 (pc != NULL)
                free(pre);
                pre = pc;
                pc = pc->next;
            }
            else{
                     //pc->next = NULL; 修改
                free(pre); //free(pc);  修改
                printf("已销毁!\n");
                return;
            }
        }
    }
    
    
    评论
  • 关注

    img

    img


    按照答主,修改后的代码

    评论
  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-09 16:41
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 10月12日
  • 已采纳回答 10月4日
  • 修改了问题 10月4日
  • 创建了问题 10月4日

悬赏问题

  • ¥15 摁摁钮(接P3.4口)无法实现点阵管播放速度调节,大家能帮我看看是什么问题吗?TAT
  • ¥15 小型网络防火墙mstp.vrrp.ospf配置
  • ¥15 grafna发送告警信息
  • ¥15 51单片机,LCD屏幕内容修改
  • ¥20 Ida Pro动态调试
  • ¥15 TensorFlow深度学习拓展项目
  • ¥20 springboot博客系统
  • ¥15 MICE包多重插补后数据集汇总导出
  • ¥15 一道算法分析问题(关于3-MSAT)
  • ¥15 C++ FLUENT 化学反应速率 编写困难