【问题标题】:Linked List C++ Node链表 C++ 节点
【发布时间】:2016-02-27 13:14:33
【问题描述】:

这是c++中链表的代码:-

#include<iostream>
using namespace std;
class node{
    public:
    int data;
    node *next;
};

void insertdata(int element ,node *head){

    node *newnode = new node();
    newnode -> data = element;
    newnode -> next = NULL;

    node *itr;
    itr=head;
    if(head == NULL)
    head = newnode;
    else{

    while(itr->next != NULL){
        itr = itr-> next;
    }

    itr->next = newnode;
}
}

void print(node *head){
    node *print = head;
    while(print != NULL){
        cout<<print->data<<"\n";
        print = print -> next;
    }
}

int main(){
    node *head = NULL;
    insertdata(10,head);
        insertdata(20,head);
            insertdata(30,head);
            print(head);
            return 0;
}

如果我将 head 声明为全局对象,这不起作用。我必须在全球范围内声明它的原因是什么。我是 B.Tech 学生二年级第一次学习它,所以看起来很混乱。

【问题讨论】:

  • 你应该正确地格式化你的代码。

标签: c++ linked-list


【解决方案1】:

在C++中,函数的参数是传值的,所以在被调用者中修改参数不会影响调用者的局部变量。

引用可以用来修改调用者的局部变量。

#include<iostream>
using namespace std;
class node{
    public:
    int data;
    node *next;
};

void insertdata(int element ,node *&head){

    node *newnode = new node();
    newnode -> data = element;
    newnode -> next = NULL;

    node *itr;
    itr=head;
    if(head == NULL)
    head = newnode;
    else{

        while(itr->next != NULL){
            itr = itr-> next;
        }

        itr->next = newnode;
    }
}

void print(node *head){
    node *print = head;
    while(print != NULL){
        cout<<print->data<<"\n";
        print = print -> next;
    }
}

int main(){
    node *head = NULL;
    insertdata(10,head);
    insertdata(20,head);
    insertdata(30,head);
    print(head);
    // do delete what you created via new
    while(head != NULL){
        node *next = head->next;
        delete head;
        head = next;
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    对于初学者来说,不需要用类键类声明节点。用类键结构声明它更自然。例如

    struct node
    {
        int data;
        node *next;
    };
    

    至于函数insertdata 那么函数参数就是它的局部变量。局部变量的任何更改都不会影响用作参数的原始对象。

    因此,您应该将第二个参数声明为对node * 的引用,例如

    void insertdata( int element, node * &head );
    

    或作为指向node *的指针

    void insertdata( int element, node **head );
    

    在第一种情况下,函数定义可能如下所示

    void insertdata( int element, node * &head )
    {
        node *newnode = new node { element, nullptr };
    
        if ( head == nullptr )
        {
            head = newnode;
        }
        else
        {
            node *itr = head;
            while ( itr->next != nullptr ) itr = itr->next;
            itr->next = newnode;
        }
    }
    

    在第二种情况下,函数定义可能如下所示

    void insertdata( int element, node **head )
    {
        while ( *head ) head = &( *head )->next;
    
        *head = new node { element, nullptr };
    }
    

    考虑到通常新节点会插入到单链表的开头。

    第一个参数最好指向列表的头部。

    这是一个基于使用第二个函数定义的演示程序。

    #include <iostream>
    
    struct node
    {
        int data;
        node *next;
    };
    
    void insertdata( node **head, int element )
    {
        while ( *head ) head = &( *head )->next;
    
        *head = new node { element, nullptr };
    }
    
    std::ostream & print( node *head, std::ostream &os = std::cout )
    {
        for ( node *current = head; current != nullptr; current = current->next )
        {
            os << current->data << ' ';
        }
    
        return os;
    }
    
    
    int main()
    {
        node *head = nullptr;
    
        for ( int x : { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } ) insertdata( &head, x );
    
        print( head ) << std::endl;
    }        
    

    它的输出是

    1 2 3 4 5 6 7 8 9 10
    

    【讨论】:

      猜你喜欢
      • 2023-01-25
      • 2019-04-26
      • 2019-05-10
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多