题目:
思路:
完整测试代码:
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node* next;
}List;
void DeleteX(List* &L,int X){
List *p;
if (L == NULL)
{
return;
}
if (L->data == X)
{
p = L;
L = L->next;
free(p);
DeleteX(L, X);
}
else
{
DeleteX(L->next, X);
}
}
int main()
{
List* p,*A = (List*)malloc(sizeof(List));
//printf("%d,%d", sizeof(List), sizeof(List*));//看是什么位操作系统环境,如果是32位则结果是8,4,否则是64位则是 16,8字节,
// //List不是 4+8而是16 的原因是有内存对齐问题
A->data = 100;
p = A;
while (A->data != 0)
{
List* m = (List*)malloc(sizeof(List));
printf("请输入data:");
scanf_s("%d",&m->data);
//printf(" %d", m->data);
A->next = m;
A = m;
}
A->next = NULL;
DeleteX(p, 100);
while (p)
{
printf("%d ", p->data);
p= p->next;
}
return 0;
}