【发布时间】:2017-06-02 07:35:15
【问题描述】:
我做的代码是这样的:
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 main 但我已经尝试过了,它仍然没有输出任何内容。
【问题讨论】:
-
int x = 1; x > 10?
标签: c++ loops for-loop linked-list doubly-linked-list