C++11 怎样!不同于=

C++11 怎样!不同于=,c++11,operators,C++11,Operators,我正在编写函数,其目标是两人一组打印列表,例如: 名单:123456 配对名单:2114365 我编写了以下代码: printPair(){ bool flag = 1; node *temp = new node(); node *temp2 = new node(); temp2 = NULL; if(!head) { printf("Empty List!!"); return 0;

我正在编写函数,其目标是两人一组打印列表,例如:

名单:123456

配对名单:2114365

我编写了以下代码:

printPair(){
    bool flag = 1;
    node *temp = new node();
    node *temp2 = new node();
    temp2 = NULL;

    if(!head)
    {
        printf("Empty List!!");
        return 0;
    }
    temp = head;

    while(!temp && !temp->next)  //Error here 1.
    {
        if(!temp2)  //Error here 2.
            temp2->next->next = temp->next;
        temp2 = temp->next;
        temp->next = temp->next->next;
        temp2->next = temp;
        if(flag)
        {
            head = temp2;
            flag = 0;
        }
        temp = temp->next;
    }
}
如果替换为以下内容,则效果良好:

1. `while(temp !=NULL && temp->next !=NULL)`
2. `if(temp2 != NULL)`
这是怎样的
不同=

而(!temp){}
将在temp的布尔值为false时执行

while(temp!=null)
将在temp具有除null以外的任何值时执行 (如果temp为真,它也会通过)


您希望使用while(temp!=null),因为您要遵循->下一个引用,直到到达列表中的最后一项(temp->next nothing)

嗯,它们是两个不同的运算符。您会遇到什么错误?当我使用相同的代码时:1.while(!temp&&!temp->next)2.如果(!temp2)输出与旧列表相同。例如:1 2 3 4输出:1 2 3 4但如果我用1.while(temp!=NULL&&temp->next!=NULL)2.如果(temp!=NULL)它给出所需的输出:输入:1 2 3 4输出:2 1 4 3请看我的答案。它解释了为什么会出现以下错误以及这两个运算符之间的差异