Linked lists

Pages: 12
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.

Good luck with the rest of the assignment.
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.

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
	#include <string>
	#include <iostream>
	#include <fstream>
	#include <cstdlib>
	#include <sstream>
	#include "Assignment4a.h"
	
		void List::insertFromFile(istream &is)
	{
	    Personnel *node = new Personnel;
	    while (node->read(is)) {
		add(node);
		node = new Personnel;
	    }
	    delete node;		// couldn't read it. Delete it now.
	}						 //end insertFromFile

	// 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();
	}
	
	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 here.
	}
	

	
	 //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 
	
    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':  {
		                 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)
	                   }//end case 1
			 break;
			 
			case '2': cout << "Manually add items" << endl;
			 break;
			 
			case '3': cout << "Print ALL items" << endl;
			 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
 	 	
// 		switch (choice)
// 		{
// 			case '1': head = 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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

 using namespace 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
};
Topic archived. No new replies allowed.
Pages: 12