C++链表指针总是nullptr

C++ linked list pointer always nullptr

我正在尝试解决这个练习,但我找不到我的错误。这个练习要求我们处理任意大小的正整数。为此,我们通过首先存储低权重数字来选择数字简单链表形式的表示。

我被要求完成 lecture_nombredisplay_nombre 函数.... 不过问题出在main上,程序没有进入if( n != nullptr )循环

这是我的代码:

#include <iostream>
#include <cctype>
#include <fstream>
using namespace std;

struct Chiffre {
    unsigned int chiffre_;   /**< single number between 0 et 9 */
    Chiffre * suivant_;      /**< pointer towards the next number with a heavier weight (for example 2 for 25 ou nullptr */
};

typedef Chiffre* Nombre;
void insertNode(unsigned int n, Nombre head, Nombre tail);
Nombre lecture_nombre( std::istream & in );
void display_nombre( Nombre n, std::ostream & out );

// The main is given by the teacher
int main(){
    while( true ) {
        Nombre n = lecture_nombre( std::cin );
        if( n != nullptr ) {
            std::cout << "lu : ";
            display_nombre( n, std::cout );
            std::cout << "\n";
            //detruit_nombre( n );
        }
        else break;
    }
    return 0;
}
 // d is a single digit number and I have to add it into the chained list and return the results
    Nombre lecture_nombre( std::istream & in )
    {
    *//Nombre res = nullptr;
    Nombre head = nullptr;
    Nombre tail = nullptr;

    while( in.good() ) {
        char c = in.get();
        if( std::isdigit( c )) {
            unsigned int d = c - '0';
            // my code starts here :
            insertNode(d,head,tail);

        }
        else break;
    }
    return head;
}
// my code starts here
void insertNode(unsigned int n, Nombre head, Nombre tail){
    struct Chiffre *newChiffre = new Chiffre;
    newChiffre->chiffre_ = n;
    newChiffre->suivant_ = nullptr;
    cout << "Insert Node :" << newChiffre->chiffre_ << endl;
    if (head==nullptr){
        head = newChiffre;
        tail = newChiffre;
    }else{
        tail->suivant_ = newChiffre;
        tail = tail->suivant_;
    }
}

void display_number( Nombre n, std::ostream & out ){
    if(n==nullptr){
        cout << n << endl;
    }else{
        cout << n->chiffre_ <<endl;
        display_number(n->suivant_,out);
    }
}

我确认节点已创建但我无法显示数字,程序将 n 作为 nullptr 所以它从未进入 if( n != nullptr ) 循环...

你会

void insertNode(unsigned int n, Nombre head, Nombre tail){

但是 nombre 的类型定义为 Chiffre*。然后在这个函数中设置

head = newChiffre

这不会反映在调用函数中。您传递一个指针,然后将指针更改为指向其他地方。 head在调用代码中保持为null是正常的。

可能的修复包括接收 Nombre &headNombre &tail。但是哎呀。指针的类型定义不利于代码阅读。

函数的参数insertNode是函数的局部变量,由原始参数值的副本初始化。更改对象的副本不会影响原始对象的值。

因此将参数声明为具有引用类型,例如

void insertNode(unsigned int n, Nombre &head, Nombre &tail){
    struct Chiffre *newChiffre = new Chiffre;
    newChiffre->chiffre_ = n;
    newChiffre->suivant_ = nullptr;
    cout << "Insert Node :" << newChiffre->chiffre_ << endl;
    if (head==nullptr){
        head = newChiffre;
        tail = newChiffre;
    }else{
        tail->suivant_ = newChiffre;
        tail = tail->suivant_;
    }
}

而且函数 display_number 不使用它的参数 out。 它可以按照以下方式定义,如演示程序所示

#include <iostream>

struct Chiffre {
    unsigned int chiffre_;   /**< single number between 0 et 9 */
    Chiffre * suivant_;      /**< pointer towards the next number with a heavier weight (for example 2 for 25 ou nullptr */
};

typedef Chiffre* Nombre;

void insertNode(unsigned int n, Nombre &head, Nombre &tail)
{
    if ( head == nullptr )
    {
        head = tail = new Chiffre { n, nullptr };
    }
    else
    {
        tail = tail->suivant_ = new Chiffre { n, nullptr };
    }
}

std::ostream & display_number( Nombre n, std::ostream &out = std::cout )
{
    if ( n == nullptr )
    {
        return out << "nullptr";
    }
    else
    {
        out << n->chiffre_ << " -> ";
        return display_number( n->suivant_, out );
    }
}

int main() 
{
    Nombre head = nullptr, tail = nullptr;
    const int N = 10;

    for ( int i = 0; i < N; i++ ) insertNode( i, head, tail );

    display_number( head ) << '\n';

    return 0;
}

程序输出

0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> nullptr