• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c++ - C++中的简单链表

[复制链接]
菜鸟教程小白 发表于 2022-8-3 00:18:05 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我即将创建一个可以插入和显示的链接,直到现在:

struct Node {
    int x;
    Node *next;
};

这是我的初始化函数,它只会被第一个 Node 调用:
void initNode(struct Node *head, int n){
    head->x = n;
    head->next = NULL;
}

添加 Node ,我认为我的链表无法正常工作的原因在于这个函数:
void addNode(struct Node *head, int n){
    struct Node *NewNode = new Node;
    NewNode-> x = n;
    NewNode -> next = head;
    head = NewNode;
}

我的 main功能:
int _tmain(int argc, _TCHAR* argv[])
{
    struct Node *head = new Node;

    initNode(head, 5);
    addNode(head, 10);
    addNode(head, 20);
    return 0;
}

让我按照我认为可行的方式运行该程序。首先我初始化头部 Node作为 Node像这样:
head = [ 5 |  NULL ]

然后我添加一个 n = 10 的新节点并将 head 作为我的参数传递。

新节点 = [ x | next ] next 指向头部。然后我更改 head 指向 NewNode 的位置,因为 NewNode 现在是 LinkedList 中的第一个节点。

为什么这不起作用?我将不胜感激任何可以使我朝着正确方向前进的提示。我认为 LinkedList 有点难以理解。

当我打印这个时,它只返回 5:



Best Answer-推荐答案


这是我能想到的最简单的例子,没有经过测试。请注意,这使用了一些不好的做法,并且与您通常使用 C++ 的方式不同(初始化列表、声明和定义的分离等)。但这是我无法在此涵盖的主题。

#include <iostream>
using namespace std;

class LinkedList{
    // Struct inside the class LinkedList
    // This is one node which is not needed by the caller. It is just
    // for internal work.
    struct Node {
        int x;
        Node *next;
    };

// public member
public:
    // constructor
    LinkedList(){
        head = NULL; // set head to NULL
    }

    // destructor
    ~LinkedList(){
        Node *next = head;
        
        while(next) {              // iterate over all elements
            Node *deleteMe = next;
            next = next->next;     // save pointer to the next element
            delete deleteMe;       // delete the current entry
        }
    }
    
    // This prepends a new value at the beginning of the list
    void addValue(int val){
        Node *n = new Node();   // create new Node
        n->x = val;             // set value
        n->next = head;         // make the node point to the next node.
                                //  If the list is empty, this is NULL, so the end of the list --> OK
        head = n;               // last but not least, make the head point at the new node.
    }

    // returns the first element in the list and deletes the Node.
    // caution, no error-checking here!
    int popValue(){
        Node *n = head;
        int ret = n->x;

        head = head->next;
        delete n;
        return ret;
    }

// private member
private:
    Node *head; // this is the private member variable. It is just a pointer to the first Node
};

int main() {
    LinkedList list;

    list.addValue(5);
    list.addValue(10);
    list.addValue(20);

    cout << list.popValue() << endl;
    cout << list.popValue() << endl;
    cout << list.popValue() << endl;
    // because there is no error checking in popValue(), the following
    // is undefined behavior. Probably the program will crash, because
    // there are no more values in the list.
    // cout << list.popValue() << endl;
    return 0;
}
我强烈建议您阅读一些有关 C++ 和面向对象编程的内容。一个好的起点可能是这样的:http://www.galileocomputing.de/1278?GPP=opoo
编辑:添加了一个弹出功能和一些输出。如您所见,程序推送 3 个值 5、10、20,然后弹出它们。之后顺序颠倒,因为此列表在堆栈模式下工作(LIFO,后进先出)

关于c++ - C++中的简单链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22141477/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap