I am having trouble in C++ putting and displaying array into a linked list. I'm having lots of trouble with linked lists in general
Here is my code. It only displays the first number. (It should display 19 21 17 22 33)
#include <iostream>
using namespace std;
struct node {
int age;
node *next;
};
void display(node *t)
{
node *temp = t;
cout << temp->age << " ";
}
int main()
{
int Age[5] = { 19, 21, 17, 22, 33 };
node* List = new node;
List = NULL;
node* temp2 = List;
while (temp2 != NULL)
{
temp2 = temp2->next;
node* List = new node;
List = NULL;
node* temp2 = List;
}
for (int i = 0; i < 5; i++)
{
temp2 = new node;
temp2 -> age = Age[i];
temp2 -> next = NULL;
if (List == NULL)
List = temp2;
}
display(List);
system("PAUSE");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire