Question

Write an append function void append(const arrayListType<elemType>& otherList) that will take an array list as a...

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 }

Homework Answers

Answer #1

#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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) //...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) // The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5. //parameter a_bag to be subtracted from this (the calling) bag //post removes all data from items_ that is also found in a_bag //Since type is...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
Write a program that will read the information from a file into a list and then...
Write a program that will read the information from a file into a list and then display the list to the screen. Remove the fifth item in the list and display the list again. Ask the program user for an entry into the list and add it to the list. Display the list one last time. disneyin.txt file daisy   123 donald   345 goofy   654 mickey   593 minnie   489 daffy   432 pluto   765 huey   321 dewey   987 lewey   554 porky   333...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below)...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below) and a write a driver code to test all of your implementations. // Define a structure to use as the list item struct ListItem { int key; int Data; }; #define MAX_SIZE 50 // Define maximum length of the list class UnsortedArray { private: int head; // Index to head of the list ListItem theList[MAX_SIZE]; // The list public: UnsortedArray(); // Class constructor ~...
array_v.h #ifndef ARRAY_V_H #define ARRAY_V_H #include <cassert> template < typename IndexType, typename BaseData > class Array_V...
array_v.h #ifndef ARRAY_V_H #define ARRAY_V_H #include <cassert> template < typename IndexType, typename BaseData > class Array_V { public: IndexType partition(IndexType lo, IndexType hi); IndexType sort(int numvals); void qsRecursive(IndexType lo, IndexType hi); IndexType getHiIndex(); IndexType getLoIndex(); void setHiIndex(IndexType index); void setLoIndex(IndexType index); Array_V(IndexType lo, IndexType hi); //constructor Array_V(int size = 0); Array_V(const Array_V< IndexType, BaseData >& initArray); //copy constructor ~Array_V(); //destructor BaseData& operator [] (IndexType); Array_V< IndexType, BaseData >& operator = (const Array_V< IndexType, BaseData >& initArray); void assign(IndexType i, const...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor and inserts it as the nth element of the list; test your implementation by turning the flag LAB3_TEST2 from 0 to 1 in config.h; - Programming Exercise 3: implement the ListArray member function find(...) that searches for the element given as a parameter; the search starts at the cursor and stops when it finds the element or at the end of the list; the...
I am having problem for the splitStrange function, and here is the feedback I get from...
I am having problem for the splitStrange function, and here is the feedback I get from the professor, but I am not sure how to do it.... Inside the loop, if the odd element is the first in the list(list_head) then you just call remove(); But if it's just another element in the list you need to delete it differently Like with using temp and pointers and delete //Strange.h template <typename ElementType> bool strange(const ElementType &element) { return true; }...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT