为什么 ReverseList 函数不起作用?

huangapple go评论74阅读模式
英文:

Why does the ReverseList function doesn't work?

问题

这是结构体的定义:

ElemTypestatus都等于int

typedef struct LNode{
    ElemType data;
    struct LNode *next;
} LNode, *LinkList;

在函数体中的代码:

			case 15://ReverseList
				if(ReverseList(&L[i_num]) == INFEASIBLE) 
					printf("The list doesn't exist!\n");
				else if(ReverseList(&L[i_num]) == ERROR) 
					printf("The list is empty!\n");
				else
					printf("Success!\n");

				getchar(); 
				getchar();
				break;

这是函数的定义:

status ReverseList(LinkList *L)
//翻转链表
{
    if(L)
    {
        LinkList prev=NULL;
		LinkList cur=*L;
		LinkList next=NULL;
		while(cur)
		{
			next=cur->next;
			cur->next=prev;
			prev=cur;
			cur=next;
		}
		*L=prev;
		return OK;
	}
    else  
		return INFEASIBLE;
}

运行函数后,链表没有被翻转。

为什么呢? 为什么 ReverseList 函数不起作用?

reverselist函数不起作用。
orz

英文:

this is the definition of the struct:

ElemTypeandstatusare both equal to int

typedef struct LNode{
	ElemType data;
	struct LNode *next;
}LNode, *LinkList;

the body:

			case 15://ReverseList
				if(ReverseList(&L[i_num]) == INFEASIBLE) 
					printf("The list doesn't exist!\n");
				else if(ReverseList(&L[i_num]) == ERROR) 
					printf("The list is empty!\n");
				else
					printf("Success!\n");

				getchar(); 
				getchar();
				break;

this is the function:

status ReverseList(LinkList *L)
//reverse the list
{
    if(L)
    {
        LinkList prev=NULL;
		LinkList cur=*L;
		LinkList next=NULL;
		while(cur)
		{
			next=cur->next;
			cur->next=prev;
			prev=cur;
			cur=next;
		}
		*L=prev;
		return OK;
	}
    else  
		return INFEASIBLE;
}

after running the func,the linked list didn't be reversed.

How come? 为什么 ReverseList 函数不起作用?

the reverselist func doesn't work
orz

答案1

得分: 1

在这些嵌套的if语句中

        case 15://ReverseList
            if(ReverseList(&L[i_num]) == INFEASIBLE) 
                printf("The list doesn't exist!\n");
            else if(ReverseList(&L[i_num]) == ERROR) 
                printf("The list is empty!\n");
            else
                printf("Success!\n");

对非空列表进行了两次反转。第一次是在这个if语句的条件中进行的

            if(ReverseList(&L[i_num]) == INFEASIBLE) 
                printf("The list doesn't exist!\n");

由于结果不是INFEASIBLE,下一个if语句接管了控制权

            else if(ReverseList(&L[i_num]) == ERROR) 
                printf("The list is empty!\n");

然后列表第二次反转。

你需要将函数调用的结果分配给一个变量,并在if语句中检查该变量。

注意函数不会返回ERROR。所以第二个if语句在任何情况下都没有意义。

所以你可以简单地写

case 15://ReverseList
    if(ReverseList(&L[i_num]) == INFEASIBLE) 
        printf("The list doesn't exist!\n");
    else
        printf("Success!\n");

或者

case 15://ReverseList
{
    status result = ReverseList(&L[i_num]);

    if( result == INFEASIBLE) 
        printf("The list doesn't exist!\n");
    else if( result == ERROR) 
        printf("The list is empty!\n");
    else
        printf("Success!\n");
}
//...
英文:

In these nested if statements

        case 15://ReverseList
            if(ReverseList(&L[i_num]) == INFEASIBLE) 
                printf("The list doesn't exist!\n");
            else if(ReverseList(&L[i_num]) == ERROR) 
                printf("The list is empty!\n");
            else
                printf("Success!\n");

a non-empty list is reversed twice. The first one it is reversed in the condition of this if statement

            if(ReverseList(&L[i_num]) == INFEASIBLE) 
                printf("The list doesn't exist!\n");

As the result is not INFEASIBLE then the next if statement gets the control

            else if(ReverseList(&L[i_num]) == ERROR) 
                printf("The list is empty!\n");

and the list is reversed the second time.

You need to assign the result of the function call to a variable and check the variable in the if statements.

Pay attention to that the function does not return ERROR. So the second if statement in any case does not make sense.

So you could just write

case 15://ReverseList
    if(ReverseList(&L[i_num]) == INFEASIBLE) 
        printf("The list doesn't exist!\n");
    else
        printf("Success!\n");

or

case 15://ReverseList
{
    status result = ReverseList(&L[i_num]);

    if( result == INFEASIBLE) 
        printf("The list doesn't exist!\n");
    else if( result == ERROR) 
        printf("The list is empty!\n");
    else
        printf("Success!\n");
}
//...

huangapple
  • 本文由 发表于 2023年4月6日 21:16:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定