理解链表C中的递归函数

理解链表C中的递归函数,c,function,recursion,struct,linked-list,C,Function,Recursion,Struct,Linked List,假设我编写了一个模拟多米诺骨牌游戏的程序,因此我想用以下方式定义一个结构: typedef struct nodo { int casella1; int casella2; struct nodo *next; } Tessera; typedef Tessera *Lista; 然后,在以偶然顺序进行一些输入之后,当一个数字超出范围0 casella1时,使用递归函数结束,如下所示: void legale(Lista *head) { Lista au

假设我编写了一个模拟多米诺骨牌游戏的程序,因此我想用以下方式定义一个结构:

typedef struct nodo { 
    int casella1;
    int casella2;
    struct nodo *next;
} Tessera;
typedef Tessera *Lista;
然后,在以偶然顺序进行一些输入之后,当一个数字超出范围0 casella1时,使用递归函数结束,如下所示:

void legale(Lista *head) {

    Lista aux,aus = *head;

    if(aus->next == NULL) {
      return;
    }

    else if(aus->casella2 == aus->next->casella1)   {
      legale(&(aus->next));
    }
    else {
      aux = aus->next;
      aus->next = aus->next->next;  
      free(aux);
    }
}
但例如,序列“12,23,34,45,54,62,7”给出了“12,23,34,45,62”,所以它没有像应该的那样删除6,2

我认为我删除指针的方式是正确的,那么为什么函数是错误的呢

代码如下所示:

#include<stdio.h>
#include<stdlib.h>
typedef struct nodo { 
    int casella1;
    int casella2;
    struct nodo *next;
    }Tessera;
typedef Tessera *Lista;

void stampa(Lista *head) {

    Lista ausil = *head;

    while(ausil != NULL) {
    printf("%d\t%d\n",ausil->casella1,ausil->casella2);
    ausil = ausil->next;
    }
}

void legale(Lista *head) {

    Lista aux,aus = *head;

    if(aus->next == NULL) {
    return;
}

    else if(aus->casella2 == aus->next->casella1)   {
    legale(&(aus->next));
}
    else {
    aux = aus->next;
    aus->next = aus->next->next;    
    free(aux);
}


}

void write (Lista *head) {
    Lista corr,aux,aus;
    int x,y;
    scanf("%d%d",&x,&y);
    aus = *head;

    while((x >= 0 && x <= 6) && (y >= 0 && y <= 6)) {

    if(aus == NULL) {

    aus = malloc(sizeof(Tessera));
    aus->casella1 = x;  
    aus->casella2 = y;
    aus->next = NULL;
    *head = aus;
}
    else {
    aux = *head;

    corr = malloc(sizeof(Tessera));
    corr->casella1 = x;
    corr->casella2 = y;
    corr->next = aux;
    *head = corr;
}
    scanf("%d%d",&x,&y);
    }

}

int main() {
    Lista Lista1 = NULL;
    write(&Lista1);
    legale(&Lista1);
    stampa(&Lista1);
return 0;
}
#包括
#包括
typedef结构节点{
int casella1;
int casella2;
结构nodo*next;
}特塞拉;
typedef Tessera*Lista;
无效stampa(列表A*头){
Lista ausil=*头;
while(ausil!=NULL){
printf(“%d\t%d\n”,ausil->casella1,ausil->casella2);
ausil=ausil->next;
}
}
无效法律(列表A*标题){
Lista aux,aus=*头;
如果(aus->next==NULL){
回来
}
否则如果(aus->casella2==aus->next->casella1){
法律(&(澳大利亚->下一步));
}
否则{
aux=aus->next;
aus->next=aus->next->next;
免费(aux);
}
}
无效写入(列表A*头){
澳大利亚辅助医疗机构Lista corr;
int x,y;
scanf(“%d%d”,&x,&y);
澳大利亚=*头;
而((x>=0&&x=0&&y)casella1=x;
aus->casella2=y;
aus->next=NULL;
*水头=澳元;
}
否则{
aux=*头;
corr=malloc(sizeof(Tessera));
corr->casella1=x;
corr->casella2=y;
corr->next=aux;
*head=corr;
}
scanf(“%d%d”,&x,&y);
}
}
int main(){
Lista Lista1=NULL;
写入(&Lista1);
法律(和清单1);
stampa(和列表1);
返回0;
}

删除重复项后,至少缺少一个递归调用

如果不递归,则在第一次删除后停止

另外,为了预防起见,在检查
aus->next==NULL
之前,您应该检查
aus==NULL
是否为空,以便在传递空列表时不会中断


编辑

当您阅读链接列表时,您正在反向构建链接列表


你把每一对都插在头上,这样最后你的顺序就倒过来了。在你读过之后,最好把你的列表打印出来,以确保它是正确的;)

我同意,但它至少应该删除最后一对,因此,如果只有一对错了,那么顺序就应该是正确的。不过我不明白你的例子,你写道:
“12,23,34,45,54,62,7”
给出了
“12,23,34,45,62”
。在这里它确实删除了
54
(它不应该有)。另外,7本身是什么?最好提供一个最小的可编译、可测试的示例。它不应该删除5 4,它应该删除6 2,7是否只是为了停止写入的序列“当一个数字超出范围0时结束
else {
  aux = aus->next;
  aus->next = aus->next->next;  
  free(aux);
}