为什么用地址传递的参数就可以不断链????
C语言中只能用地址传递,不能用引用传递“&"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct listNode{
int data;
struct listNode* next;
}listNode, *listPointer;
void createList(listPointer *L)
{
int x;
//L头节点地址
listPointer p;
listPointer temp;
//printf("%d", *x);
p = (listPointer)malloc(sizeof(listNode));
(*L) = p;
printf("%s", "按序输入链表的值");
scanf("%d", &x);
while (x != 999)
{
temp = (listPointer)malloc(sizeof(listNode));
temp->data = x;
temp->next = NULL;
p->next = temp;
p = temp;
scanf("%d", &x);
}
}
//1.递归删除无头节点链表中所有的X
void deletXRcur(listPointer *p, int x)
{
listPointer temp;
if ((*p) == NULL)
return;
if ((*p)->data == x)
{
temp = *p;
*p = (*p)->next;
free(temp);
deletXRcur(p, x);
}
else{
deletXRcur(&((*p)->next), x);
}
}
void deletXRcur2(listPointer p, int x)
{
listPointer temp;
if ((p) == NULL)
return;
if ((p)->data == x)
{
temp = p;
p = (p)->next;
free(temp);
deletXRcur2(p, x);
}
else{
deletXRcur2((p)->next, x);
}
}
void printList(listPointer L)
{
while (L->next != NULL)
{
printf("%d ", L->next->data);
L = L->next;
}
}
int main()
{
listPointer myL;
myL = (listPointer)malloc(sizeof(listNode));
createList(&myL);
printf("\n");
printList(myL);
//deletXRcur2(myL->next,5);
//printf("\n");
//printList(myL);
deletXRcur(&(myL->next), 5);
printf("\n");
printList(myL);
system("pause");
}
分析:
不断链表情况:实际是对他的next的内容进行了修改
断链表情况:实际是对Node的修改
其实就是修改前一个节点next的内容;所以要修改的级别是listPoint,所以传入的必须是listPointe的指针!!!!和上篇中的结论一致!!!