link list

i am working on this program for a while and i am having trouble using multiple pieces of information in the linked list. i need to be able to input multiple data (ie name and hours) into the linked list then have it print out. i cant figure out how to get more than just the name stored and printed.

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
#include <string>
#include <iostream>

using namespace std;

struct nodeType
{
     string info;
     nodeType *link;
};       
      
class person
{
      public:
             person();
             person(string person_name);
             string get_person_name();
             void print();
             
             string name;
};

class hStaff: public person
{
      int hours;
      
      public:
             hStaff();
             hStaff(string hStaff_name, int hStaff_hours);
             int get_hStaff_hours();
};

class sStaff: public person
{
      string office_num;
      
      public:
             sStaff();
             sStaff(string sStaff_name, string sStaff_office_num);
             string get_office_num();
};

class super: public sStaff
{
      char ext;
      
      public:
             super();
             super(string super_name, string super_office_num, char super_ext);
             char get_super_ext();
};

//#endif

person::person()
{
     name = "none";
}

person::person(string person_name)
{
     name = person_name;
}

string person::get_person_name()
{
	return name;
}

void person::print()
{
     nodeType *current;
     nodeType *first;
     
     current = first;
     
     while (current != NULL) 
     {
        cout << current->info << endl;
        current = current->link;
     }
}

hStaff::hStaff()
{
     hours = 0;
}

hStaff::hStaff(string hStaff_name, int hStaff_hours)
                                :person(hStaff_name)
{
     name = hStaff_name;
     hours = hStaff_hours;
}

int hStaff::get_hStaff_hours()
{
	return hours;
}  

sStaff::sStaff()
{
     office_num = "unknown";
}

sStaff::sStaff(string sStaff_name, string sStaff_office_num)
                      :person(sStaff_name)
{
	office_num = sStaff_office_num;
}

string sStaff::get_office_num()
{
	return office_num;
}

super::super()
{
	ext = 'a';
}

super::super(string super_name, string super_office_num, char super_ext)
                              :sStaff(super_name, super_office_num)
{
	ext = super_ext;
}

char super::get_super_ext()
{
	return ext;
}

#include <iostream>
#include <string>

using namespace std;

int menu();

int main()
{
    string hStaff_name;
    string hStaff_hours;
    nodeType *first, *last, *newNode, *current;
    string name;
    
    person person_type;
    hStaff hStaff_type;
    
    newNode = new nodeType;
    newNode->info = "";
    newNode->link = NULL;

    first = newNode;
    
	for (;;)
	{
		switch (menu())
		{
		case 0:
			return 0;
		case 1:
                        last = NULL;
             
                        newNode = new nodeType;
                        newNode->info = hStaff_name;
                        newNode->link = first;
                        first = newNode;
             
                        if (last == NULL)
                        {
                             last = newNode;
                        }
 			break;
		case 2:

			break;
		case 3:
             
             break;
        case 4:
             hStaff_type.print();
             break;
	default:
		cout << "error" << endl;
		}
	}
	return 0;
}

int menu()
{
	int choice;

	for (;;)
	{
		cout << endl << "0 Exit" 
             << endl << "1 add hourly staff" 
             << endl << "2 add salary staff" 
             << endl << "3 add supervisor" 
             << endl << "4 view list of staff"
             << endl;

		cin >> choice;

		cin.clear();
		cin.ignore(250, '\n');

		if ((choice < 0) || (choice > 5))
		{
			cout << "try again" << endl;
			continue;
		}
		return choice;
	}
}
No, no, no. That's not how you implement a linked list. This is what the structures should look like: http://en.wikipedia.org/wiki/Linked_list#Linearly-linked_lists
You're manually modifying the structure instead of having class methods do all the work.

person::get_person_name(): PROTIP: don't use getters and setters for public members. It's like adding a door to a house without walls.
person::print(): 'first' is uninitialized. 'current' is undeclared. You then proceed to assign 'first' to 'current'.
main(): What do you use 'last' for if you change its value on every cycle? if (last==NULL) will always be true because of the above. last=newNode makes 'last' and 'first' equivalent, thus rendering 'last' useless.
int menu(): if ((choice < 0) || (choice > 5)): If you're going to check for invalid values, do it properly and make sure that choice<5. Don't have the function return only to let the switch jump to the default case and print "error".
Last edited on
im incredibly confused
Please, do expand.
i dont exactly know what you meant
...
By which part?
i think this is what you were hinting at but its not printing correctly

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
#include <string>
#include <iostream>

using namespace std;

struct nodeType
{
     string name;
     int hours;
     nodeType *link;
};   
      
class person
{
      public:
             person();
             person(string person_name);
             string get_person_name();
             
             string name;
};

class hStaff: public person
{
      int hours;
      
      public:
             hStaff();
             hStaff(string hStaff_name, int hStaff_hours);
             void set_hStaff(string, int);
             int get_hStaff_hours();
             void add_hStaff(string, int);
             void print();
};

class sStaff: public person
{
      string office_num;
      
      public:
             sStaff();
             sStaff(string sStaff_name, string sStaff_office_num);
             string get_office_num();
             void print();
};

class super: public sStaff
{
      char ext;
      
      public:
             super();
             super(string super_name, string super_office_num, char super_ext);
             char get_super_ext();
};

//#endif

person::person()
{
     name = "none";
}

person::person(string person_name)
{
     name = person_name;
}

string person::get_person_name()
{
	return name;
}

hStaff::hStaff()
{
     hours = 0;
}

hStaff::hStaff(string hStaff_name, int hStaff_hours)
                                :person(hStaff_name)
{
     
}

