正在努力学习的小白袁 2022-10-04 10:58 采纳率: 87%
浏览 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

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

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败