C++,学习链表

标签 c++ linked-list

我正在学习一个非常简单的链表,
但是下面这段代码不起作用,当我输入 1 和 2 时,系统输出只有 1 和 NULL。
有人可以帮我吗?
这是我的代码。

#include <iostream>  

using namespace std;  

struct node  
{  
    char username[10];  

    node *next;  
};  



int main()  
{  
    node *head;  
    node *place;  
    node *tail;  

    head = new node;  
    place = head;  
    tail = place->next;  

    cout<<"username 1 ";  
    cin>>place->username;  
    place = tail;  
    place = new node;  
    tail = place->next;  
    cout<<"username 2 ";  
    cin>>place->username;  

    place = head;  
    cout<<"username 1 ";  
    cout<<place->username<<endl;  
    place = place->next;  
    cout<<"username 2 ";  
    cout<<place->username<<endl;  


    return 17;  
}  

感谢您的任何帮助,谢谢。

最佳答案

您永远不会将节点链接在一起。 在整个程序中,您没有设置next 字段。 在纸上解决这个问题,一次一个声明。 而不是……

place = new node;  
tail = place->next;

创建新节点时,将其添加到列表的末尾:将当前端链接到新节点,然后移动结束指针。

place = new node;  
tail->next = place;
tail = place;

关于C++,学习链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33448422/

相关文章:

c++ - std::equal_range 不适用于具有 operator< 定义的结构

c++ - 提高SQLite每秒更新的性能?

java - 如何对链表节点数组进行从高到低排序(基于float值)

c++ - 如何计算指针的哈希值?

java - BJP5练习16.7 : deleteBack — Help me understand the solution

c++ - 如何将二叉搜索树转换为双向链表?

c++ - 使用 cURLpp 进行多次下载的进度指示器

c++ - 如何禁用 QTabWidget 中更改选项卡?

C++ 11内存模型和在不同线程中访问同一结构的不同成员

algorithm - 棘手的链表问题