【发布时间】:2013-08-21 08:08:36
【问题描述】:
我正在尝试插入一些节点。我的实现基于斯坦福教程。 http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
以下是我的代码。
struct node
{
int p_data;
struct node* p_next;
node(node* head, int data)
{
p_next = head;
p_data = data;
}
explicit node(int data)
{
p_next = nullptr;
p_data = data;
}
}
这是我的插入函数
node* insert_node(node* head, int data)
{
return new node(head, data);
}
我想要做的是,我为初始设置了 1、2、3 的列表,并想添加更多元素,如 5、6、7。以下是我的尝试,但插入没有做任何事情。所以我只打印出1、2、3。在主函数中,我有...
struct node* head = new node(NULL);
struct node* nodep_01 = new node(NULL);
struct node* nodep_02 = new node(NULL);
head->p_data = 1;
head->p_next = nodep_01;
nodep_01->p_data = 2;
nodep_01->p_next = nodep_02;
nodep_02->p_data = 3;
nodep_02->p_next = nullptr;
所以如果我打印这个,我会得到 1、2、3。然后我尝试再插入一个值为 5 的元素,但它什么也没做。
insert_node(head, 5);
有人可以帮我做这件事吗?我想在此列表中插入元素...提前谢谢!
【问题讨论】:
-
您可能还对我对链表变体的调查感兴趣:rossbencina.com/code/singly-linked-list-data-structure-variants
标签: c++ pointers linked-list new-operator nodes