hello every body
i want to know how to save a linked list int a text file and how to load a file in a linked list ??
any one can help me and give me a simple example code
thanks in advance

Too many possible options to say the best way. Here is one way: For writing:
write a header into the file, something like "<singly linked list follows>"
iterate through the linked list writing one item per line
write a footer into the file, something like "<end of singly linked list>"
Then, when reading, look for the header, and append an element for each line until you see the footer.
Of course you still have issues about how to encode/decode the actual data. You could end up reinventing SOAP (or CORBA) if you aren't careful!

griswolf is heading in the right direction. Use an XML structure for your collection, which is basically what a singly linked list is, an ordered collection of similar things/objects. That's very easy in XML form:

<list>
    <element>
        <field1>1</field1>
        <field2>my name</field2>
    </element>
    <element>
        <field1>2</field1>
        <field2>your name</field2>
    </element>
    .
    .
    .
</list>

So, you walk through the list, writing each element and its data members as you go. To reconstitute the list, each time you find the opening <element> tag (or whatever you name it), you allocate a buffer of the appropriate structure type, then for each field in the element, you parse out the data as a string, and convert if necessary, setting the appropriate structure member field. If it is the first element, it becomes the head of the list, and each subsequent element in the list is linked to the previous one. Voila! You have now reconstituted your list from the text (xml) file.

FWIW, I have had to do this numerous times in my work as a software engineer, so perhaps it is second nature to me. In any case, if you do use XML, there are a lot of good C and C++ xml parsing tools such as xerces to use.

This article has been dead for over six months. Start a new discussion instead.