Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ : Extend your implementation of the arrayList template from Lab 3 to include a function called elemType getMin() that finds the smallest element in

C++ :

Extend your implementation of the arrayList template from Lab 3 to include a function called elemType getMin() that finds the smallest element in a list. Implement this function using recursion.

// arrayList.hpp

#ifndef arrayList_hpp

#define arrayList_hpp

#include

using namespace std;

template <class elemType>

class ArrayList

{

private:

elemType *listArray= new elemType[10];//data mem to store data

// elemType *t= listArray;

// int current; //current size

int max=10;

public:

int current=0;

ArrayList()

{

listArray = new elemType[max];

}

ArrayList(ArrayList *t)

{

listArray = t;

current = 0;

}

bool isEmpty() const

{

if(current == 0)

return true; //true if list is empty

else

return false;

}

bool isFull() const

{

if(current == max)

return true;

else

return false;

}

int listSize(){

return current;

}

int maxListSize(){

return max;

}

void print(){

for(int x = 0; x < current; x++)

// Displays the current index position data

cout<

cout<

}

bool isItemAtEqual(int pos, elemType data){

if(listArray[pos] == data)

return true;

else

return false;

}

void insertAt(int pos, elemType data){

if(pos >=0 && pos <= current)

{

// Loops from current position to given index position

for(int x = current; x >= pos; x--)

// Move data to next position

listArray[x+1] = listArray[x];

// Assigns data at parameter index position

listArray[pos] = data;

current++;

}

else

cout<<" Invalid position";

}

void insertEnd(elemType data){

// Calls the function to check if list is not full

if(!isFull())

{

// Adds the data at end position

listArray[current] = data;

// Increase the counter by one

current++;

}

else

cout<<" List is full";

}

void removeAt(int pos){

// Checks for valid index position

if(pos >=0 && pos <= current)

{

// Loops from parameter position to end of the list

for(int x = pos; x <= current; x++)

// Shift data to next

listArray[x] = listArray[x+1];

// Decrease the counter by one

current--;

}

else

cout<<" Invalid position";

}

elemType retreiveAt(int pos){

return listArray[pos];

}

void replaceAt(int pos, elemType data){

listArray[pos] = data;

}

void clearList(){

current = 0;

}

void operator = (ArrayList const &array) {

current = array.current;

max = array.max;

for(int i=0;i

listArray[i] = array.listArray[i];

}

}

};

#endif /* arrayList_hpp */

//main.cpp

#include

#include

using namespace std;

int main() {

ArrayList <int>myList;

cout<<" Maximum size: "<

cout<<" Current size: "<

// myList.print();

myList.insertEnd(10); //inserting 10 at the end

myList.insertEnd(20); //inserting 20 at the end

myList.insertEnd(30);

myList.insertEnd(40);

myList.insertEnd(50);

myList.insertEnd(60);

myList.insertEnd(70);

myList.insertEnd(80);

myList.insertEnd(90);

myList.insertEnd(100);

// myList.insertEnd(110);

myList.print();

myList.insertAt(1, 15); //inserting 15 at index 1 and output should be 10, 15, 20,..

myList.print();

myList.insertAt(12, 15); //invalid index 12

cout<

myList.insertAt(6, 99);

myList.removeAt(2); //removing value from index 2

myList.print();

myList.removeAt(20); //invalid index 20

cout<

cout<<"is full?"<

cout<<"is empty?"<

myList.replaceAt(3, 90); //90 at index 3

myList.print();

cout<//checking if index2 has 22

cout<//checking if index 3 has 90

myList.clearList(); //clears the list

cout<<"Current size: "<

ArrayList <double>myListD;

cout<<"Maximum size: "<

cout<<"Current size: "<

myList.insertEnd(15.1);

myList.insertEnd(29.2);

myList.insertEnd(36.2);

myList.insertEnd(43.4);

myList.print();

myList.insertAt(1, 19.6);

myList.print();

// cout<<"Current size: "<

myList.insertAt(12, 76.88);

cout<

myList.removeAt(2);

myList.print();

myList.removeAt(20);

cout<

cout<<"Current size: "<

cout<<"is full? "<

cout<<"is empty? "<

myList.replaceAt(3, 99.55);

myList.print();

cout<

cout<

myList.clearList(); //clears the list

cout<

cout<<"Current size: "<

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

Students also viewed these Databases questions