void hStaff::set_hStaff(string hStaff_name, int hStaff_hours)
{
     name = hStaff_name;
     hours = hStaff_hours;
}


int hStaff::get_hStaff_hours()
{
	return hours;
}

void hStaff::add_hStaff()
{
     nodeType *first, *last, *newNode;
     //string hStaff_name;
     //int hStaff_hours;
     
     last = NULL;
             
     newNode = new nodeType;
     newNode->name = hStaff_name;
     newNode->hours = hStaff_hours;
     newNode->link = first;
     first = newNode;  
     

}

void hStaff::print()
{
     nodeType *current;
     nodeType *first;
     
     current = first;
     
     while (current != NULL) 
     {
        cout << "Hourly staff name: " << current->name << endl 
             << "     hours worked: " << current->hours << endl << endl;
        current = current->link;
     }
}

sStaff::sStaff()
{
     office_num = "unknown";
}

sStaff::sStaff(string sStaff_name, string sStaff_office_num)
                      :person(sStaff_name)
{
	office_num = sStaff_office_num;
}

string sStaff::get_office_num()
{
	return office_num;
}

super::super()
{
	ext = 'a';
}

super::super(string super_name, string super_office_num, char super_ext)
                              :sStaff(super_name, super_office_num)
{
	ext = super_ext;
}

char super::get_super_ext()
{
	return ext;
}

#include <iostream>
#include <string>

using namespace std;

int menu();

int main()
{
    string hStaff_name;
    int hStaff_hours;
    string sStaff_office_num;
    string sStaff_name;
    nodeType *first, *last, *newNode, *current;
    
    string name;
    
    person person_type;
    hStaff hStaff_type;
    sStaff sStaff_type;
    
    newNode = new nodeType;
    newNode->name = "";
    newNode->hours = 0;
    newNode->link = NULL;

    first = NULL;
    
	for (;;)
	{
		switch (menu())
		{
		case 0:
			return 0;
		case 1:
	         cout << "Enter name: ";
			 getline(cin, hStaff_name);
			 
             cout << "Enter hours: ";
			 cin >> hStaff_hours;
			 
			 hStaff_type.set_hStaff(hStaff_name, hStaff_hours);
			 
			 hStaff_type.add_hStaff();
             

             
 			 break;
		case 2:


			 break;
		case 3:
             break;
        case 4:
             hStaff_type.print();
             break;
		default:
			cout << "error" << endl;
		}
	}
	return 0;
}

int menu()
{
	int choice;

	for (;;)
	{
		cout << endl << "0 Exit" 
             << endl << "1 add hourly staff" 
             << endl << "2 add salary staff" 
             << endl << "3 add supervisor" 
             << endl << "4 view list of staff"
             << endl;

		cin >> choice;

		cin.clear();
		cin.ignore(250, '\n');

		if ((choice < 0) || (choice > 5))
		{
			cout << "try again" << endl;
			continue;
		}
		return choice;
	}
}
You mean to tell me that that compiles?
But half of the variables are undeclared.
And no, that's absolutely not what I meant by letting the class methods do all the work. You need to read a couple of chapters on data structures before you can understand why your design (if you can call it that) is so wrong.

Let me enumerate some of the things you're doing wrong:
1. You don't have a list class for your node class. Without it, what you have there is not a linked list per se.
2. Your node class isn't generic. This can be excused, I suppose.
3. You moved your structure modifying code from one wrong place to another. Not just any class should be able to modify the structure of the list. The list and ONLY the list should be able to modify its own structure, following the instructions from the calling functions.
4. The following is unrelated to data structures, and more on the subject of basic C++ programming: YOU'RE USING UNDECLARED VARIABLES.
5. And now, structured programming: some of your functions don't receive any parameters, yet their bodies suggest that some parameters are needed. Either you copy-pasted the code from a different function, in which case this is a case of code rot, or you're expecting to use global variables, which would be a case of good ol' unstructured programming.
Last edited on
you must feel pretty good about yourself belittling me and my program, telling me everything i did wrong and not helping me figure out how to make it right. im glad that you have earned yourself an well-deserved sense of self satisfaction by logging onto C++ forums and bashing people who have put aside their pride long enough to ask for your help. i congratulate you on time well spent, i hope your computer chair serves as a good companion. thank you
Not really.
Knowing what is wrong with something is, no less, the second step towards fixing it (the first step being knowing that there is something wrong at all).
I wasn't trying to "belittle" neither you nor your program. I was pointing out its flaws so you could correct them yourself. I also saw that you had a rudimentary knowledge of what a linked list was, so I gave you a link to information on the subject.

But now my patience has been met with impatience and that's something I really don't like.
Last edited on
Don't worry be happy!!, you have written most of the code but need some more effort to make it as a linked list.
First modify the base class as follows
1
2
3
4
5
6
7
8
9
10
class person
{
      public:
             person();
             person(string person_name);
             string get_person_name();
            person* pNext; // @@ added to hold the next person
      private: //@@ added the access specifier       
             string name;
};


Now I will explain how this works

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Let us say we want to create three persons named X,Y and Z

person* a= new person("X");
person* b= new person("Y");
person* c= new person("Z");
// we can create a linked list by connecting them using the pointer person* pNext

a->pNext=b;
b->pNext=c;
c->pNext=NULL;
// if you want to iterate through
person* temp= a;
while( temp){
cout<< temp->get_person_name().c_str();
temp=temp->pNext;
}


If you understood it then modfy AddPerson() etc..
Topic archived. No new replies allowed.