C 为什么验证方法不能正常工作?
我有一个方法可以在链表的开头插入:C 为什么验证方法不能正常工作?,c,C,我有一个方法可以在链表的开头插入: void insertBegin(int value) { struct node *var; var=(struct node *)malloc(sizeof (struct node)); var->data=value; if(head==NULL) { head=var; head->next=NULL; } else { va
void insertBegin(int value)
{
struct node *var;
var=(struct node *)malloc(sizeof (struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->next=NULL;
}
else
{
var->next=head;
head=var;
}
}
在主方法中,我使用上述方法在开始时插入一些元素:
int main{
int actual[] = {50, 70, 80, 100, 77, 200, 44, 70, 6, 0};
int expected[] = {0, 6, 70, 44, 200, 77, 100, 80, 70, 50};
for(i=0; i<listsize; i++){
insertBegin(actual[i]);
}
if(verify(expected))
printf("correct");
else
printf("incorrect");
return 0;
}
int main{
int-actual[]={50,70,80,100,77,200,44,70,6,0};
int预期[]={0,6,70,44,200,77,100,80,70,50};
对于(i=0;idata)
返回-1;
i=1;
温度=头部->下一步;
对于(i=0;i<10;i++){
对于(int j=0;j<10;j++){
如果(临时->数据==预期的[i])
返回true;
其他的
返回false;
}
}
返回0;
}
试试这个:
bool verify(int expected[]) {
struct node *temp = head;
int i = 0;
if(temp == NULL)
return false;
while(temp){
if(expected[i++] != temp->data)//if(i == listsize || expected[i++] != temp->data)
return false;
temp = temp->next;
}
return true;
}
你得到了什么输出?你期望什么样的产出。请阅读以下内容:验证方法正确时返回0,不正确时返回其他值。if语句检查反向
return-1
means返回true代码>intmain{
-->intmain(无效){
bool verify(int expected[]) {
struct node *temp = head;
int i = 0;
if(temp == NULL)
return false;
while(temp){
if(expected[i++] != temp->data)//if(i == listsize || expected[i++] != temp->data)
return false;
temp = temp->next;
}
return true;
}