#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;
}好的,我试着在列表中添加一个新元素,我如何像后进先出存储器(栈)那样移动头部,使最后一个元素在最上面。
任何帮助都将不胜感激!指针和节点最近弄乱了我的大脑……
发布于 2011-04-17 05:38:54
由于我不确定每一个答案都能完全回答这个问题,这里有一个链表实现(没有测试:
// your (correct) structure
struct Node
{
float item; // storage for the node's item
Node* next; // pointer to the next node
};现在我们需要两个指针来查看列表:
/* some pointers */
struct List
{
Node* head;
Node* tail;
};现在我们需要创建一些元素。正如其他人所说,您可以使用new:
/* create some elements we want to link in */
Node* elem1 = new Node();
Node* elem2 = new Node();
Node* elem3 = new Node();
/* maybe even set their properties! */
elem1->item = 3.14;
elem2->item = 3.14;
elem3->item = 3.14;注意到我还没有尝试将这些元素添加到列表中吗?这是因为我在脑海中有一个函数,看起来像这样:
void addtolist(List &list, Node* node)
{
/* if no head, initialise the list */
if ( list->head == NULL )
{
list->head = node;
list->tail = node;
}
else if ( list->head != NULL && list->tail != NULL )
{
/* access the tail element and set its
next to this ptr.
Move tail to this node */
list->tail->next = node;
list->tail = node;
}
else
{
/* probably raise an exception! */
}
}你可以这样调用它:
List l;
addtolist(l, elem1); /* etc */删除元素有点麻烦,因为你必须去那个元素,记住它的前一个元素,抓取它的下一个元素,把它们连接起来,然后删除你所在的Node*。
现在开始遍历列表..。您的术语HEAD|TAIL让我想起了Erlang和尾递归,其中当前元素称为头部,其余元素称为尾部。如果我写道:
Node* cur = l.head;
while ( cur != NULL )
{
// do something with cur.item ?
cur = cur->next;
}你可以看到这一切的发生。在这里用head替换cur是无害的,这要归功于List结构。
最后,我在这里使用了一种非常类似C的方法,但也有模板的余地:
template<typename T>
struct node
{
T item; // storage for the node's item
Node<T>* next; // pointer to the next node
};并将List结构封装为一个类:
template<typename T>
class List
{
protected:
Node<T>* head;
Node<T>* tail;
public:
void addtolist(Node<T>* node);
Node<T>* gethead();
Node<T>* gettail();
}这会让你更接近std::list。
发布于 2011-04-17 04:39:02
temp是一个未初始化的指针。所以-
temp-> item = a; // temp is not initialized or pointing to a memory location
// that has Node object to use operator ->首先,需要使用new为temp分配内存位置。
temp = new Node;
temp -> item = a;现在将其指定为head。类似地,在while循环中也为子节点分配内存。并在程序终止前,使用delete将从子级获取的所有资源返回给头部。
发布于 2011-04-17 04:45:23
此外,请注意,您正在执行从int到float的隐式强制转换
temp-> item = a;因为a是一个int,而temp->item是一个double。
要解决您的问题:您希望在访问temp之前分配一个新结构,因此
temp = new Node();https://stackoverflow.com/questions/5689432
复制相似问题