C ++链表指针始终为nullptr

时间:2019-09-16 13:37:14

标签: 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 )循环...

2 个答案:

答案 0 :(得分:2)

函数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

答案 1 :(得分:1)

你做

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

但是nombre的类型定义为Chiffre*。然后在此功能中设置

head = newChiffre

这不会反映在调用函数中。您传递一个指针,然后将指针更改为指向其他位置。通常head在调用代码中保持为空。

可能的解决方法包括接收Nombre &headNombre &tail。但是。指针的typedef不能使代码易于阅读。