Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C++中添加节点_C++_Linked List - Fatal编程技术网

在C++中添加节点

在C++中添加节点,c++,linked-list,C++,Linked List,为什么这个代码不起作用?我从一门课程中借用了它,后来我尝试用C语言制作另一个,但它再次显示了相同的错误 #include<iostream> #include<conio.h> #include<stdlib.h> #include<string.h> using namespace std; struct Nodo{ int dato; struct Nodo *next; }; void InsertarLista(Nod

为什么这个代码不起作用?我从一门课程中借用了它,后来我尝试用C语言制作另一个,但它再次显示了相同的错误

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

using namespace std;

struct Nodo{
    int dato;
    struct Nodo *next;
};

void InsertarLista(Nodo *&lista, int n);


main(){
    struct Nodo *lista=NULL;
    int n;
    int opcion;
    
    do{
    cout<<"Ingrese un numero:";
    cin>>n;
    InsertarLista(lista,n);
    getch();
    fflush(stdin);
    cout<<"\nDesea agregar otro elemento a la lista?: (1) si (2) no :\n";
    cin>>opcion;
    }while(opcion!=2);


    
    getch();
    return 0;
}

void InsertarLista(Nodo *&lista, int n){
    Nodo *nuevo = new Nodo();
    nuevo->dato=n;
    
    Nodo *aux1=lista;
    Nodo *aux2;
    
    
    while((aux1->dato<(n)) && (aux1!=NULL)){
        aux2=aux1;
        aux1=aux1->next;    
    }
    
    if(lista==aux1){
        lista=nuevo;
    }else{
        aux2->next=nuevo;
    }
    
    nuevo->next=aux1;
    
    cout<<"elemento"<<n<<" agregado a la lista correctamente.\n";
}
我以前做过队列和堆栈,我只是不明白为什么这个问题只发生在链表上

下面是我发布的错误截图:

进入InsertarLista后,lista最初为空。将lista分配给aux1,然后在检查aux1是否为NULL之前尝试访问aux1->dato。之后您将检查NULL

更改此项:

while((aux1->dato<(n)) && (aux1!=NULL)){
对此,请改为:

while((aux1!=NULL) && (aux1->dato<n)){

从左到右(而不是从右到左)计算其操作数。由于,仅当左操作数首先计算为true时,才会计算右操作数。

您得到了什么错误。。。代码不工作是没有意义的请不要发布代码/错误的图像。而是将代码/错误作为文本发布到代码块中。看见此外,要想知道这句话是怎么说的,就必须先把它转录下来,然后再翻译。这让潜在的回答者更难回答。非常感谢你刚刚救了我的命。