Question: ** C++ only!! ** You are to template your general list class. You will use the main program I supplied earlier for the first assignment.
** C++ only!! **
You are to template your "general list" class. You will use the main program I supplied earlier for the first assignment. You will have to change the main program where 'List' objects are declared to template declarations.
Program:
#include
using namespace std;
const int CAPACITY = 20; typedef int ElementType; typedef size_t size_type;
class List { private: ElementType myAry[CAPACITY][2]; size_type used; int pos;
public:
List() { clear(); pos=0; }
List(List& aryList);
void clear(); void first(); void prev(); void next(); void last(); void replace(int, int); void setPos(int); void erase(); void insertAfter(int, int); void insertBefore(int, int); int size(); bool empty(); int getXElement(); int getYElement(); int getPos();
List operator= (List& aryListB);
};
ostream& operator <<(ostream& outs, List& aryList); List operator + (List& ListA, List& ListB); bool operator == (List aryList1, List aryList2);
bool List::empty() //returns T/F if list is empty or not. { if(size() == 0) { return true; } else { return false; } }
void List::first() //makes current position at the beginning of the list { { pos = 0; } }
void List::last() //makes current position at the end of the list { pos=used-1; }
void List::prev() //places current position at the previous element in the list { if (pos>0) { pos--; } }
void List::next() //places current position at the next element of the list { if (pos < (used - 1)) { pos++; } }
int List::getPos() //returns current position or where you are in the list
{ return pos; }
void List::setPos(int newPos) //places current position in a certain position in the list { if (newPos >= 0 && newPos < used) { pos = newPos; } }
void List::insertBefore(int a, int b) //inserts a new element before the current position { if (pos >= CAPACITY) { cout << "Cannot insert a new element, capacity reached." << endl; }
else { for (int i=pos; i void List::insertAfter(int a, int b) //inserts a new element after the current position { if (pos >= CAPACITY) { cout << "Cannot insert a new element, capacity reached." << endl; } else { for (int i=pos; i int List::getXElement() //returns the one element that current position is pointing to { if (size() <= 0) { return 0; } else { return myAry[pos][0]; } } int List::getYElement() //returns the one element that current position is pointing to { if (size() <= 0) { return 0; } else { return myAry[pos][1]; } } int List::size() //returns the size of the list (number of elements in the list) { return used; } void List::replace(int a, int b) //replace the current element with a new (x, y) value { myAry[pos][0] = a; myAry[pos][1] = b; } void List::erase() //deletes the current element { if (used>0) { myAry[pos][0] = 0; myAry[pos][1] = 0; } } void List::clear() //makes the list an empty list { used = 0; pos = 0; int i; for (i = 0; i < CAPACITY; i++) { myAry[i][0] = 0; myAry[i][1] = 0; } } List::List(List& aryListB) { pos=0; this->clear(); int orig_pos = aryListB.getPos(); aryListB.first(); for (int i = 0; i < aryListB.size(); aryListB.next(), i++) { this->myAry[i][0] = aryListB.getXElement(); this->myAry[i][1] = aryListB.getYElement(); this->used++; } this->pos = orig_pos; aryListB.setPos(orig_pos); } ostream& operator <<(ostream& outs, List& aryList) //to print out in ( , ) { aryList.first(); if(aryList.empty()) { cout<<"---------"; } else { for (int i = 0; i < aryList.size(); aryList.next(), i++) { outs<<"("< List operator+(List& ListA, List& ListB) //adding two lists together { List ListC(ListA); int orig_pos = ListB.getPos(); ListC.first(); ListB.first(); for (int i = 0; i < ListB.size(); ListB.next(), ListC.next(), i++) { ListC.replace(ListB.getXElement() + ListC.getXElement(), ListB.getYElement() + ListC.getYElement()); } ListB.setPos(orig_pos); return ListC; } bool operator == (List aryList1, List aryList2) //comparing two lists if they are equal { for (int i = 0; i < aryList1.size(); i++) { if (aryList1.getXElement() == aryList2.getXElement() && aryList1.getYElement() == aryList2.getYElement()) { return true; } else { return false; } } } bool operator != (List aryList1, List aryList2) //comparing two lists if they are not equal { if (aryList1 == aryList2) { return false; } else { return true; } } List List::operator = (List& aryListB) //assignment operator { int orig_pos = aryListB.getPos(); aryListB.first(); used = 0; for (int i = 0; i < aryListB.size(); aryListB.next(), this->next(), i++) { this->myAry[i][0] = aryListB.getXElement(); this->myAry[i][1] = aryListB.getYElement(); } pos = orig_pos; aryListB.setPos(orig_pos); return aryListB; } //beginning of main int main() { List a, b; int endit; //inserting values in list a for (int i=1;i<=10;i++) { a.insertAfter(i, i * 2); } //printing list a cout << "List a : " << endl; cout << " " << a << endl; cout << "Number of elements in a - " < //inserting values in list b for (int i=1;i<=10;i++) { b.insertBefore(i, i * 2); } //printing list b cout << "List b : " << endl; cout << " " << b << endl; cout << "Number of elements in b - " < //comparing list a and list b if (a == b) { cout << "a == b: List a & b are equal" << endl; } else { cout << "a == b: List a & b are NOT equal" << endl; } //finding the first element in each list a.first(); b.first(); cout<<"First element in list a: (" << a.getXElement() << "," << a.getYElement()<<")" < //finding the last element in each list a.last(); b.last(); cout << "Last element in list a: (" < cout << endl << endl << " Start of new stuff" << endl; //erasing list b b.clear(); cout << "b.erase(): Empty List b: " << b << endl; //comparing list a and b again as not equal if (a != b) { cout << "a != b: List a & b are not equal" << endl; } else { cout << "a != b: List a & b are equal" << endl; } cout << "Modified Object 'a' (erase 3) " << endl; cout << "List a: " << a << endl; cout << "Modified Object 'b' (replace 3)" << endl; cout << "List b: " << b << endl; //copying list b into c List c(b); cout << "Copy Constructor c(b)" << endl; cout << "List b : " << b << endl; cout << "List c : " << c << endl; //comparing c and b if (c == b) cout << "List c & b are equal" << endl; else cout << "List c & b are Not equal" << endl; //list c and e List e = c; cout << "Object 'c' assigned to Object 'e':" << endl; cout << "List c : " << c << endl; cout << "List e : " << e << endl; List d=a; cout << endl << " check out boundary conditions" << endl; List sq; cout << "number of elements in empty sq list = " << sq.size() << endl; cout << " print empty list sq: " << sq << endl; a.first(); sq.erase(); sq.erase(); cout << "First element in list a: (" << a.getXElement() << "," << a.getYElement()<<")"<< endl; sq.setPos(5); cout << "empty sq values " << sq << endl; sq.insertBefore(333, 444); cout << "sq list: " << sq << endl; sq.next(); sq.next(); cout << "sq.getElement() = " << sq.getXElement() << "," << sq.getYElement() << endl; cout << "sq list = " << sq << endl; sq.prev(); sq.prev(); cout << "sq.getElement()= " << sq.getXElement() << "," << sq.getYElement() << endl; cout<< "sq list = "<< sq << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
