View the step-by-step solution to:

Question

//main.cpp

#include <iostream>

#include "DynList.h"

using namespace std;

int

main()

{

//Define the object of class

dynListType *l1=new dynListType(5);

dynListType *l2=new dynListType(5);


//Insert elements into firt list

l1->insert(5);

l1->insert(15);

l1->insert(10);

l1->insert(20);

cout<<"l1 contains "<< l1->remove(2)<<endl;

cout<<"After removing a value, l1 contains "<< l1->insert(0,0)<<endl;

cout<<"After insertion of 0, l1 contains "<<l1->remove(2)<<endl;

//Copy first list into second list

l2=l1;

cout<<"l2 contains "<< l2->insert(30,0)<<endl;

l2->insert(40);

l2->insert(50);

if(l2->insert(10,100))

cout<<"Insertion successfuln"<<endl;

else

cout<<"Insertion could not takes place due to invalid indexn"<<endl;


cout<<"Modified l2 contains "<<l2->remove(2)<<endl;


if(l1==l2)

cout<<"Contents of l1 and l2 are samen"<<endl;

else

cout<<"Contents of l1 and l2 are differentn"<<endl;

dynListType *l3=l2;

cout<<"l3 contains "<<l3->remove(2)<<endl;

if(l3==l2)

cout<<"Contents of l3 and l2 are samen"<<endl;

else

cout<<"Contents of l3 and l2 are differentn"<<endl;

cout<<"l3 contains 20 at index "<<l3->search(20)<<endl;

cout<<"At index 1, l3 contains "<<l3->at(1)<<l3->search(30)<<endl;

dynListType *l4=*l1+l3;

cout<<"l4 contains "<<l4->remove(2)<<endl;

return 0;

}

//DynList.h

#ifndef _DYNLIST_H_

#define _DYNLIST_H_

#include <ostream>

class dynListType

{

private:

int *dataPtr; // static array storing data items

int size; // actual # of elements stored. size <= maxSize

int maxSize; // The capacity of this dynListType obj. 0 <= maxSize <= MAX.

public:

dynListType(int para); // constructor

dynListType(dynListType &obj); //copy constructor

~dynListType(); //destructor

int getSize() const; // return # of elements actually stored

int getMaxSize() const; //return capacity

bool isEmpty() const;

int search(int element) const; // look for an item and return index of first occurrence else -1

int at(int index) const; // return element at a specific location

void insert(int element); //insert an element at the end

bool insert(int index, int element); // insert an element into location index and shift remaining elements

void print() const; // print content of list on screen in format of [a, b, c] (like what ArrayList in Java does)

bool remove(int index); // remove element at the specified location and shift remaining elements

dynListType* operator +(const dynListType* l) const;


friend std::ostream &operator << ( std::ostream &output, const dynListType *l )

{

int sizen=l->getSize();

output<<"[ ";

for(int i=0;i<sizen;i++)

output << l->at(i)<<" ";

output<<"]";

return output;

}

void operator=(dynListType &obj); //overloading of assignment operator

bool operator==(dynListType obj);

};

#endif

//DynList.cpp

#include "DynList.h"

#include <iostream>

using namespace std;

dynListType::dynListType(int para=5) // constructor

{

maxSize = para;

dataPtr = new int[para];

}

dynListType::dynListType(dynListType &obj) //copy constructor

{

this->size = obj.size;

this->maxSize=obj.maxSize;

this->dataPtr=new int[this->size];

for(int i=0;i<size;i++)

{

this->dataPtr[i]=obj.dataPtr[i];

}

}

dynListType::~dynListType() //destructor

{

delete dataPtr; //release the memory

}

int dynListType::getSize() const { return size; } // return # of elements actually stored

int dynListType::getMaxSize() const { return maxSize; } // return capacity

bool dynListType::isEmpty() const { return size == 0; }

int dynListType::search(int element) const // look for an item and return index of first occurrence else -1

{

for(int i=0;i<size; i++) {

if(dataPtr[i]==element)

return i;

}

return -1; //indicating that element is not found

}

int dynListType::at(int index) const // return element at a specific location

{

if(index>=0 && index<=size)

return dataPtr[index];

std::cout<<"Invalid index accessed"<<std::endl;

}

void dynListType::insert(int element) //insert an element at the end

{

if(size>=0) {

dataPtr[size]=element;

}

else

{

int i;

maxSize*=2; //double the size

int *temp = new int[maxSize]; //create a temporary array

for(i=0;i<size;i++)

{

temp[i]=dataPtr[i]; //copy the elements

}

temp[i]=element; //insert new element at last

delete dataPtr; //release old memory

dataPtr = temp;

}

size++;

}

bool dynListType::insert(int index, int element) // insert an element into location index and shift remaining elements

{

if(index>=0&&index<=size)

{

int i;

if(size>maxSize)

{

maxSize*=2; //double the size

int *temp = new int[maxSize]; //create a temporary array

for(i=0;i<size;i++) {

temp[i]=dataPtr[i]; //copy the elements

}

delete dataPtr; //release old memory

dataPtr = temp;

}

size++;

for(i=size;i>index;i--)

dataPtr[i]=dataPtr[i-1];

dataPtr[i]=element;

return true;

}

return false; //invalid index was given

}

bool dynListType::remove(int index) // remove element at the specified location and shift remaining elements

{

if(index>=0&&index<=size) {

for(int i=index;i<size;i++) {

dataPtr[i]=dataPtr[i+1];

}

size--;

return true;

}

return false; //indicating invalid index

}

dynListType* dynListType::operator +(const dynListType* l) const

{

int size1=this->size,size2=l->getSize();

int nsize=size1+size2;

dynListType* templ=new dynListType(nsize);

for(int i=0;i<size;i++) {

templ->insert(this->at(i));

}

for(int i=0;i<size;i++) {

templ->insert(l->at(i));

}

return templ;

}

void dynListType::operator=(dynListType &obj) //overloading of assignment operator

{

this->size = obj.size;

this->maxSize=obj.maxSize;

this->dataPtr=new int[this->size];

for(int i=0;i<size;i++)

{

this->dataPtr[i]=obj.dataPtr[i];

}

}

bool dynListType::operator==(dynListType obj)

{

if(this->size!=obj.size) //both contains unequal number of elements

return false;

for(int i=0;i<size;i++)

{

if(this->dataPtr[i]!=obj.dataPtr[i])

return false; //unequal values

}

return true; //both contains same values in order

}

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes