Linked lists

Pages: 12
I have to create a program that demonstrates the understanding of linked lists, sorting and exception handling. I have to read a file and assign that data to a linked list. I understand what a node is but there are a few questions that I can not seem to find the answers on.

1.) A node can only hold on set of data? i.e. first Name, Last Name, Job Title, etc would each have their own node?

2. I would need to provide a separate linked list for each person to hold their data?

Part of the assignment reads:
"Create a structure to use as a linked list node that contains the the following:"
First Name
Last Name
Job Title
Employment Status(character)
Hourly Wage
"next" pointer

Based on that statement, it would seem to me that I will need to read all of the data into the structure and then read it into a linked list?
1.) A node can only hold on set of data? i.e. first Name, Last Name, Job Title, etc would each have their own node?
No, you need a struct to hold complex data. You cannot have different data types within a list.

2. I would need to provide a separate linked list for each person to hold their data?
No, see above. Each person is represented by a node.

Based on that statement, it would seem to me that I will need to read all of the data into the structure and then read it into a linked list?
Yes.
Thanks for the reply...So basically, I would be creating a struct for each different person (and his associated info from my text file) and storing the address of the struct in the linked list? Seems like a weird way to store info.
So basically, I would be creating a struct for each different person
No, you create one struct:
1
2
3
4
5
6
7
8
9
10
struct Person
{
  std::string FirstName;
  std::string LastName
  std::string JobTitle;
  char EmploymentStatus;
  int HourlyWage;

 Person *next; // "next" pointer
};
With this struct you create as many object as you want:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
...
  Person *p1 = new Person();
  Person *p2 = new Person();
  p1->HourlyWage = 10;
  p1->next = p2;
  p2->HourlyWage = 9;
  p2->next = nullptr;
...
  return 0;
}
ohhhhh......thanks!!
So I need to put that in a loop and since I do not know how many lines of data the text file will contain, I need to make the "1" a variable. something like this:

1
2
counter = 1//this will actually be the result of how many lines are in the text file
Person *pcounter = new Person();


update:
well, its obvious that my idea above wont work.....How do I make the
Person *p1 = new Person()
expandable based on the number of lines in the text file?
Last edited on
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Person *cur_person = head;
while(stream)
{
  Person *p = new Person();
// Read data from stream to p
if(stream)
{
  if(cur_person)
    cur_person->next = p;
  else
  {
     head = p; // Note: You probably make Person *head a global variable in order to access it from other functions (?)
     cur_person = head;
  }
  ++counter; // Note:Create it near by head
}
else
  delete p;
}
Thanks, 1 question though.....why is a pointer used to cur_person & p outside the if statement and not inside the if?

really appreciate your help...
cur_person needs to be outside the loop. It is a kind of iterator and needs the initialization before the loop starts.

p needs to be before the if because within the if the data is added to the list. Before you can do so you need to check whether the data is successfully read (e.g. no eof). So the if on line 6 ensures that only valid data is added.

You may read the data like so:
stream >> p->FirstName >> p->LastName >> ...
I am really having a hard time with some errors that I don't understand why it happening. I get the following errors stating incomplete type and forward declaration.......????

I have watched so many tutorial and searched the internet so many time, that my head is spinning.

