【发布时间】:2011-04-16 20:33:56
【问题描述】:
#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;
}
好吧,我尝试在列表中添加一个新元素,我将如何像 LIFO 内存(堆栈)一样移动头部,所以最后一个元素位于最顶部..
任何帮助将不胜感激!指针和节点最近在搞乱我的大脑......
【问题讨论】:
-
要使用代码按钮
{},您必须选择所有代码。 -
下次请使用更合适的语言。
-
一个简单的 google 搜索可以显示很多 C++ 已经完成的示例
-
我想自己做,所以我知道如何从头开始做
-
更新了...试图把所有东西都放在一个函数中
标签: c++ linked-list self