链表 C++ 、问题自学
#include <iostream>
using namespace std;
struct Node
{
int item; // storage for the node's item
Node* next; // pointer to the next node
};
Node* addNode(Node*& head, int data , int& count)
{
Node * q; // new node
q = new Node; // allocate memory for the new mode
q->item = data; // inserting data for the new node
q->next = head; // point to previous node ?? how would i do that? ( am i doing it correctly?)
count++; // keep track of number of node
head = q;
return q;
}
int main()
{
int a, count=0;
int data;
bool repeat;
Node *head= NULL;
//^^ assuming thats creating the first node ^^
do
{
cout << "please enter the data for the next node" <<endl;
cin >> data;
addNode(head, data, count);
cout << "do you wish to enter another node? (enter true or false)" << endl;
cin >>repeat;
}
while (repeat == true);
// assuming this is the print function
while(head != NULL)
{
cout << "output" << temp->item << endl;
cout << temp->next << endl;
}
system("pause");
return 0;
}
好吧,我尝试在列表中添加一个新元素,我如何像后进先出内存(堆栈)一样移动头部,所以最后一个元素位于最顶部..
任何帮助将不胜感激!指针和节点最近弄乱了我的大脑......
#include <iostream>
using namespace std;
struct Node
{
int item; // storage for the node's item
Node* next; // pointer to the next node
};
Node* addNode(Node*& head, int data , int& count)
{
Node * q; // new node
q = new Node; // allocate memory for the new mode
q->item = data; // inserting data for the new node
q->next = head; // point to previous node ?? how would i do that? ( am i doing it correctly?)
count++; // keep track of number of node
head = q;
return q;
}
int main()
{
int a, count=0;
int data;
bool repeat;
Node *head= NULL;
//^^ assuming thats creating the first node ^^
do
{
cout << "please enter the data for the next node" <<endl;
cin >> data;
addNode(head, data, count);
cout << "do you wish to enter another node? (enter true or false)" << endl;
cin >>repeat;
}
while (repeat == true);
// assuming this is the print function
while(head != NULL)
{
cout << "output" << temp->item << endl;
cout << temp->next << endl;
}
system("pause");
return 0;
}
okey i tried adding a new element in the list how would i move the head around like a LIFO memory (stack) so the last element is on the very top..
any help would be appreciated ! The pointers and the nodes are messing with my brain lately ....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
temp
是一个未初始化的指针。因此 -首先,需要使用
new
为temp
分配内存位置。现在分配它
head
。类似地,也在 while 循环中为子节点分配内存。并在程序终止前使用delete
将从child获取的所有资源返回到head。temp
is an uninitialized pointer. So -First,
temp
needs to be allocated memory location usingnew
.And now assign it
head
. Similarly allocate memory for the child nodes too in thewhile
loop. And return all the resources acquired from child to head usingdelete
before program termination.您在这里似乎有一些误解:
您的“头”是列表的开始。这永远是开始。
通过将元素分配给最后一个节点的
next
指针,可以add将元素追加到链表中。第三,你没有分配任何东西。
现在...要添加下一个内容,您要么需要跟踪最后一个节点(尾部),要么遍历列表以查找最后一个节点。
这是 O(n) 的执行时间。通过跟踪尾部,您可以使其 O(1):
编辑: 正如所指出的,如果您想添加到列表中,您可以:
You seem to have some misunderstandings here:
Your "head" is the start of the list. It's always the start.
You
addappend elements to a linked list by assigning them to the last node'snext
pointer.Third, you're not allocating anything.
Now ... to add the next thing, you either need to keep track of the last node (tail), or traverse the list to find the last node.
This is O(n) execution time. By keeping track of the tail you make it O(1):
EDIT: As pointed out, if you want to prepend to the list, you would:
因为我不确定每个答案都完全回答它,所以这里有一个链表实现(没有 testig 编写:
现在我们需要两个指针来照顾列表:
现在我们需要创建一些元素。正如其他人所说,你可以这样做 您
注意到我还没有尝试将这些元素添加到列表中吗?那是因为我想到了一个如下所示的函数:
可以通过这样做来调用它:
删除元素有点棘手,因为你必须转到该元素,所以记住它的前一个元素,抓住它的下一个元素,将它们连接起来并删除你所在的 Node*
现在遍历列表......你的术语
HEAD|TAIL
让我想起了 Erlang 和尾递归,其中当前元素被称为头部,其余元素被称为尾部。 如果我写:你可以看到用
head< 替换
cur
。由于List
结构,这里的 /code> 是无害的。最后,我在这里使用了一种非常类似于 C 的方法,但还有模板的范围:
并封装
List
。 > struct 作为一个类:这让您更接近
std::list
。Since I'm not sure every answer completely answers it, here's a linked list implementation (written without testig:
Now we need two pointers somewhere to look after the list:
Now we need to create some elements. As others have said, you can do that with new:
Notice how I didn't try to add these elements to a list yet? That's because I've got a function in mind which looks like this:
You can call this by doing this:
Deleting elements is somewhat more tricky, since you have to go to that element, remember its previous element, grab it's next element, join them up and delete the Node* you're on.
Now for traversing lists... your terminology
HEAD|TAIL
reminds me of Erlang and tail recursion, where the current element is referred to as the head and the remainder the tail. If I write:You can see this happening. Replacing
cur
withhead
here would be harmless thanks to theList
struct.Finally, I've used a very C-like approach here, but there's scope for templates:
and encapsulating the
List
struct as a class:Which brings you a little bit closer to
std::list
.另外请注意,您正在执行从
int
到float
的隐式转换,因为
a
是int
,而 < code>temp->item 是一个double
。解决你的问题:你想在访问
temp
之前分配一个新的结构,因此Additionally note that you are doing an implicit cast from
int
tofloat
onas
a
is anint
, whiletemp->item
is adouble
.To solve your problem: You want to allocate a new structure before accessing
temp
, thus