涼宮こなた 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是随机值

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作