Problem with creating a circular linked list
Hello. I've been making a circular linked list and I'm trying to assign each list a "name" and its next "link" but when I run my code it crashes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
Soldier *Head;
Head = NULL;
string names[] = {"Arman","Bogut","Castro","Damascus","Elene"};
for (int i = 0; i < 5; ++i)
{
Soldier *newsoldier = new Soldier;
newsoldier->link = NULL;
newsoldier->name = names[i];
Soldier *tmp;
tmp = Head;
if (tmp != NULL) //if not first
{
while (tmp->link != NULL)
tmp = tmp->link;
tmp->link = newsoldier;
}
else
{
cout << "hello";
Head = newsoldier;
}
cout << tmp->name << endl;
}
|
1 2 3
|
tmp = Head;
//...
cout << tmp->name << endl;
|
What will happen if Head is null?
the newsoldier is assigned to head?
Yes, but will value of tmp change?
ohhh I get it. I feel sorry for myself not being able to fix such problems. Thank you again. :)
Topic archived. No new replies allowed.