0

我创建了一个名为“foo”的函数,我首先在主函数中调用她,参数 row=0 和 col=0。该函数所做的第一件事(我为调试所做的)是打印行和列。由于某种原因,程序打印的第一件事是:

1 1
1 0

而只是比0 0。当它应该首先打印她的第一个参数(0 0)时,有人知道这是为什么吗?

Tnx,院长

#include <stdio.h>
#include <stdlib.h>
#define size (3)
#include "List.h"
void foo(List *addit,List *list, int row,int col,char from,int sum,int array[][size])
{
    printf("%d ",col);
    printf("%d\n",row);
    if(row==size-1&&col==size-1){
            add(addit,row*10+col);
            nested(list,addit,sum+array[row][col]);
            return;
        }
    if(row!=size-1&&from!='d'){
        addit=(clone(addit));
        foo(addit,list,row+1,col,'u',sum+array[row][col],array);
        add(addit,row*10+col);
    }
    if(row!=0&&from!='u'){
            addit=(clone(addit));
            foo(addit,list,row-1,col,'d',sum+array[row][col],array);
            add(addit,row*10+col);
    }
    if(col!=0&&from!='l'){
            addit=(clone(addit));
            foo(addit,list,row,col-1,'r',sum+array[row][col],array);
            add(addit,row*10+col);
    }
    if(col!=size-1&&from!='r'){
            addit=(clone(addit));
            foo(addit,list,row,col+1,'l',sum+array[row][col],array);
            add(addit,row*10+col);
    }
}
int max(List* lst)
{
    Node *node=lst->first;
    int var=0;
    while(node!=NULL){
        if(node->info>var)
            var=node->info;
        node=node->next;    
    }
    return var;
}
int main()
{
    List * l1=(List*)(malloc(sizeof(List)));;
    make_list(l1,0);
    int array[size][size];
    unsigned i,k;
    for(k=0;k<size;++k){
        for(i=0;i<size;++i){
            //scanf("%d",array[k][i])
            array[k][i]=i;
        }
    }
    List *list=(List*)(malloc(sizeof(List)));
    make_list(list,0);
    foo(list,l1,0,0,'u',0,array);
    printf("maindebug");
    printf("%d",max(l1));
}

这是 List.c

#include <stdlib.h>
#include <stdio.h>
#include "List.h"
#include "Node.h"
List* clone(List* l1){
    List *l2=(List*)(malloc(sizeof(List)));
    Node* node=l1->first;
    make_list(l2,node->info);
    node=node->next;
    while(node!=NULL){
        add(l2,node->info);
        node=node->next;
    }
    return l2;
}
int make_list(List* l1,int new_info)
{
    Node *node=(Node*)(malloc(sizeof(Node)));
    if(node==NULL)
        return 1;
    node->info=new_info; node->next=NULL; node->prev=NULL;
    l1->first=node;
    l1->last=node;
    l1->len=1;
    l1->sum=new_info;   
    return 0;
}
float get_avg(List* l1){
    return (float)l1->sum/l1->len;
}
int nested(List* big,List* nes, int new_info){
    Node *node=(Node*)(malloc(sizeof(Node)));
    node->info=new_info; node->next=NULL; node->prev=NULL; node->lst=nes;
    if(node==NULL)
        return 1;
    Node* temp=big->last;
    big->last=node;
    temp->next=big->last;
    (++big->len);
    big->sum+=new_info;
    return 0;
}
int add(List *l1, int new_info)
{
    Node* temp=l1->last;
    Node* node=(Node*)(malloc(sizeof(Node)));
    if(node==NULL)
        return 1;
    node->info=new_info; node->next=NULL; node->prev=temp;
    l1->last=node;
    temp->next=l1->last;
    (++l1->len);
    l1->sum+=new_info;
    return 0;
}
void del(List* l1,Node * node)
{
    int a=1;
    if(l1->len!=1)
    {
        if(node==l1->first){
            l1->first=node->next;
            Node* temp=l1->first;
            temp->prev=NULL;
            a=0;    
        }
        if(a!=0&&node==l1->last){
            l1->last=node->prev;
            Node* temp=l1->last;
            temp->next=NULL;
            a=0;
        }
        if(a!=0&&node!=l1->last&&node!=l1->first)
        {
        Node* pre=node->prev; Node* nex=node->next;
        pre->next=node->next;
        nex->prev=node->prev;
        }
    }   
    l1->sum-=node->info;
    free(node);
    --(l1->len);
}
void destruct(List* l1){
    unsigned i=0;
    for(i=1;i<=l1->len;i++){
        del(l1,l1->first);
    }
}
void printinfo(List* l1){
    unsigned i=0; 
    Node* node=l1->first;
    for(i=1;i<=l1->len;i++){
        printf("%d\n",node->info);
        node=node->next;
    }
}
4

0 回答 0