Please help me figure out what I'm doing wrong...

Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
	#include <string>
	#include <iostream>
	#include <fstream>
	#include <cstdlib>
	#include <sstream>
	#include "Assignment4.h"
	
	using namespace std;
	
	//****function declarations
 //GetFileName
 string getFileName()
 {
 string fileName;	
 cout << "Enter Name of file to open INCLUDING extension ::" << endl;
 cin >> fileName;
 //================test to make sure the fileName name has a txt extension
 string ext="";
for(int i = fileName.length()-1;i>fileName.length()-5;i--)
{
    ext += fileName[i];
}
cout<<ext;
if(ext != "txt.")
    cout<<"File name does not contain the proper extension, Please Enter Name of file to open INCLUDING extension :: ";
    cin >> fileName;
 //====================================================================== 
 return fileName;
 }//end getFileName
	//insert data into structure
	//void insert (Personnel *&head, Personnel *&last, string firstName, string lastName, string jobTitle, char empStatus, double wage){
	void insert (int numberOfLines){
    struct node* head;
    struct node* cur_person;
	string firstName, lastName, jobTitle, fileName;
	char empStatus;
	double wage;
	int counter = 0;
	ifstream file;
	fileName = getFileName();
	file.open(fileName.c_str());
	//file.open("Personnel.txt");
	while (counter != numberOfLines){

//====================================	
	 //Personnel *cur_person = head;
	//while(file)
	//{
	  Personnel *p = new Personnel();
	// Read data from stream to p
	if(file.is_open())
	{
	  if(cur_person)
	    cur_person->next = p;
	  else
	  {
	     head = p; // Note: You probably make Person *head a global variable in order to access it from other functions (?)
	     cur_person = head;
	  }//end else
	  ++counter; // Note:Create it near by head
	}//end if
	else
	  delete p;
	}//end while

	
	}//end insert
 void getUserInfo(){
 	string firstName, lastName, jobTitle;
 	char empStatus;
 	double wage;
 	cout << "Enter FIRST name of associate :" << endl;
 	cin >> firstName;
 	cout << "Enter LAST name of associate :" << endl;
 	cin >> lastName;
 	cout << "Enter job title of associate :" << endl;
 	cin >> jobTitle;
 	
 	    while (empStatus != 'P' && empStatus != 'p' && empStatus != 'F' && empStatus != 'f')
 	    {
 	    	cout << "Is associate <F>ull OR <P>art time ?" << endl;
 	cin >> empStatus;
		 }//end while
    cout << "Enter number of hours per week associate works :" << endl;
    cin >> wage;
 }
	

	
	//get the number of rows in a text file
	int LengthOfFile (string file){
		ifstream inFile;
		int numberOfLines = 0;
		string line;
		inFile.open(file.c_str());
		while (!inFile.eof()){
			getline (inFile, line);
			numberOfLines ++;
		}//end while
		cout << numberOfLines;
	 inFile.close();
	 return numberOfLines;
	  insert(numberOfLines);	
	}//end NumberOfLines
	
	int main() {
	//-----------variables
	struct node* cur_person;
	struct node* head;
	//cur_person = head;
	string file;
	file = getFileName();
	//-----------open text file
	 //open text file and determine the number of lines contained in it
	  LengthOfFile(file);
	  getUserInfo();

		return 0;
	}//end main 


Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using namespace std;
 
 struct Personnel {

 string firstName;
 string lastName;
 string jobTitle;
 char empStatus;
 double wage;
 //cur_person head
 Personnel *next;
 Personnel *head;
 Personnel *cur_person;
 	
 };// end struct
 
 //void insert (Personnel *&head, Personnel *&last, string firstName, string lastName, string jobTitle, char empStatus, double wage);
 int LengthOfFile (string file);
 void insert (int numberOfLines);
 string fileOpen();
This
1
2
 Personnel *head;
 Personnel *cur_person;
does not belong to Personnel, just remove this.

cur_person is a local variable in insert(...). It is not used anywhere else. So remove it from all other places.

If you replace what I named stream with what you named file then you do not need the LengthOfFile(...) function because it stops reading at the end of file.

Line 103 does nothing since it appears after the return. Just remove it.

I get the following errors stating incomplete type and forward declaration.......????
Post the exact errors.
these are the errors I am getting...

1
2
3
54	16		[Error] invalid use of incomplete type 'struct insert(int)::node'
57	12		[Error] cannot convert 'Personnel*' to 'insert(int)::node*' 
33	12		[Error] forward declaration of 'struct insert(int)::node'
line 33/34: You do not have a struct node.
this is my line 33/34:

1
2
 struct node* head;
 struct node* cur_person;
this is my line 33/34:
Exactly. Where is node defined?
this is part of my problem. I look through my text book, cant find an example. look online, cant find an example. I do not know where it should of how it should be declared.
I finally got it. Its a little frustrating for me as I am very fluent in the programming language for AutoCAD.

