As I am trying to digest this solution, I am curious as to why there are 2 structs?
One named "Personnel" that holds the data for each node and then another that is named "List". Why wouldn't just the struct "personnel" work for everything?
Also, I initially considered making the read from file a separate function but since it is only used once, I figured it would work just as well in the same function as the open file function. But now after looking at it from a different perspective, it does look cleaner to have it in the switch function. It doesn't seem to get lost in all of the code.
Anyway, thanks for your advice and I hope that you don't tire of my seemingly endless questions. I am a "why" kind of guy who obsesses over why and how things work. I really appreciate the fact that you took time to explain your reasoning and I can follow your style of teaching. A lot of times, in tutorials and example code on various sites, there really is no accompanying explanation.
Think of a linked list as a collection of objects, all of the same type, just like an array is a collection of objects. The difference is that an array has a fixed size and a linked list can grow at run time to whatever size you want, limited only by the size of memory.
I am curious as to why there are 2 structs?
There are several reasons:
- If you used one struct, then how would you represent an empty list? With a separate "root" pointer? But then you'd have to pass that pointer to most of the methods.
- What if you tried to apply a "list" function like delete to a node that was half-way through the list?
I initially considered making the read from file a separate function but since it is only used once, I figured it would work just as well in the same function as the open file function.
Certainly it would, but like I said, if you think of the design process as "how do I make doing the work easy?" then you'll end up writing code that is more flexible and better organized.
well, ive been working on this for a few days, trying to digest this and assemble it. For some reason @ line 141, I am getting an out of scope error for the list.insertFromList function. I have tried moving the function up to the top of the code, I have tried calling this function a billion different ways (not really sure why its being called with a "." operator) and I am just missing something. I am sure that it is something small but my assignment is due Wednesday and I am starting to stress.
usingnamespace std;
struct Personnel
{
Personnel() : next(NULL) {} // always initialize next to NULL
string firstName;
string lastName;
string jobTitle;
char empStatus;
double wage;
Personnel *next;
bool read(istream &is); // read from an open file, or any stream
void print(ostream &os); // print to open file or any stream
bool fromUser(); // prompt and read from user on cin
};
struct List {
List() : root(NULL) {}
// Delete all items in the list
~List();
// Add a node to the front of the list. node MUST be allocated
// via new. The List will be responsible for deleting it.
void add(Personnel *node);
void insertFromFile(istream &is);
// Print the list to the stream
void print(ostream &os);
private:
Personnel *root; // head of the list
};