Advertisement

Loading linked list from file

Started by July 04, 2000 09:30 PM
2 comments, last by dj9781 24 years, 5 months ago
I''m able to load the list fine but the only problem is that it loads it in backwards. Is there any way to fix this without having to do a full blown out sort using a singly linked list? Also do I have to write a function that deletes the entire list when the program is done using it or does it do this for me?
Just add each element to the opposite end of the list. In other words, if you add items to the tail as you load them, change that so that they''re inserted at the head.
Advertisement
Are you writing the list backwards or something?

And anything you allocate, you must deallocate.
As a guess, i imagine you are just holding a pointer to the head of your list, then adding new nodes to the head of the list.

Instead keep two pointers, one for the head and one for the tail. Your first node in the list will be pointed to by the head and the tail, then always add other nodes to the tail. When you have finished, the head will point to the list in the correct order.

This topic is closed to new replies.

Advertisement