Please help me by looking at this to make sure that I have it structured right. I still have a lot of work to do but I need to make sure that I am headed in the right direction.

Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	#include <string>
	#include <iostream>
	#include <fstream>
	#include <cstdlib>
	#include <sstream>
	#include "Assignment4.h"
	
	using namespace std;
	
	//****function declarations
 //GetFileName
 string getFileName()
 {
 string fileName;	
 cout << "Enter Name of file to open INCLUDING extension ::" << endl;
 cin >> fileName;
 //================test to make sure the fileName name has a txt extension
 string ext="";
for(int i = fileName.length()-1;i>fileName.length()-5;i--)
{
    ext += fileName[i];
}
cout<<ext;
if(ext != "txt.")
    cout<<"File name does not contain the proper extension, Please Enter Name of file to open INCLUDING extension :: ";
    cin >> fileName;
 //====================================================================== 
 return fileName;
 }//end getFileName
 
	//insert data into structure
	//void insert (Personnel *&head, Personnel *&last, string firstName, string lastName, string jobTitle, char empStatus, double wage){
	void insert (int numberOfLines){
    struct Personnel *head;
	Personnel *cur_person = head;
	string firstName, lastName, jobTitle, fileName;
	char empStatus;
	double wage;
	int counter = 0;
	ifstream file;
	fileName = getFileName();
	file.open(fileName.c_str());
	//file.open("Personnel.txt");
//	while (counter != numberOfLines){
//
////====================================	
//	 Personnel *cur_person = head;
	while(file)
		{
		  Personnel *p = new Personnel();
		// Read data from stream to p
		if(file.is_open())
			{
			  if(cur_person)
		      {
			    cur_person->next = p;
		      }// end if
			   else
			   {
			     head = p; // Note: You probably make Person *head a global variable in order to access it from other functions (?)
			     cur_person = head;
			   }//end else
			  ++counter; // Note:Create it near by head
			}//end if
			else
			  delete p;
		    }//end if
	}//end insert
 void getUserInfo(){
 	string firstName, lastName, jobTitle;
 	char empStatus;
 	double wage;
 	cout << "Enter FIRST name of associate :" << endl;
 	cin >> firstName;
 	cout << "Enter LAST name of associate :" << endl;
 	cin >> lastName;
 	cout << "Enter job title of associate :" << endl;
 	cin >> jobTitle;
 	
 	    while (empStatus != 'P' && empStatus != 'p' && empStatus != 'F' && empStatus != 'f')
 	    {
 	    	cout << "Is associate <F>ull OR <P>art time ?" << endl;
 	cin >> empStatus;
		 }//end while
    cout << "Enter number of hours per week associate works :" << endl;
    cin >> wage;
 }
	

	
	//get the number of rows in a text file
	int LengthOfFile (string file){
		ifstream inFile;
		int numberOfLines = 0;
		string line;
		inFile.open(file.c_str());
		while (!inFile.eof()){
			getline (inFile, line);
			numberOfLines ++;
		}//end while
		cout << numberOfLines;
	 inFile.close();
	 return numberOfLines;
	  
	}//end NumberOfLines
	
	int main() {
	//-----------variables
	//struct node* cur_person;
//	struct Personnel *head;
//	Personnel *cur_person = head;
	//cur_person = head;
	string file;
	file = getFileName();
	//-----------open text file
	 //open text file and determine the number of lines contained in it
	  //LengthOfFile(file);
	  getUserInfo();

		return 0;
	}//end main 


Header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 using namespace std;
 
 struct Personnel {

 string firstName;
 string lastName;
 string jobTitle;
 char empStatus;
 double wage;
 //cur_person head
 Personnel *next;
 Personnel *head;
 Personnel *cur_person;
 	
 };// end struct
 
 //void insert (Personnel *&head, Personnel *&last, string firstName, string lastName, string jobTitle, char empStatus, double wage);
 int LengthOfFile (string file);
 void insert (int numberOfLines);
 string fileOpen();
Can someone please tell me why the "insertFromFile()" function isn't working properly? I found a very good tutorial from Warren Rachele on youtube about linked lists and I tried to modified it to work with bringing in text from a txt file but for the life of me, I cant see where it goes off the rails. As a side note, the same code works for manually entering the info but not for text from a file.

