我有一个基本问题。 int storage [] = {8,6,4,2}。为什么打印2 4 6 8而不是8 6 4 2?你能解释为什么吗?哪部分代码会导致它?我无法理解。打印链接列表 - C++
下面是代码:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
struct node {
int info;
node *next;
node::node()
{}
node::node (const int & s, node * link)
: info(s), next (link)
{}
};
void DisplayList (node * head)
{
cout << "The list content is: ";
node * ptr = head;
while (ptr != NULL)
{
cout << ptr ->info << " ";
ptr = ptr->next;
}
cout << endl<<endl;
}
int main()
{
int storage[] = {8,6,4,2};
node *head = NULL;
node *temp = NULL;
for (int k=0; k < 4; k++) {
temp = new node();
temp->info = storage[k];
temp->next = head;
head = temp;
}
DisplayList (head);
cin.ignore();
cin.get();
return 0;
}
调试它,看看! – StoryTeller 2013-03-03 13:52:35
为什么不在调试器中单步执行代码?你会学到更多的方法...... – 2013-03-03 13:53:02
我甚至不知道如何调试。我只是一个初学者编码器。 – Nordin 2013-03-03 14:30:51