我有一个基本问题。整数存储[] = {8,6,4,2}。为什么打印 2 4 6 8 而不是 8 6 4 2?你能解释一下为什么吗?代码的哪一部分导致它?我无法理解。
这是代码:
#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;
}