main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
	#include <string>
	#include <iostream>
	#include <fstream>
	#include <cstdlib>
	#include <sstream>
	#include "Assignment4.h"
	
	using namespace std;
	
 //******************************************
 void print_list(Personnel *in_root)
 //=======================================
 // PRINT LIST CONTENTS
 //=======================================
 {
 	Personnel *next_ptr;
 	
 	next_ptr = in_root;
 	
 	if(next_ptr == NULL)
 	{
 		cout << "EMPTY LIST: No nodes to print...." << endl;
	}//end if
	else
	{
		while(next_ptr != NULL)
		{
			cout << next_ptr -> firstName << endl;
			cout << next_ptr -> lastName << endl;
			cout << next_ptr -> jobTitle << endl;
			cout << next_ptr -> empStatus << endl;
			cout << next_ptr -> wage << endl;
			next_ptr = next_ptr -> next;
		}//end while
	}//end else
	system("pause");
 }//end print_list
 
 //=====================Add node by manual input.
 Personnel *add_node(Personnel *in_root)
 //=========================================
 // ADD A NEW NODE
 //=========================================
 {
 	Personnel *next_ptr = NULL;
 	Personnel *prev_ptr = NULL;
 	
 	if(in_root == NULL)
 	{
 		// list is empty
 		if((in_root = new Personnel) != NULL)
 		{
 			next_ptr = in_root;
 			
 			cout << "First Name: ";
 			cin >> next_ptr -> firstName;
 			cout << "Last Name: ";
 			cin >> next_ptr -> lastName;
 			cout << "Job Title: ";
 			cin >> next_ptr -> jobTitle;
 			cout << "Employments Status <F>ull or <P>art time: ";
 			cin >> next_ptr -> empStatus;
 			cout << "Wage: ";
 			cin >> next_ptr -> wage;
 			 			
 			next_ptr -> next = NULL;
 		}//end if
 		else
 		{
 			cout << "ERROR: Unable to allocate memory." << endl;
 			return NULL;
		}//end else
 		return next_ptr;
	}//end if
 	else
 	{
 		//list has members
 		next_ptr = in_root;
 		while(next_ptr -> next != NULL)
 		{
 			next_ptr = next_ptr -> next;
 	    }//end while
 	    prev_ptr = next_ptr;
 	    if((next_ptr = new Personnel) != NULL)
 	    {
 	    	prev_ptr -> next = next_ptr;
 			cout << "First Name: ";
 			cin >> next_ptr -> firstName;
 			cout << "Last Name: ";
 			cin >> next_ptr -> lastName;
 			cout << "Job Title: ";
 			cin >> next_ptr -> jobTitle;
 			cout << "Employments Status <F>ull or <P>art time: ";
 			cin >> next_ptr -> empStatus;
 			cout << "Wage: ";
 			cin >> next_ptr -> wage;
 			
 			next_ptr -> next = NULL;
		}//end if
		else
		{
			cout << "ERROR: Unable to allocate memory." << endl;
		}//end else
		return in_root;
 	}//end else
 };//end Personnel
 
 //**********************
 //GetFileName
 string getFileName()
 {
 string fileName;	
 cout << "Enter Name of file to open INCLUDING extension ::" << endl;
 cin >> fileName;
 //================test to make sure the fileName name has a txt extension
 string ext="";
for(int i = fileName.length()-1;i>fileName.length()-5;i--)
{
    ext += fileName[i];
}
//cout<<ext;
if(ext != "txt.")
 {
    cout<<"File name does not contain the proper extension, Please Enter Name of file to open INCLUDING extension :: " <<endl;
    cin >> fileName;
}//end if
 return fileName;
 }//end getFileName
 
 //=================add node from text file
 string insertFromFile (Personnel *in_root)
 {
	Personnel *next_ptr = NULL;
	Personnel *prev_ptr = NULL;
	string firstName, lastName, jobTitle, fileName;
	char empStatus;
	double wage;
	//int counter = 0;
	ifstream file;
	fileName = getFileName();
	file.open(fileName.c_str());
	
	if(!file.is_open())
    {
       exit(EXIT_FAILURE);
       cout << "File did not open." << endl;
    }// end if

	file >> firstName >> lastName >> jobTitle >> empStatus >> wage;
	//cout << firstName << lastName << jobTitle << empStatus << wage << endl;
	
////////====================================
 while(file)
     {
    	
 	
 	if(in_root == NULL)
 	{
 		// list is empty
 		if((in_root = new Personnel) != NULL)
 		{
 			next_ptr = in_root;
 			next_ptr -> firstName;
			next_ptr -> lastName;
			next_ptr -> jobTitle;
			next_ptr -> empStatus;
			next_ptr -> wage;

 			next_ptr -> next = NULL;
 		}//end if
 		else
 		{
 			cout << "ERROR: Unable to allocate memory." << endl;
 			return NULL;
		}//end else
 		return next_ptr;
	}//end if
 	else
 	{
 		//list has members
 		next_ptr = in_root;
 		while(next_ptr -> next != NULL)
 		{
 			next_ptr = next_ptr -> next;
 	    }//end while
 	    prev_ptr = next_ptr;
 	    if((next_ptr = new Personnel) != NULL)
 	    {
 	    	prev_ptr -> next = next_ptr;
            next_ptr -> firstName;
			next_ptr -> lastName;
			next_ptr -> jobTitle;
			next_ptr -> empStatus;
			next_ptr -> wage;
 			
 			next_ptr -> next = NULL;
		}//end if
		else
		{
			cout << "ERROR: Unable to allocate memory." << endl;
		}//end else
		return in_root;
 	}//end else
 }//end while
 
	}//end insertFromFile
 //=====================getUserInfo=====
 void getUserInfo(){
 	string *firstName, *lastName, *jobTitle;
 	char *empStatus;
 	double *wage;
 	cout << "Enter FIRST name of associate :" << endl;
 	cin >> *firstName;
 	cout << "Enter LAST name of associate :" << endl;
 	cin >> *lastName;
 	cout << "Enter job title of associate :" << endl;
 	cin >> *jobTitle;
 	
 	    while (*empStatus != 'P' && *empStatus != 'p' && *empStatus != 'F' && *empStatus != 'f')
 	    {
 	    	cout << "Is associate <F>ull OR <P>art time ?" << endl;
 	cin >> *empStatus;
		 }//end while
    cout << "Enter hourly wage :" << endl;
    cin >> *wage;
 }//end getUserInput
 //============menu======	
 void menu()
 {
 	Personnel *head = NULL;
	char choice;
 	bool quit = false;
	while (!quit)
      {
 	cout << "*****MENU*****" << endl;
 	cout << "1. Add items from Text File" << endl;
 	cout << "2. Manually Add Item" << endl;
 	cout << "3. Print ALL associates" << endl;
 	cout << "4. Search and Print" << endl;
 	cout << "5. EXIT" << endl;
 	 cout << "Enter a menu choice: " << endl;
        cin >> choice;
        cout << endl;
 	 	
 		switch (choice)
 		{
 			case '1': insertFromFile(head);
			 break;
			 
			case '2': head = add_node(head);
			 break;
			 
			case '3': print_list(head);
			 break;
			 
			case '4': cout << "Function to search for and print an associate to screen" << endl;
			 break;
			 
			case '5': cout << "EXIT" << endl; 
			 quit = true;
			 break;  
		}//end switch
	}//end while
 }//end menu
	
 int main()
 { 
 //-----------variables
	menu();
	system("pause");
	return 0;
 }//end main 


Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 using namespace std;
 
 struct Personnel {

 string firstName;
 string lastName;
 string jobTitle;
 char empStatus;
 double wage;
 //cur_person head
 Personnel *next;
// Personnel *head;
// Personnel *cur_person;
 	
 };// end struct
 
 Personnel *add_node (Personnel *in_root);
 void print_list (Personnel *in_root);
 string fileOpen();
 string insertFromFile (Personnel *in_root);
 void menu();


