简体   繁体   中英

Printing All A Linked Lists' Elements (C++)

I am trying to use this function to print off of a linked lists' elements, but when I do it infinitely repeats the first item in the list. Any help is appriciated

}
cout << "Your list is: " << endl;

Node * start = head;
while (start)
{
    cout << start->data<<endl;
    start = head->next;
}

return menu(); 
}
while (start)
{
    cout << start->data<<endl;
    start = start->next;
}

Will fix it.

Your loop is re-accessing the first list item on every iteration. You are not progressing to subsequent items at all.

You need to change this

start = head->next;

to this

start = start->next;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM