英文:
Why does the ReverseList function doesn't work?
问题
这是结构体的定义:
ElemType
和status
都等于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
函数不起作用。
orz
英文:
this is the definition of the struct:
ElemType
andstatus
are 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?
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");
}
//...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论