Lines 163-167 don't do anything. Did you mean to assign something to those members?
Line 176 causes it to return after inserting the first person.
Lines 184-186: it looks like you're inserting at the end of the list. Was this way you intended? It's much faster to insert at the beginning of a linked list.
Lines 190-194: same problem as lines 163-167.
In addition, insertFromFile returns a string, but you're returning a pointer to struct Personnel.

I think you need to reorganize the code. There should be just one function that inserts into a list. When you want to insert from a file, you read the file, and then call the function to insert into the list. When you want to read from cin, you read the person there and then call the function to insert.

Think of it this way: the purpose of your functions and methods is NOT to "do all the work." Their purpose is to "make doing the work easy." If you approach coding this way you'll create nice reusable code.

For example, take class Personnel. You need to be able to read it from a file, or prompt for the data from the user, and you need to be able to print it out. So create those methods:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
};

...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// read from an open file, or any stream
bool Personnel::read(istream &is)
{
    is >> firstName >> lastName >> jobTitle >> empStatus >> wage;
    return is.good();
}


// print to open file or any stream
void Personnel::print(ostream &os)
{
    os << firstName << endl;
    os << lastName << endl;
    os << jobTitle << endl;
    os << empStatus << endl;
    os << wage << endl;
}


// prompt and read from user on cin
bool Personnel::fromUser()
{
    cout << "Enter person's data:" << endl;
    cout << "First Name: ";
    cin >> firstName;
    cout << "Last Name: ";
    cin >> lastName;
    cout << "Job Title: ";
    cin >> jobTitle;
    cout << "Employments Status <F>ull or <P>art time: ";
    cin >> empStatus;
    cout << "Wage: ";
    cin >> wage;
    return cin.good();
}


