c语言写链表遇到的问题,问题出在删除,但是不知道是什么原因
```c
#include <stdio.h>//双向链表
#include <stdlib.h>
typedef struct a {
int n;
struct a *last;
struct a *next;
} link;
link *creat();
int add(link *p, int aim);
link *find(int a, link *p);
int print(link *p);
int del(link *p, int aim);
int main() {
link *p;
p = creat();
add(p, 2);
add(p, 4);
print(p);
del(p, 4);
print(p);
}
link *creat() {
link *p;
p = (link *)malloc(sizeof(link));
p->last = p;
p->next = p;
p->n = 0;
return p;
}
int add(link *p, int aim) {
link *pp, *s;
pp = (link *)malloc(sizeof(link));
pp->next = p->next;
s = pp->next;
s->last = pp;
p->next = pp;
pp->last = p;
pp->n = aim;
return 1;
}
link *find(int a, link *p) {
link *pp;
pp = p;
do {
if (pp->n == a)
return pp;
pp = pp->next;
} while (pp != p);
return NULL;
}
int del(link *p, int aim) {
link *pp;
pp = find(aim, p);
printf("\n%d\n", pp->n);
printf("\n%d\n", pp->last->n);
if (pp = NULL) {
return 0;
} else {
printf("问题没出\n");
pp->last->next = pp->next;
printf("问题没出\n");
pp->next->last = pp->last;
free(pp);
}
return 1;
}
int print(link *p) {
link *pp;
pp = p;
int i = 0;
do {
i++;
printf("%d\t", pp->n);
pp = pp->next;
if (i && i % 10 == 0)
printf("\n");
} while (pp != p);
return 1;
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/672891620876110.png "#left")