-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 13.2 (Completed)
341 lines (317 loc) · 9.27 KB
/
Exercise 13.2 (Completed)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
1. (Practice) Enter and run Program 13.1.
#include "stdafx.h"
#include <iostream>
#include <list>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
list<string> names, addnames;
string n;
// add names to the original list
names.push_front("Dolan, Edith");
names.push_back("Lanfrank, John");
// create a new list
addnames.push_front("Acme, Sam");
addnames.push_front("Mening, Stephen");
addnames.push_front("Zemann, Frank");
names.sort();
addnames.sort();
// merge the second list into the first
names.merge(addnames);
cout << "The final list size is : " << names.size() << endl;
cout << "This list contains the names : \n";
while (!names.empty())
{
cout << names.front() << endl;
names.pop_front(); // remove the object
}
system("PAUSE");
return 0;
}
//Done!!
2. (Practice) Enter and run Program 13.2.
//Done!!
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
using namespace std;
// class declaration section
class NameTele
{
private:
string name;
string phoneNum;
// class implementation section
public:
NameTele(string nn, string phone) // constructor
{
name = nn;
phoneNum = phone;
}
// inline method definitions
string getName(){ return name; }
string getPhone(){ return phoneNum; }
};
int main()
{
list<NameTele> employee; // instantiate and initialize the list
// using objects in the array
employee.push_front(NameTele("Acme, Sam", "(555) 898–2392"));
employee.push_back(NameTele("Dolan, Edith", "(555) 682–3104"));
employee.push_back(NameTele("Lanfrank, John", "(555) 718–4581"));
employee.push_back(NameTele("Mening, Stephen", "(555) 382–7070"));
employee.push_back(NameTele("Zemann, Harold", "(555) 219–9912"));
// retrieve all list objects
// use accessor methods to extract the name and pay rate
cout << "The size of the list is " << employee.size() << endl;
cout << "\n Name Telephone";
cout << "\n-------------- --------------\n";
while (!employee.empty())
{
cout << employee.front().getName()
<< "\t " << employee.front().getPhone() << endl;
employee.pop_front(); // remove the object
}
system("PAUSE");
return 0;
}
3. (Modify) Modify Program 13.2 to prompt the user for a name. Have the program search the
existing list for the entered name. If the name is in the list, display the corresponding phone
number; otherwise, display this message: The name is not in the current phone list.
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
using namespace std;
// class declaration section
class NameTele
{
private:
string name;
string phoneNum;
// class implementation section
public:
NameTele(string nn, string phone) // constructor
{
name = nn;
phoneNum = phone;
}
// inline method definitions
string getName(){ return name; }
string getPhone(){ return phoneNum; }
};
int main()
{
string name;
list<NameTele> employee; // instantiate and initialize the list
// using objects in the array
employee.push_front(NameTele("Acme,Sam", "(555) 898–2392"));
employee.push_back(NameTele("Dolan,Edith", "(555) 682–3104"));
employee.push_back(NameTele("Lanfrank,John", "(555) 718–4581"));
employee.push_back(NameTele("Mening,Stephen", "(555) 382–7070"));
employee.push_back(NameTele("Zemann,Harold", "(555) 219–9912"));
// retrieve all list objects
// use accessor methods to extract the name and pay rate
cout << "The size of the list is " << employee.size() << endl;
//cout << "\n Name Telephone";
//cout << "\n-------------- --------------\n";
/*while (!employee.empty())
{
cout << employee.front().getName()
<< "\t " << employee.front().getPhone() << endl;
employee.pop_front(); // remove the object
}*/
cin >> name;
while (!employee.empty()){
if (employee.front().getName() == name){
cout << employee.front().getPhone() << endl;
}
else if (employee.front().getName() != name){
cout << "This name is not in the current phone list" << endl;
}
employee.pop_front();
}
system("PAUSE");
return 0;
}
4. (Practice) Write a C++ program containing a linked list of 10 integer numbers. Have the program
display the numbers in the list.
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
using namespace std;
// class declaration section
class NameTele
{
private:
int num;
// class implementation section
public:
NameTele(int nn) // constructor
{
num = nn;
}
// inline method definitions
int getNum(){ return num; }
};
int main()
{
list<NameTele> numbers; // instantiate and initialize the list
// using objects in the array
numbers.push_front(NameTele(201));
numbers.push_back(NameTele(120));
numbers.push_back(NameTele(4581));
numbers.push_back(NameTele(7070));
numbers.push_back(NameTele(9912));
numbers.push_front(NameTele(12343));
numbers.push_back(NameTele(123));
numbers.push_back(NameTele(4521));
numbers.push_back(NameTele(770));
numbers.push_back(NameTele(992));
// retrieve all list objects
// use accessor methods to extract the name and pay rate
cout << "The size of the list is " << numbers.size() << endl;
cout << "\n Numbers ";
cout << "\n------------------ \n";
while (!numbers.empty())
{
cout << numbers.front().getNum() << endl;
numbers.pop_front(); // remove the object
}
system("PAUSE");
return 0;
}
5. (Practice) Using the linked list of objects shown in Figure 13.3, write the sequence of steps
for deleting the object for John Lanfrank from the list.
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
using namespace std;
// class declaration section
class NameTele
{
private:
string name;
string phoneNum;
// class implementation section
public:
NameTele(string nn, string phone) // constructor
{
name = nn;
phoneNum = phone;
}
// inline method definitions
string getName(){ return name; }
string getPhone(){ return phoneNum; }
};
int main()
{
string name;
list<NameTele> employee; // instantiate and initialize the list
// using objects in the array
employee.push_front(NameTele("Acme, Sam", "(555) 898–2392"));
employee.push_back(NameTele("Dolan, Edith", "(555) 682–3104"));
employee.push_back(NameTele("Lanfrank, John", "(555) 718–4581"));
employee.push_back(NameTele("Mening, Stephen", "(555) 382–7070"));
employee.push_back(NameTele("Zemann, Harold", "(555) 219–9912"));
// retrieve all list objects
// use accessor methods to extract the name and pay rate
cout << "The size of the list is " << employee.size() << endl;
cout << "\n Name Telephone";
cout << "\n-------------- --------------\n";
name = "Lanfrank, John";
while (!employee.empty())
{
if (employee.front().getName() == name){
employee.pop_front();
}
cout << employee.front().getName()
<< "\t " << employee.front().getPhone() << endl;
employee.pop_front(); // remove the object
}
system("PAUSE");
return 0;
}
6. (Practice) Generalize the description in Exercise 5 to describe the sequence of steps for
removing the nth object from a list of linked objects. The nth object is preceded by the (n - 1)st
object and followed by the (n + 1)st object. Make sure to store all pointer values correctly.
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
using namespace std;
// class declaration section
class NameTele
{
private:
string name;
string phoneNum;
// class implementation section
public:
NameTele(string nn, string phone) // constructor
{
name = nn;
phoneNum = phone;
}
// inline method definitions
string getName(){ return name; }
string getPhone(){ return phoneNum; }
};
int main()
{
int n = 3;
list<NameTele> employee; // instantiate and initialize the list
// using objects in the array
employee.push_front(NameTele("Acme, Sam", "(555) 898–2392"));
employee.push_back(NameTele("Dolan, Edith", "(555) 682–3104"));
employee.push_back(NameTele("Lanfrank, John", "(555) 718–4581"));
employee.push_back(NameTele("Mening, Stephen", "(555) 382–7070"));
employee.push_back(NameTele("Zemann, Harold", "(555) 219–9912"));
// retrieve all list objects
// use accessor methods to extract the name and pay rate
cout << "The size of the list is " << employee.size() << endl;
cout << "\n Name Telephone";
cout << "\n-------------- --------------\n";
while (!employee.empty())
{
if (employee.size() == n){
employee.pop_front();
}
cout << employee.front().getName()
<< "\t " << employee.front().getPhone() << endl;
employee.pop_front(); // remove the object
}
system("PAUSE");
return 0;
}
7. (Desk check) Determine the output of this program:
#include <iostream>
#include <list>
using namespace std;
int main()
{
int intValue;
double sum = 0.0;
double average;
int nums[] = {1, 2, 3, 4, 5 }; // create array of integer values
list<int> x(nums, nums + 4); // instantiate a list of ints using
// a constructor that initializes
// the list with values from array
cout <<”\nThe list x initially has a size of “ << x.size()
<< “,\n and contains the objects: “;
while (!x.empty())
{
cout << x.front() << “ “;
x.pop_front();
}
cout << endl;
}
------------------------------------
The list x initially has a size of 4,
and contains the objects : 1 2 3 4
-------------------------------------