这个删除链表节点的代码输出为什么总是那么奇怪,

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
struct link
{
    char name[20];
    char password[20];
    struct link *next;
} link,*linklist;
struct link* add(struct link *L,char *name,char *password);
void print(struct link *L);
struct link* delete(struct link *L,char *name);
//bool judge(char *name,char *password);
void update(char *name,char *password);
int main()
{

    struct link *L;
    size_t a;
    L=(struct link*)malloc(sizeof(struct link));
    L->next=NULL;
    L=add(L,"hello","1");
    L=add(L,"world","2");
    L=add(L,"me","3");
    print(L);
    printf("----------------\n");
    L=delete(L,"me");
    print(L);
}

struct link* delete(struct link *L,char *name)
{
struct link *p,*q,*t;
p=L;
// t=p;
while(p)
{
if(strcmp(p->name,name)==0)
{
t->next=p->next;
free(t);
}
t=p;
p=p->next;

}
return L;

}


在delete方法里,free(t)改为free(p)才对,因为你删除的是p结点!!

你删除错了,while(p){}好像死循环,还没判断删除的三种情况(是删除首部、中间还是尾部)

delete这里,free(t)的t改为p才对