Now let's think about the list itself. You can add a node to the list and you can print it out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);

    // Print the list to the stream
    void print(ostream &os);
private:
    Personnel *root;		// head of the list
};

...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void
List::print(ostream &os)
 //=======================================
 // PRINT LIST CONTENTS
 //=======================================
{
    Personnel *next_ptr;

    next_ptr = root;

    if (next_ptr == NULL) {
	os << "EMPTY LIST: No nodes to print...." << endl;
    }						 //end if
    else {
	while (next_ptr != NULL) {
	    next_ptr->print(os);  // use the print() method that we defined above
	    next_ptr = next_ptr->next;
	}					 //end while
    }						 //end else
}						 //end print_list

 //=====================Add node by manual input.
void
List::add(Personnel * node)
 //=========================================
 // ADD A NEW NODE
 //=========================================
{
    node->next = root;
    root = node;
};						 //end Personnel

List::~List()
{
    // ADD CODE to delete all items in the list.
}

As indicated, you will need to add the code to delete the items from the list when it is destroyed.

Notice how short the add() method has become. When you add to the front of the list, it's only 2 statements.

Now let's look at inserting from a file. Remember, you want to "make doing the work easy." So let's make this a method of the list. And just like with print, let's pass in the stream:
struct List {
...
void insertFromFile(istream &is);
...
};[/code]

And that code becomes:
1
2
3
4
5
6
7
8
9
10
void
List::insertFromFile(istream &is)
{
    Personnel *node = new Personnel;
    while (node->read(is)) {
	add(node);
	node = new Personnel;
    }
    delete node;		// You created one but couldn't read it. Delete it now.
}						 //end insertFromFile 


Notice how I use the read() method from personnel to read the node from the stream. Note that the read() method returns true or false depending on whether it succesfully read the record, and I'm using that return value in the while() loop. I'm also using the add() method to add the node to the list. The one tricky part here is that when read() finally fails, you have to delete the last node that you created.

But you still need to get the file name and open it. Let's move that to the code inside menu:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void
menu()
{
    List list;
...
	case '1':
	    {
		string fileName = getFileName();
		ifstream file;
		file.open(fileName.c_str());
		if (!file.is_open()) {
		    cout << "File did not open." << endl;
		    exit(EXIT_FAILURE);
		}						 // end if
		list.insertFromFile(file);
	    }
	    break;


Now all you have to do is change the code for add_node(). It just needs to create a new Personnel object, call fromUser() on it to prompt the user and read the data, and then insert it in the list.
very nice....that's what I am ultimately trying to do. I just didn't realize that it could be simplified so much. Also, I guess the thing that really confuses me with linked lists is that each node is named the same, which in my case is....Personnel. That just seems odd to me.
Pages: 12