这是结构体的定义:
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)
//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;
}
运行函数后,链表没有被反转
如何来吗?:)
reversellist函数不起作用下跪
在这些嵌套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");
如果结果不是in可行的,那么下一个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");
}
//...