,我所做的代码是这样的:如何打印一个简单的链表(C++)?
struct node
{
int value;
node *prev;
node *next;
};
void play()
{
node *head = NULL, *temp = NULL, *run = NULL;
for (int x = 1; x > 10; x++)
{
temp = new node(); //Make a new node
temp -> value = x; //Assign value of new node
temp -> prev = NULL; //Previous node (node before current node)
temp -> next = NULL; //Next node (node after current node)
}
if (head == NULL)
{
head = temp; //Head -> Temp
}
else
{
run = head; //Run -> Head
while (run -> next != NULL)
{
run = run -> next; //Go from node to node
}
run -> next = temp; //If next node is null, next node makes a new temp
temp -> prev = run;
}
run = head; //Play from start again
while (run != NULL) //Printing
{
printf("%d\n", run -> value);
run = run -> next;
}
}
int main()
{
play();
system ("pause");
return 0;
}
但是,它不工作。没有输出(完全空白)。我怎样才能让这个链表正确打印?我希望它输出:
1 2 3 4 5 6 7 8 9 10
,我还有其他的选择是让为印刷另一个单独的函数或将整个事情为int主,但我已经试过了,它仍然没有做任何输出。
'int x = 1; x> 10'? –
您应该使用[std :: list](http://en.cppreference.com/w/cpp/container/list)。如果它是一项家庭作业,请编写自己的'void output_list(struct node * list);'函数。在所有情况下,使用[GCC](http://gcc.gnu.org/)编译所有警告和调试信息('g ++ -Wall -Wextra -g')并学习如何使用调试器**' gdb'。 –