涼宮こなた 2018-10-28 16:17 采纳率: 100%
浏览 939
已采纳

新手使用链表过程中遇到一个问题

exist函数里

执行for循环,执行到链表末尾的时候,curt1=curt1->next 之后应该已经为空了,为什么还会进入这个for循环
然后进行循环里第一个if比较的时候就报错了,,,,感觉是个挺脑残的问题,无奈我链表用的不熟练,完全不知道怎么解决

求指点!thx

#include<stdio.h>
#include<windows.h>
#define MAX 8

int temp[MAX];
int allcount=0;

typedef struct result{
    int num[MAX];
    struct result* next;
}*rst;

rst head=NULL;
rst last;
rst curt;

void print()
{
    int count;
    for(curt=head;curt!=NULL;curt=curt->next)
    {
        for(count=0;count<MAX;count++)
        {
            printf("%d",curt->num[count]);
        }
        printf("\n");
    }
}

int exist()
{
    rst curt1;
    int count1;
    int exist=1;
    if(head==NULL)
    {
        return 0;
    }
    for(curt1=head;curt1!=NULL;curt1=curt1->next)
    {
        for(count1=0;count1<MAX;count1++)
        {
            if(curt1->num[count1]!=temp[count1])
            {
                exist=0;
                break;
            }
        }
        for(count1=0;count1<MAX;count1++)
        {
            if(curt1->num[count1]!=temp[MAX-1-count1])
            {
                exist=0;
                break;
            }
        }
        for(count1=0;count1<MAX;count1++)
        {
            if(curt1->num[count1]!=MAX-1-temp[count1])
            {
                exist=0;
                break;
            }
        }
        for(count1=0;count1<MAX;count1++)
        {
            if(curt1->num[count1]!=MAX-1-temp[MAX-1-count1])
            {
                exist=0;
                break;
            }
        }
        if(exist==1)
        {
            return 1;
        }
        exist=1;
    }
    return 0;
}

void work(int current)
{
    int x,count;

    for(x=0;x<MAX;x++)
    {
        if(temp[x]==-1)//x暂时有用 
        {
            temp[x]=current;
            if(current==MAX-1)
            {
                allcount++;//test
                if(exist()==1)
                {
                    if(head==NULL)
                    {
                        head=(rst)malloc(sizeof(struct result));
                        curt=head;
                        for(count=0;count<MAX;count++)
                        {
                            head->num[count]=temp[count];
                        }
                    }
                    else
                    {
                        last=curt;
                        curt=(rst)malloc(sizeof(struct result));
                        last->next=curt;
                        for(count=0;count<MAX;count++)
                        {
                            curt->num[count]=temp[count];
                        }
                    }
                }

            }
            else
            {
                work(current+1);
            }
            temp[x]=-1;
        }
    }
}


int main(void)
{
    int count;
    for(count=0;count<MAX;count++)
    {
        temp[count]=-1;
    }
    work(0);
    printf("%d",allcount);
    print();
}
  • 写回答

2条回答 默认 最新

  • threenewbee 2018-10-28 17:21
    关注

    head=(rst)malloc(sizeof(struct result));
    curt=(rst)malloc(sizeof(struct result));
    之后分别加上
    head->next = NULL;
    curt->next = NULL;
    否则next是随机值

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 涼宮こなた 2018-10-28 16:26
    关注

    完整代码

     #include<stdio.h>
    #include<windows.h>
    #define MAX 8
    
    int temp[MAX];
    int allcount=0;
    
    typedef struct result{
        int num[MAX];
        struct result* next;
    }*rst;
    
    rst head=NULL;
    rst last;
    rst curt;
    
    void print()
    {
        int count;
        for(curt=head;curt!=NULL;curt=curt->next)
        {
            for(count=0;count<MAX;count++)
            {
                printf("%d",curt->num[count]);
            }
            printf("\n");
        }
    }
    
    int exist()
    {
        rst curt1;
        int count1;
        int exist=1;
        if(head==NULL)
        {
            return 0;
        }
        for(curt1=head;curt1!=NULL;curt1=curt1->next)
        {
            for(count1=0;count1<MAX;count1++)
            {
                if(curt1->num[count1]!=temp[count1])
                {
                    exist=0;
                    break;
                }
            }
            for(count1=0;count1<MAX;count1++)
            {
                if(curt1->num[count1]!=temp[MAX-1-count1])
                {
                    exist=0;
                    break;
                }
            }
            for(count1=0;count1<MAX;count1++)
            {
                if(curt1->num[count1]!=MAX-1-temp[count1])
                {
                    exist=0;
                    break;
                }
            }
            for(count1=0;count1<MAX;count1++)
            {
                if(curt1->num[count1]!=MAX-1-temp[MAX-1-count1])
                {
                    exist=0;
                    break;
                }
            }
            if(exist==1)
            {
                return 1;
            }
            exist=1;
        }
        return 0;
    }
    
    void work(int current)
    {
        int x,count;
    
        for(x=0;x<MAX;x++)
        {
            if(temp[x]==-1)
            {
                temp[x]=current;
                if(current==MAX-1)
                {
                    allcount++;//test
                    if(exist()==1)
                    {
                        if(head==NULL)
                        {
                            head=(rst)malloc(sizeof(struct result));
                            curt=head;
                            for(count=0;count<MAX;count++)
                            {
                                head->num[count]=temp[count];
                            }
                        }
                        else
                        {
                            last=curt;
                            curt=(rst)malloc(sizeof(struct result));
                            last->next=curt;
                            for(count=0;count<MAX;count++)
                            {
                                curt->num[count]=temp[count];
                            }
                        }
                    }
    
                }
                else
                {
                    work(current+1);
                }
                temp[x]=-1;
            }
        }
    }
    
    
    int main(void)
    {
        int count;
        for(count=0;count<MAX;count++)
        {
            temp[count]=-1;
        }
        work(0);
        printf("%d",allcount);
        print();
    }
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 unity打光没有照亮物体
  • ¥25 powershell如何拷贝1周前的文件
  • ¥15 询问MYSQL查询SQLSERVER数据表并比较差异后,更新MYSQL的数据表
  • ¥15 关于#前端#的问题,请各位专家解答!
  • ¥15 最小生成树问题 Prim算法和Kruskal算法
  • ¥25 医院住院病人呼叫器设计
  • ¥15 不想和现在的团队合作了,怎么避免他们对程序动手脚
  • ¥20 C语言字符串不区分大小写字典排序相关问题
  • ¥15 关于#python#的问题:我希望通过逆向技术爬取1688搜索页下滑加载的数据
  • ¥15 关于Linux的终端里,模拟实现一个带口令保护的屏保程序遇到的输入输出的问题!(语言-c语言)