C++链表指针总是nullptr

标签 c++ list pointers reference singly-linked-list

<分区>

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

我被要求完成 lecture_nombredisplay_nombre 功能.... 但是,问题主要出在程序没有进入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 )循环...

最佳答案

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

关于C++链表指针总是nullptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57958070/

相关文章:

c++ - 取消引用运算符和函数的问题

c++ - Crypto++ 无法解释的崩溃

c++ - 有没有办法使用核心文件找到泄漏的内存?

java - JNI 字符串返回值

c++ - OpenGL:vertexArray 与 glBegin()

c - C语言中如何使用指针合并两个数组

java - 如何在第一个列表中按另一个列表对一个列表进行排序?

java - 如何在java8中更改字符串列表中的项目

c# - 匹配来自两个列表(或数组)的项目

c - 什么情况下数组会衰减为指针?