Write an append function
void append(const arrayListType<elemType>& otherList)
that will take an array list as a parameter and append it to the current list.
Write an operator+ function that will create a new list containing the contents of the two lists
added.
arrayListType<elemType> operator+(const arrayListType<elemType> & rhs) const
so that you can write code like a = b + c where a b and c are all arrayListTypes.
Add your functions to the template and write a main program to test your changes. Your main program can create an array of integers so you do not have to create any more elaborate classes.
Note that the template code already contains a copy constructor and an operator= function.
1 #include<iostream>
2 #include<string>
3
4 using namespace std;
5
6 template
7 class arrayListType
8 {
9
10 public:
11 const arrayListType& operator=(const
arrayListType&);
12 bool isEmpty() const;
13 bool isFull() const;
14 int listSize() const;
15 int maxListSize() const;
16 void print() const;
17 bool isItemAtEqual(int location, const elemType& item)
const;
18 void insertAt(int location, const elemType&
insertItem);
19 void insertEnd(const elemType& insertItem);
20 void removeAt(int location);
21 void retrieveAt(int location, elemType& retItem)
const;
22 void replaceAt(int location, const elemType& repItem);
23 void clearList();
24 int seqSearch(const elemType& item) const;
25 void insert(const elemType& insertItem);
26 void remove(const elemType& removeItem);
27
28 void removeAll(const elemType& removeItem); //problem
2
29
30 elemType min(); //problem 3
31
32 elemType max(); //problem 4
33
34 arrayListType(int size = 100);
35
36 //copy constructor
37 arrayListType(const arrayListType& otherList);
38
39 //destructor
40 ~arrayListType();
41
42 protected:
43
44 elemType *list;
45 int length;
46 int maxSize;
47 };
48
49 template
50 bool arrayListType::isEmpty() const{
51 return (length==0);
52 }
53
54 template
55 bool arrayListType::isFull() const{
56 return (length==maxSize);
57 }
58
59 template
60 int arrayListType::listSize() const{
61 return length;
62 }
63
64 template
65 int arrayListType::maxListSize() const{
66 return maxSize;
67 }
68 template
69 void arrayListType::print() const{
70 for (int i = 0; i 71 cout< 72 }
73 cout< 74 }
75
76 template
77 bool arrayListType::isItemAtEqual(int location, const
elemType& item) const{
78 return (list[location]==item);
79 }
80
81 template
82 void arrayListType::insertAt(int location, const elemType&
insertItem){
83 if((location<0)||(location>=maxSize)){
84 cerr<<"The position of the item to be inserted is out of
range."< 85 }
86 else{
87 if(length>=maxSize)//list is full
88 {
89 cerr<<"Cannot insert in a full list"< 90 }
91 else{
92 for (int i = length; i>location; i--){
93 list[i] = list[i-1]; //move elements down
94 }
95 list[location] = insertItem;
96 length++;
97 }
98 }
99 }
100
101 template
102 void arrayListType::insertEnd(const elemType&
insertItem){
103 if (length>=maxSize) //list is full
104 {
105 cerr<<"Cannot insert in a full list."< 106 }
107 else{
108 list[length] = insertItem;
109 length++;
110 }
111 }
112
113 //problem 1; assumption - list unsorted
114 template
115 void arrayListType::removeAt(int location){
116 if((location<0)||(location>=length)){
117 cerr<<"The location of the item to be removed is out of
range."< 118 }
119 else{
120 list[location] = list[length-1];
121 length--;
122 }
123 }
124
125 template
126 void arrayListType::retrieveAt(int location, elemType&
retItem) const{
127 if((location<0)||(location>=length)){
128 cerr<<"The location of the item to be retrieved is out of
range."< 129 }
130
131 else{
132 retItem = list[location];
133 }
134
135 }
136
137 template
138 void arrayListType::replaceAt(int location, const elemType&
repItem){
139 if((location<0)||(location>=length)){
140 cerr<<"The location of the item to be replaced is out of
range."< 141 }
142 else{
143 list[location] = repItem;
144 }
145 }
146
147 template
148 void arrayListType::clearList(){
149 length = 0;
150 }
151
152 template
153 arrayListType::arrayListType(int size){
154 if (size<0){
155 cerr<<"The array size must be positive; creating an array
of size 100."< 156 maxSize = 100;
157 }
158 else{
159 maxSize = size;
160 }
161 length = 0;
162 list = new elemType[maxSize];
163 //assert(list!=NULL);
164 }
165
166 template
167 arrayListType::~arrayListType(){
168 delete [] list;
169 }
170
171 template //copy constructor
172 arrayListType::arrayListType(const arrayListType&
otherList){
173 maxSize = otherList.maxSize;
174 length = otherList.length;
175 list = new elemType[maxSize];
176 assert(list!=NULL); //terminate if unable to allocate memory
space
177 for(int j = 0; j 178 list[j] = otherList.list[j];
179 }
180 }
181
182 //overloading assignment operator
183 template
184 const arrayListType& arrayListType::operator=(const
arrayListType& otherList){
185 if (this != &otherList){ //avoid self-assignment
186 delete [] list;
187 maxSize = otherList.maxSize;
188 length = otherList.length;
189
190 list = new elemType[maxSize]; //create the array
191 assert (list != NULL); //if unable to allocate memory space,
terminate program
192 for(int i = 0; i 193 list[i] = otherList.list[i];
194 }
195 }
196 return *this;
197 }
198
199 //search:
200
201 template
202 int arrayListType::seqSearch(const elemType& item)
const{
203 int loc;
204 bool found = false;
205 for (loc = 0; loc 206 if (list[loc]==item){
207 found = true;
208 break;
209 }
210 }
211 if (found)
212 return loc;
213 else
214 return -1;
215 }
216
217 template
218 void arrayListType::insert(const elemType&
insertItem){
219 int loc;
220
221 if(length == 0){//list is empty
222 list[length++] = insertItem; //insert the item and
increment
223 }
224 else if(length == maxSize){
225 cerr<<"Cannot insert in a full list."< 226 }
227 else{
228 loc = seqSearch(insertItem);
229 if(loc == -1){
230 //item isn't already in the list
231 list[length++] = insertItem;
232 }
233 else{
234 cerr<<"The item to be inserted is already in the list. No
duplicates allowed."< 235 }
236 }
237 }
238
239 template
240 void arrayListType::remove(const elemType&
removeItem){
241 int loc;
242
243 if (length == 0){
244 cerr<<"Cannot delete from an empty list."< 245 }
246 else{
247 loc = seqSearch(removeItem);
248 if(loc!=-1){
249 removeAt(loc);
250 }
251 else{
252 cout<<"the item to be deleted is not in the list."<
253 }
254 }
255 }
256 template
257 void arrayListType::removeAll(const elemType& removeItem){
//problem 2
258 int loc;
259 if(length == 0){
260 cerr<<"Cannot delete from an empty list."< 261 }
262 else{
263 loc = seqSearch(removeItem);
264 while(loc!=-1){
265 removeAt(loc);
266 loc = seqSearch(removeItem);
267 }
268 }
269
270 }
271 template
272 elemType arrayListType::min(){ //problem 3
273 elemType soln;
274 if(length==0){
275 cerr<<"No minimum possible for an empty list."< 276
}
277 else{
278 soln = list[0]; //set smallest to be first item...
279 for(int i = 1; i 280 if (soln>list[i]){//if you can find a
smaller item, replace...
281 soln = list[i];
282 }
283 }
284 }
285 return soln;
286 }
287 template //similar to looking for min
288 elemType arrayListType::max(){ //problem 4
289 elemType soln; //stores max value
290 if(length==0){ //cannot do anything with an empty list
291 cerr<<"No maximum possible for an empty list."< 292
}
293 else{
294 soln = list[0]; //initialize soln to the first item of the
list
295 for(int i = 1; i 296 if(soln 297 soln = list[i];//replace soln
with value of what we're currently looking at
298 }
299 }
300 }
301 return soln;
302 }
303
304 int main(){
305
306 /*This is the example from the book; it just shows you the
basics
307 that we can declare an arrayListType out of primitive-ish
types,
308 and in spite of the fact we're dealing with different types,
the behavior
309 of this class is the same.
310 */
311
312
313 arrayListType intList(100);
314 arrayListType stringList;
315
316 int number;
317 string str;
318
319 cout<<"Enter ten integers: ";
320 for (int i = 0; i<10; i++){
321 cin>>number;
322 intList.insertAt(i, number);
323 }
324
325 cout< 326 intList.print();
327 cout< 328
329 cout<<"Enter the item to be deleted: ";
330 cin>>number;
331 intList.remove(number);
332 cout<<"After removing "< 333 intList.print();
//problem 1
334 cout< 335
336 cout<<"Enter item to be deleted: ";
337 cin>>number;
338 intList.removeAll(number); //problem 2
339 cout<<"Updated list: "< 340 intList.print();
341
342 cout<<"Smallest item in list: "< 343
cout<<"Largest item in list: "< 344
345
346 return 0;
347 }
#include<iostream>
#include<string>
using namespace std;
template <class elemType>
class arrayListType
{
public:
const arrayListType& operator=(const arrayListType&);
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int location, const elemType& item)
const;
void insertAt(int location, const elemType& insertItem);
void insertEnd(const elemType& insertItem);
void removeAt(int location);
void retrieveAt(int location, elemType& retItem) const;
void replaceAt(int location, const elemType& repItem);
void clearList();
int seqSearch(const elemType& item) const;
void remove(const elemType& removeItem);
void removeAll(const elemType& removeItem); //problem 2
elemType min(); //problem 3
elemType max(); //problem 4
//copy constructor
arrayListType(const arrayListType& otherList);
//destructor
~arrayListType(){}
void append(const arrayListType<elemType>&
otherList);
arrayListType<elemType> operator+(const
arrayListType<elemType> & rhs) const;
void insert(const elemType& insertItem){
list[length++]=insertItem;
}
arrayListType(int size = 100){
///initialize arrayList with size
list=new elemType[size];
///set lenght as 0
length=0;
///set maxSize as size
maxSize=size;
}
protected:
elemType *list;
int length;
int maxSize;
};
template <class elemType>
void arrayListType<elemType>::print() const{
for (int i = 0; i <length;i++){
cout<<list[i]<<" ";
}
}
template <class elemType>
int arrayListType<elemType>::listSize() const{
return length;
}
template <class elemType>
bool arrayListType<elemType>::isEmpty() const{
return (length==0);
}
template <class elemType>
void arrayListType<elemType>::retrieveAt(int location,
elemType& retItem) const{
if((location<0)||(location>=length)){
cerr<<"The location of the item to be retrieved is out of
range.";
}
else{
retItem = list[location];
}
}
template <class elemType>
void arrayListType<elemType>::append(const
arrayListType<elemType>& otherList){
///now copy list
for(int i=0;i<otherList.listSize() ;i++){
otherList.retrieveAt(i,list[length++]);
///copy elements at list [lenght] from position 'i'of
otherlist
}
}
template <class elemType>
arrayListType<elemType>
arrayListType<elemType>::operator+(const
arrayListType<elemType> & rhs) const{
///we need to create new list a which will store the addition result of calling obj and rhs
arrayListType<elemType> newList;
///now insert all list items from calling object's list
into newList
for(int i=0;i<length ;i++){
newList.insert(list[i]);
}
newList.append(rhs);
return newList; ///return result
}
int main(){
arrayListType <int> intList;
// arrayListType <string> stringList;
int number;
string str;
/// adding 5 elements to intlist
intList.insert(10);
intList.insert(20);
intList.insert(30);
intList.insert(40);
intList.insert(50);
cout<<"\nFirst =";
intList.print();
///create another intlist
arrayListType <int> intList2;
/// adding 4 elements to intlist2
intList2.insert(100);
intList2.insert(200);
intList2.insert(300);
intList2.insert(4000);
cout<<"\nsecond =";
intList2.print();
cout<<"\nAfter first_second =first+second ";
///using + operator
arrayListType <int> first_second=intList+intList2;
cout<<"\nfirst_second=";
first_second.print();
cout<<"\n\nAfter second_first =second+first ";
///using + operator
arrayListType <int> second_first=intList2+intList;
cout<<"\nsecond_first=";
second_first.print();
///create string list of 3 elemets
arrayListType <string> stringList1;
stringList1.insert("A1");
stringList1.insert("A2");
stringList1.insert("A3");
///create string list of 6 elemets
arrayListType <string> stringList2;
stringList2.insert("B1");
stringList2.insert("B2");
stringList2.insert("B3");
stringList2.insert("B4");
stringList2.insert("B5");
stringList2.insert("B6");
cout<<"\n\n********\n\nString + operator ";
cout<<"\nStr1 =\n";
stringList1.print();
cout<<"\nStr2 =\n";
stringList2.print();
arrayListType <string> strAns=stringList1+stringList2;
cout<<"\nAfter str1+str2\n";
strAns.print();
cout<<"\n\n********\n\nAPPEND*************\n\n";
cout<<"\nStr1 =\n";
stringList1.print();
cout<<"\nStr2 =\n";
stringList2.print();
cout<<"\nafter str2.append(str1) => str2 becomes ->
";
stringList2.append(stringList1);
stringList2.print();
cout<<"\nint1 =\n";
intList.print();
cout<<"\nint2 =\n";
intList2.print();
cout<<"\nafter int1.append(int2) => int1 becomes ->
";
intList.append(intList2);
intList.print();
return 0;
}
TEST RESULT
Get Answers For Free
Most questions answered within 1 hours.