Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I'm needing help with this C++ program, it needs to be converted to a template. The code itself is fine and runs as intended.

Hello, I'm needing help with this C++ program, it needs to be converted to a template. The code itself is fine and runs as intended. I would appreciate any help. Thank you for your time.

#include #include using namespace std; const int CAPACITY = 20; class List { private: int aryList[CAPACITY][2]; int arySize; int pos;

public: List(); 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);

friend List operator + (List& ListA, List& ListB); }; ostream& operator <<(ostream& outs, List& aryList); List operator + (List& ListA, List& ListB); bool operator == (List aryList1, List aryList2);

List::List() { this->clear(); }; List::List(List& aryListB) { this->clear(); int orig_pos = aryListB.getPos(); aryListB.first(); for (int i = 0; i < aryListB.size(); aryListB.next(), i++) { this->aryList[i][0] = aryListB.getXElement(); this->aryList[i][1] = aryListB.getYElement(); this->arySize++; } this->pos = orig_pos; aryListB.setPos(orig_pos); }

void List::clear() { arySize = 0; pos = 0; int i; for (i = 0; i < CAPACITY; i++) { aryList[i][0] = 0; aryList[i][1] = 0; } }

void List::first() { pos = 0; }

void List::prev() { if (pos > 0) pos--; }

void List::next() { if (pos < (arySize - 1)) pos++; }

void List::last() { if (arySize > 0) pos = (arySize - 1); else pos = 0; }

void List::replace(int a, int b) { aryList[pos][0] = a; aryList[pos][1] = b; }

void List::setPos(int newPos) { if (newPos >= 0 && newPos < arySize) pos = newPos; }

void List::erase() { if (arySize > 0) { for (int i = pos; i < arySize - 1; i++) { aryList[i][0] = aryList[i + 1][0]; aryList[i][1] = aryList[i + 1][1]; } aryList[arySize - 1][0] = 0; aryList[arySize - 1][1] = 0; arySize -= 1; } }

void List::insertAfter(int a, int b) { if (pos >= CAPACITY - 1) cout << "No insert made" << endl; else { for (int i = arySize; i > pos; i--) { aryList[i][0] = aryList[i - 1][0]; aryList[i][1] = aryList[i - 1][1]; } if (arySize == 0) { aryList[pos][0] = a; aryList[pos][1] = b; } else { aryList[pos + 1][0] = a; aryList[pos + 1][1] = b; pos++; } arySize++; } }

void List::insertBefore(int a, int b) { if (pos == (int)CAPACITY - 1) { aryList[pos][0] = a; aryList[pos][1] = b; } else { for (int i = arySize; i >= pos && i != 0; i--) { aryList[i][0] = aryList[i - 1][0]; aryList[i][1] = aryList[i - 1][1]; } aryList[pos][0] = a; aryList[pos][1] = b; if (arySize < CAPACITY) arySize++; } } int List::size() { return arySize; }

bool List::empty() { return (size() == 0); }

int List::getXElement() { if (size() <= 0) return 0; else return aryList[pos][0]; }

int List::getYElement() { if (size() <= 0) return 0; else return aryList[pos][1]; }

int List::getPos() { return pos; } ostream& operator <<(ostream& outs, List& aryList) { int orig_pos = aryList.getPos(); aryList.first(); outs << "[ "; for (int i = 0; i < aryList.size(); aryList.next(), i++) { outs << '[' << aryList.getXElement() << " " << aryList.getYElement() << "]"; } outs << " ]"; aryList.setPos(orig_pos); return outs; } List operator+(List& ListA, List& ListB) { 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) { if (aryList1.size() == aryList2.size()) { int orig_pos1 = aryList1.getPos(); int orig_pos2 = aryList2.getPos(); aryList1.first(); aryList2.first(); for (int i = 0; i < aryList1.size(); i++, aryList1.next(), aryList2.next()) { if (aryList1.getXElement() != aryList2.getXElement() && aryList1.getYElement() != aryList2.getYElement()) { aryList1.setPos(orig_pos1); aryList2.setPos(orig_pos2); return false; } } aryList1.setPos(orig_pos1); aryList2.setPos(orig_pos2); return true; } return false; } bool operator != (List aryList1, List aryList2) { if (aryList1 == aryList2) return false; else return true; } List List::operator = (List& aryListB) { int orig_pos = aryListB.getPos(); aryListB.first(); first(); arySize = 0; for (int i = 0; i < aryListB.size(); aryListB.next(), this->next(), i++) { this->aryList[i][0] = aryListB.getXElement(); this->aryList[i][1] = aryListB.getYElement(); this->arySize++; } pos = orig_pos; aryListB.setPos(orig_pos); return aryListB; }

int main() { List a,b; int endit;

for (int i=1;i<=10;i++) a.insertAfter(i,i*2); cout << "List a : " << endl; cout << " " << a << endl; cout << "Number of elements in a - " << a.size() << endl;

for (int i=1;i<=10;i++) b.insertBefore(i,i*2); cout << "List b : " << endl; cout << " " << b << endl; cout << "Number of elements in b - " << b.size() << endl;

if ( a == b ) cout << "a == b: List a & b are equal" << endl; else cout << "a == b: List a & b are Not equal" << endl;

a.first(); b.first(); cout << "First elmenet in list a " << a.getXElement() << "," << a.getYElement() << endl; cout << "First elmenet in list b " << b.getXElement() << "," << b.getYElement() << endl; a.last(); b.last(); cout << "Last elmenet in list a " << a.getXElement() << "," << a.getYElement() << endl; cout << "Last elmenet in list b " << b.getXElement() << "," << b.getYElement() << endl; cout << endl << endl << " Start of new stuff" << endl;

b.erase(); cout << "b.erase(): Print Empty List b: " << b << endl;

if ( a != b ) cout << "a != b: List a & b are not equal" << endl; else cout << "a != b: List a & b are equal" << endl;

for (int i=1;i<=10;i++) b.insertBefore(i, -i*2); cout << "List b with neg y's" << endl; cout << "List b: " << b << endl;

a.setPos(5); b.first(); for ( int i=1; i<4; i++) { a.erase(); b.replace(i, i*3); b.next(); }

cout << "Modified Object 'a' (erase 3) " << endl; cout << "List a: " << a << endl; cout << "Modified Object 'b' (replace 3)" << endl; cout << "List b: " << b << endl;

List c(b); cout << "Copy Constructor c(b)" << endl; cout << "List b : " << b << endl; cout << "List c : " << c << endl;

List e; e = c; cout << "Object 'c' assigned to Object 'e':" << endl; cout << "List c : " << c << endl; cout << "List e : " << e << endl;

List d; d=a; d.first(); endit = d.size()/2; for ( int i = 1; i < endit; d.next(), i++) { d.insertBefore(d.getXElement()*2,d.getYElement()*2); d.next(); } cout << "Results after some inserts at front of d " << endl; cout << "List d : " << d << endl;

a.first(); endit = a.size(); for ( int i = 1; i < endit; a.next(), i++) { a.replace(a.getPos()-a.getXElement(),a.getPos()-a.getYElement()); a.next(); } cout << "Results after adding pos & flipping signs on list a" << endl; cout << "List a : " << a << endl;

List alist(b); alist.clear(); for (int i=1;i<=5;i++) alist.insertAfter(i,i*5); alist.first(); cout << "New List alist with positions printed above: " << endl; for (int i=1;i<=5;i++) { cout << setw(8) << alist.getPos(); alist.next(); } cout << endl; alist.first(); for (int i=1;i<=5;i++) { cout << setw(6) << alist.getXElement() << "," << alist.getYElement(); alist.next(); } cout << endl; 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; sq.first(); sq.erase(); sq.erase(); cout << "First elmenet 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions