I've spent the whole day reading the book given to us by the University as well as tutorials all over the internet regarding linked lists. I'm not looking for a solution to my assignment, I'm looking for a better understanding in linked lists and a methodology.
The program we're making allows a person to edit a soldier database, by creating entries, finding and deleting one, printing them, and of course, loading that database on start-up from a file, and saving it back to it on demand. In order to be memory efficient, we need to use dynamic memory allocation, which means we need to keep the memory use down to the most essential as far as the database goes. So when we delete an entry, we must also free up the memory it used.
It's getting late and I'm getting pretty dizzy and anxious. I still have 2 weeks to find it out, but there's lots of studying to do and I wanted to get the assignment out of the way first, since I enjoy programming.
So here are the things I want cleared up, anyone who has some time available and could help, I'd respect it.
1. In order to start pointing .next pointers towards the next entry in the linked list, we need to have a first entry (the "head" as many tutorials call it.) Does that mean that I have to check if it's the first entry during the load from the file or during manual creation of an entry?
2. How will I be able to safely use the same linked list, the same data, in all the separate functions I write? I'm quite confused on this part.
3. What's the quickest way to File I/O regarding linked lists? Fixed size arrays/structures are easy, simple fread and fwrite with size of, but how can I go on reading the data off each node? Is it simpler than I'm thinking it is? Just a simple loop?
For a quick idea, here are the structure declarations. The rest of the code I'm reluctant to post, it's messy, it's incomplete, it's most probably wrong and does nothing right and could crash, only thing is it compiles right.
I'm following tutorials letter by letter, but my head has started spinning and I'm guessing most of the code I've written will go straight to the trash, especially since I began writing some functions based on just the record structure and not a linked list, and now I'm not sure how to get the data from soldiers to soldier without much fuss.Code:/* Initialize record structure for each soldier */ struct record { long int aa; char firstname[20]; char lastname[30]; char datein[11]; char dateout[11]; int freedays; int servicedays; } soldiers[1]; /* Initialize nodes for linked list */ struct node { struct record soldier; struct node* next; };