链表这样释放内存为啥不可以?
程序代码:
# include <stdio.h> # include <stdlib.h> # define LEN sizeof(struct ring) struct ring { int num; struct ring *next; }; void blist(struct ring *head, int n) //建立环状链表 { int i; struct ring *pre = head, *new, *fin; for(i = 0; i < n; i++) { pre->num = i+1; fin = pre; new = malloc(LEN); pre->next = new; pre = new; } fin->next = head; } void del(struct ring *head) //每第三个节点删除 { struct ring *pre, *bef, *t; pre = bef = head; while(pre->next != pre) { pre = bef = pre->next; pre = pre->next; t = pre; free(pre); pre = bef->next = t->next; } printf("剩下最后一人的原序号为:NO.%d\n", pre->num); } int main() { struct ring *head; int n; printf("请输入参与的人数:"); scanf("%d", &n); head = malloc(LEN); blist(head, n); del(head); //print(head); return 0; }