// // Created by Arias Arevalo, Carlos on 5/6/20. // #include "object.h" #include "list.h" #include "vsarray.h" #include using std::string; VSArray::VSArray(size_t capacity, double...

1 answer below »


  • Implementing ADT List using Variable Size Arrays

  • Implementing ADT List using Double Linked Lists









// // Created by Arias Arevalo, Carlos on 5/6/20. // #include "object.h" #include "list.h" #include "vsarray.h" #include using std::string; VSArray::VSArray(size_t capacity, double increasePercentage){ _data = new Object*[_capacity]; for (size_t i = 0; i < _capacity;="" i++){="" _data[i]="nullptr;" }="" }="" vsarray::vsarray(const="" vsarray&="" list){="" _size="list._size;" _data="new" object*[_capacity];="" for="" (size_t="" i="0;" i="">< _size;="" i++){="" _data[i]="list.Get(i)-">Clone(); } for (size_t i = _size; i < _capacity;="" i++){="" _data[i]="nullptr;" }="" }="" vsarray::~vsarray(){="" for(size_t="" i="0;" i="">< _size;="" i++){="" delete="" _data[i];="" }="" }="" const="" vsarray&="" vsarray::operator="(const" vsarray&="" rhs){="" if="" (&rhs="=" this){="" return="" *this;="" }="" if="" (_capacity="" !="rhs._capacity){" clear();="" delete[]="" _data;="" _capacity="rhs._capacity;" _data="new" object*[_capacity];="" for="" (size_t="" i="0;" i="">< _capacity;="" i++){="" _data[i]="nullptr;" }="" }="" for(size_t="" i="0;" i="">< rhs.size();="" i++){="" _data[i]="rhs.Get(i)-">Clone(); } return *this; } bool VSArray::Insert(Object* element, size_t position){ if(position > _size) return false; if(_capacity == _size) return false; if(position < _size){="" for(size_t="" i="_size;" i=""> position; i--){ _data[i] = _data[i-1]; } } _data[position] = element; _size++; return true; } int VSArray::IndexOf(const Object* element)const{ for(size_t i = 0; i < _size;="" i++){="" if(_data[i]-="">Equals(*element)){ return i; } } return -1; } Object* VSArray::Remove(size_t position){ if(position >= _size){ return nullptr; } Object* retVal = _data[position]; for (size_t i = position; i < _size="" -="" 1;="" i++){="" _data[i]="_data[i+1];" }="" _data[_size-1]="nullptr;" _size--;="" return="" retval;="" }="" object*="" vsarray::get(size_t="" position)const{="" if="" (position="">= _size) return nullptr; else return _data[position]; } string VSArray::ToString()const{// no work needed return ""; } void VSArray::Clear(){ for (size_t i = 0; i < _size;="" i++){="" delete="" _data[i];="" _data[i]="nullptr;" }="" _size="_capacity;" }="" void="" vsarray::resize()="" {="" _size="0;" }="" size_t="" vsarray::getcapacity()="" const="" {="" return="" _capacity;="" }="" #include="" "person.h"="" #include="" "integer.h"="" #include="" "list.h"="" #include="" "dlinkedlist.h"="" #include="" "vsarray.h"="" #include=""> #include #include #include #include using std::cout; using std::endl; using std::string; using std::stringstream; using std::setw; using std::fixed; using std::setprecision; void Test(List*, const string&, bool=true); int main(int argc, char* argv[]){ cout < "="==================================="">< endl;="" cout="">< endl="">< endl;="" cout="">< "there="" are="" 2="" sets="" of="" unit="" tests:"="">< endl;="" cout="">< "="" 1.="" double="" linked="" list="" -="" 55"="">< endl;="" cout="">< "="" 2.="" variable="" size="" array="" list="" -="" 55"="">< endl;="" cout="">< "="==================================="">< endl;="" cout="">< "="==================================="">< endl;="" cout="">< endl="">< endl;="" cout="">< "double="" linked="" list="" tests"="">< endl;="" test(new="" doublelinkedlist(),="" "double="" linked="" list",="" false);="" cout="">< "variable="" size="" array="" list="" tests"="">< endl;="" test(new="" vsarray(5),="" "variable="" size="" array="" list",="" false);="" cout="">< "-="" -="" -="" -="" -="" -="" -="" -="" -="" -="" -="" -="" -="" -="" -="" -="" -="" -="" -="" "="">< endl;="" cout="">< endl;="" cout="">< "="" c="" o="" n="" g="" r="" a="" t="" u="" l="" a="" t="" i="" o="" n="" s"="">< endl;="" cout="">< "="" all="" test="" passed!"="">< endl="">< endl;="" return="" 0;="" }="" void="" test(list*="" list,="" const="" string&="" message,="" bool="" fixedsize){="" cout="">< "testing:="" "="">< message="">< endl="">< endl;="" size_t="" passed="0;" const="" double="" total="55.0;" assert(list-="">Size() == 0); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">IsEmpty() == true); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" person*="" person="nullptr;" integer*="" integer="nullptr;" object*="" object="nullptr;" person*="" george="new" person("george",="" 12);="" integer*="" ten="new" integer(10);="" object*="" dummy="new" object();="" person="new" person("george",="" 12);="" assert(list-="">Insert(person, 2) == false); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">IsEmpty() == true); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">Insert(person, 0) == true); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">Get(0)->ToString() == "Person: {name: George, age: 12}"); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">Size() == 1); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">IndexOf(GEORGE) == 0); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" integer="new" integer(10);="" assert(list-="">Insert(integer, 0) == true); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">Get(0)->ToString() == "10"); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">Get(1)->ToString() == "Person: {name: George, age: 12}"); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">Size() == 2); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">IndexOf(TEN) == 0); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">IndexOf(GEORGE) == 1); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">IndexOf(DUMMY) == -1); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" person="new" person("patrick",="" 1);="" assert(list-="">Insert(person, 1) == true); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">Size() == 3); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" person="new" person("natalia",="" 19);="" assert(list-="">Insert(person, 2) == true); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">Size() == 4); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" integer="new" integer(4);="" assert(list-="">Insert(integer, 4) == true); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">Size() == 5); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" if="" (fixedsize){="" for="" (size_t="" i="0;" i="">< list-="">Size(); i++) { assert(list->Insert(integer, i) == false); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" }="" assert(="" (object="list-">Remove(0)) != nullptr ); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">Size() == 4); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(object-="">ToString() == "10"); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" delete="" object;="" object="nullptr;" ten-="">SetValue(4); assert(list->IndexOf(TEN) == 3); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">IndexOf(GEORGE) == 2); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" ten-="">SetValue(10); assert(list->IndexOf(TEN) == -1); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" }else{="" assert(list-="">Insert(new Person("Violet", 1), list->Size()) == true); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">Size() == 6); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(="" (object="list-">Remove(5)) != nullptr ); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(object-="">ToString() == "Person: {name: Violet, age: 1}"); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" fixing="" the="" leak="" delete="" object;="" object="nullptr;" assert(list-="">Size() == 5); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" final="" version="" tests="" for="" all="" code="" stringstream="" inputdata,="" inserteddata;="" inputdata.str(="" name="" age="" position="" |="" number="" position="" "winston="" 55="" 0\n"="" "75="" 0\n"="" "peter="" 43="" 3\n"="" "32="" 3\n"="" "legolas="" 156="" 9\n"="" "99="" 1\n"="" "patricia="" 21="" 0\n"="" "130="" 0\n"="" "anna="" 23="" 1\n"="" "1304="" 14"="" );="" string="" name;="" size_t="" age,="" position,="" number;="" for="" (size_t="" i="0;" i="">< 5;="" i++)="" {="" inputdata="">> name >> age >> position; assert(list->Insert(new Person(name, age), position) == true); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" inputdata="">> number >> position; assert(list->Insert(new Integer(number), position) == true); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" }="" assert(list-="">Size() == 15); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" inserteddata.str(="" name="" age="" position="" |="" number="" position="" "winston="" 55="" 5\n"="" "75="" 3\n"="" "peter="" 43="" 8\n"="" "32="" 7\n"="" "legolas="" 156="" 13\n"="" "99="" 4\n"="" "patricia="" 21="" 2\n"="" "130="" 0\n"="" "anna="" 23="" 1\n"="" "1304="" 14"="" );="" for="" (size_t="" i="0;" i="">< 5;="" i++)="" {="" inserteddata="">> name >> age >> position; object = new Person(name, age); assert(list->IndexOf(object) == static_cast(position)); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" inserteddata="">> number >> position; delete object; object = nullptr; object = new Integer(number); assert(list->IndexOf(object) == static_cast(position)); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" delete="" object;="" object="nullptr;" }="" assert(="" (object="list-">Remove(4)) != nullptr); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(list-="">Size() == 14); cout < setw(10)="">< fixed="">< setprecision(2)="">< ++passed="" total="" *="" 100.0="">< endl;="" assert(object-="">ToString() == "99"); cout < setw(10)>
Answered 2 days AfterMay 10, 2021

Answer To: // // Created by Arias Arevalo, Carlos on 5/6/20. // #include "object.h" #include "list.h" #include...

Rushendra answered on May 13 2021
149 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here