基本的な質問があります。int storage []={8,6,4,2}。なぜこれは246 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;
}