【发布时间】:2014-02-27 22:10:02
【问题描述】:
我正在尝试打印链接列表中的节点(正向)。 定义如下:
struct Node {
string val;
Node* next;
Node* prev;
};
struct Stew {
Node* first;
Node* last;
};
其中 Stew 有两个特殊的指针,一个指向第一个元素,一个指向最后一个元素。
我很肯定我的尝试是正确的,但实际上并非如此。
void print (const Stew& q, char direction) {
assert (!isEmpty(q));
{
Node* current = new Node;
current = q.first;
cout << current -> val;
while((current -> next) != NULL)
{
current = current -> next;
cout << (current -> val);
}
delete current;
}
我知道其中存在逻辑错误,但我似乎无法确定它。任何帮助将不胜感激。
【问题讨论】:
-
您是否考虑过对其进行调试,或者甚至绘制带有方框和箭头的图表来检查您的操作是否正确?
-
你为什么要创建一个新节点只是为了打印你的链表?
-
调试这个问题的最好方法是画一张图,然后指出问题所在。
-
@PaulMcKenzie 目的是让我的代码更清晰,以表明我有另一个节点正在扫描列表
-
你为什么要删除节点只是为了打印你的链表?
标签: c++ printing linked-list c++98