C++链表,堆栈(某种)
#include <iostream>
using namespace std;
struct Node
{
int item; // storage for the node's item
Node* next; // pointer to the next node
};
/**************
use reference
**************/
void 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;
}
int main()
{
int a, count = 0;
int data;
char callen;
Node *head = NULL;
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 >> callen;
}while( callen != 'n' );
// assuming this is the print function
while(head != NULL)
{
cout << "output" << head->item << endl;
head = head->next; //next element
}
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
};
/**************
use reference
**************/
void 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;
}
int main()
{
int a, count = 0;
int data;
char callen;
Node *head = NULL;
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 >> callen;
}while( callen != 'n' );
// assuming this is the print function
while(head != NULL)
{
cout << "output" << head->item << endl;
head = head->next; //next element
}
system("pause");
return 0;
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 do-while 循环中尝试这个
而不是
另外,更改 addNode 的签名,如下所示:
In the do-while loop try this
instead of
Also, change the signature of addNode as follows:
该代码不应编译,因为您在
addNode
中使用变量head
,但head
是main
的本地变量。The code shouldn't compile because you are using the variable
head
inaddNode
buthead
is local tomain
.您可以使用 std::stack 进行后进先出。
You could use std::stack for LIFO.
听起来您正在尝试了解一些有关链接列表的知识。惊人的!
无论如何,我不会给你确切的答案,但我会用伪代码给你一些指示,特别是对于你的 addNode 成员函数:
作为视觉:
Sounds like you're trying to learn a bit about link lists. Awesome!
Anyway, I'm not going to give you the exact answer, but I'll give you some pointers in pseudo code, in particular for your addNode member function:
As a visual:
您这样做的方式,通过将 addNode 函数实现为推送操作,已经移动了头部,因此头部将始终指向您添加的最后一个元素。
因此,要实现删除最后添加的元素的功能,只需要编写一个简单的pop操作:复制头部地址,将第二个元素作为新的头部,并释放复制地址处的内存:
The way you're doing it, by implementing your addNode function as a push operation, already moves the head around, so the head will always point to the last element you added.
Therefore, to implement a function to delete the last element added, you just need to write a simple pop operation: copy the address of the head, make the second element the new head, and release the memory at the copied address:
您可以尝试以下修改后的代码。
You could try the following modified code.