#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內存(棧),所以最後一個元素是在最頂端..鏈表C++,問題自學習
任何幫助將不勝感激!指針和節點最近都在和我的大腦搞混......
要使用代碼按鈕「{}」,您必須選擇所有代碼。 – 6502 2011-04-16 20:43:33
下次請使用更合適的語言。 – 2011-04-16 20:44:32
一個簡單的谷歌搜索可能會告訴你很多C++已經完成的例子 – dynamic 2011-04-16 21:50:01