struct && list
am calling on function insert to load some numbers, but display is giving me only the first number..can someone explain why? thank you
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
struct list{
int value;
list *next= 0;
};
void insertion (int val, list * & head);
void eliminate_all(list * & head );
void display_all ( list * &head);
int main ()
{list * head1 = 0 ;
insertion(1, head1 );
insertion (8, head1);
insertion (9, head1);
display_all( head1 );
return 0;
}
void insertion (int val, list * & head)
{
list * temp = new list;
temp->value = val;
temp->next = 0;
if (head == 0)
{ head = temp;}
else
{
list * curr = head;
while (curr->next != 0)
{
curr = curr->next;
}
curr = temp;
}
}
void eliminate_all(list * & head)
{ list * temp = head;
do { list* temp1;
temp1 = temp->next;
temp->next = temp1->next;
delete temp;
}
while ( temp -> next != 0);
}
void display_all ( list * &head)
{
for (list * temp = head; temp!= 0; temp = temp->next)
{
std::cout<<temp->value<<" ,";
}
}
|
Line 42 curr = temp;
change to curr->next = temp;
thanks
Topic archived. No new replies allowed.