链表删除中free()释放内存问题!大家帮忙看看错在哪里?
程序代码:
/* Note:Your choice is C IDE */ #include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef struct list { int date; struct list *next,*pre; }node,*link; link head=NULL,prelink; link bulid(int init) { link newlink=(link)malloc(sizeof(node)); newlink->date = init; if(head==NULL) { head = newlink; head->next = NULL; head->pre = NULL; prelink = newlink; } else { prelink->next = newlink; newlink->pre = prelink; newlink->next = NULL; prelink = newlink; } return head; } void display(link currlink) { link p; p=currlink; while(p!=NULL) { printf("%d->",p->date); p=p->next; } } void del(int a,link b) { link p,temp; p=b; while(p!=NULL) { printf("%d->",p->date); if(p->date==a) { if(p==head) { printf("dfdasfa\n"); head = head->next; head->pre =NULL; free(p);//此处出错 } else if(p->next==NULL) { p->pre->next=NULL; free(p); //此出出错 } else { temp = p->next; p = p->pre; p->next = temp; temp->pre=p; } } p=p->next; } } void main() { link frist; int n,i; printf("input your first data!\n"); scanf("%d",&n); frist=bulid(n); for(i=0;i<3;i++) { printf("input:\n"); scanf("%d",&n); bulid(n); } display(frist); printf("\n"); printf("input your delete data!\n"); scanf("%d",&n); del(n,frist); printf("\n"); display(frist); }程序中有两次使用free(),都出错了,到底要怎么写?大家帮忙看看