这段里面,为啥我改了下删除函数就不能用了???
下面是原本正确程序的一部分:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxlen 20
struct student
{
int num;
char name[10];
int age;
char address[20];
};
typedef struct student elementtype;
struct nodelist
{
struct nodelist *next;
elementtype data;
};
typedef struct nodelist node;
node *initial_node()
{
node *L;
L=(node *)malloc(sizeof(node));
L->next=NULL;
return L;
}
//添加
node *nodeadd(node *L, elementtype x)
{
node *S, *P = L;
int k = 0;
S=(node*)malloc(sizeof(node));
S->data=x;
S->next=P->next;
P->next=S;
return P;
}
//删除
node *nodedel(node *L, elementtype x)
{
node *S, *P = L;
int k = 0;
while( (P->next != NULL) && (P->next->data.num != x.num))
P = P->next;
if (P != NULL)
{
S = P->next;
P->next = S->next;
free(S);
}
return P->next;
}
然后我把删除功能部分的程序改成下面这样后,就无法实现删除功能了,这是为什么??
//删除
node *nodedel(node *L, char n[20]) //elementtype x
{
node *S, *P = L;
int k = 0;
while( (P->next != NULL) && (P->next->data.name != n )) // x.num
P = P->next;
if (P != NULL)
{
S = P->next;
P->next = S->next;
free(S);
}
return P->next;
}