我创建的是单向循环链表,但是在我销毁的过程中,遇到了报错,我不知道是什么原因
一下三张图分别是销毁代码,内存监视,报错说明
源代码
#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;
}
没有注释哈
太懒注释了,我是复习的时候写的