CODING in C++ Lab 3- Arrays Implementing Lists using Arrays What you will learrn
ID: 3746827 • Letter: C
Question
CODING in C++
Lab 3- Arrays Implementing Lists using Arrays What you will learrn Implementing arrays Implementing list ADT using templates Running time analysis Coding exercise Implement abstract data type list using arrays. Functionalities desired are as follows Function Constructors Destructors bool isEmpty() const bool isFull() const int listSize() const int maxListSize() const void print() bool isItemAtEqual(int, elemType) Checks if the item at position matches the 2nd parameter void insertAt (int, elemType) void insertEnd(elemType) void removeAt (int) elemType retreiveAt(int) void replaceAt(int, elemType) void clearList() operator= Description Decide if you need to use any parameters Especially required if you use dynamic memory management Checks if list is empt Checks if list is full Returns the size of the list Returns the maximum possible size of the list Prints the elements of the list on the console Inserts 2nd parameter at position Inserts object to end of the list Removes object at position Retrieves object at position Replaces object at position with 2nd parameter Empties the list Overload the assignment operator Here, elemType is the type of the members of the list. In a given list, all elements are of the same type You should use template implementation to enable functionality to have lists storing different types of objects dynamically What to turn in A zip file containing the arrayList.h file with your template declaration and implementation, and a main.cpp file with test cases to show that your program works For each function, analyze the running time of your algorithm. Report it in a file called report.pdf. Include your sources as references in report.pdf.Explanation / Answer
//File Name: ArrayList.h
#ifndef ArrayList_H
#define ArrayList_H
#include <iostream>
using namespace std;
// Defines a template class ArrayList
template <class elemType>
class ArrayList
{
// Data member to store data
elemType *listArray;
// Current size
int current;
public:
// Prototype of member function
ArrayList();
ArrayList(ArrayList *);
bool isEmpty();
bool isFull();
int listSize();
int maxListSize();
void print();
bool isItemAtEqual(int, elemType);
void insetAt(int, elemType);
void insertEnd(elemType);
void removeAt(int);
elemType retreiveAt(int);
void replaceAt(int, elemType);
void clearList();
};// End of class
#endif
----------------------------------------------------------------------------------------------
#include "ArrayList.h"
#include <iostream>
#define MAX 10
using namespace std;
// Default constructor definition
template<class elemType>
ArrayList<elemType>::ArrayList()
{
// Dynamically allocates list of size MAX
listArray = new elemType[MAX];
// Sets the current to zero
current = 0;
}// End of default constructor
// Parameterized constructor definition
template<class elemType>
ArrayList<elemType>::ArrayList(ArrayList *t)
{
// Assigns parameter t to data member listArray
listArray = t;
current = 0;
}// End of parameterized constructor
// Returns true if list is empty otherwise returns false
template<class elemType>
bool ArrayList<elemType>::isEmpty()
{
// Checks if current is zero returns true
if(current == 0)
return true;
// Otherwise returns false
else
return false;
}// End of function
// Returns true if list is full otherwise returns false
template<class elemType>
bool ArrayList<elemType>::isFull()
{
// Checks if current is equals to MAX returns true
if(current == MAX)
return true;
// Otherwise returns false
else
return false;
}// End of function
// Functin to return current size of the list
template<class elemType>
int ArrayList<elemType>::listSize()
{
return current;
}// End of function
// Function to return maximum size of the list
template<class elemType>
int ArrayList<elemType>::maxListSize()
{
return MAX;
}// End of function
// Function to print the list
template<class elemType>
void ArrayList<elemType>::print()
{
cout<<" ************** List Contents ************** ";
// Loops till end of the list
for(int x = 0; x < current; x++)
// Displays the current index position data
cout<<listArray[x]<<", ";
cout<<endl;
}// End of function
// Function to return true if given index position contains given data as parameter
template<class elemType>
bool ArrayList<elemType>::isItemAtEqual(int pos, elemType data)
{
// Checks if the given index position data is equals to parameter data return true
if(listArray[pos] == data)
return true;
// Otherwise returns false
else
return false;
}// End of function
template<class elemType>
void ArrayList<elemType>::insetAt(int pos, elemType data)
{
// Checks for valid index position
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++;
}// End of if condition
// Otherwise displays error message
else
cout<<" ERROR: Invalid position.";
}// End of function
// Function to insert data at end
template<class elemType>
void ArrayList<elemType>::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++;
}// End of if condition
// Otherwise displays error message
else
cout<<" ERROR: List full.";
}// End of function
// Function to remove data at given position
template<class elemType>
void ArrayList<elemType>::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--;
}// End of if condition
// Otherwise display error message
else
cout<<" ERROR: Invalid position.";
}// End of function
// Function to return data at given index position
template<class elemType>
elemType ArrayList<elemType>::retreiveAt(int pos)
{
return listArray[pos];
}// End of function
// Function to replace data at given index position
template<class elemType>
void ArrayList<elemType>::replaceAt(int pos, elemType data)
{
listArray[pos] = data;
}// End of function
// Function to clear the list
template<class elemType>
void ArrayList<elemType>::clearList()
{
current = 0;
}// End of function
// main function definition
int main()
{
ArrayList <int>myList;
cout<<" *********** Integer operation *********** ";
cout<<" Maximum size: "<<myList.maxListSize();
cout<<" Current size: "<<myList.listSize();
cout<<" Insert at end 10 ";
myList.insertEnd(10);
cout<<" Insert at end 20 ";
myList.insertEnd(20);
cout<<" Insert at end 30 ";
myList.insertEnd(30);
cout<<" Insert at end 40 ";
myList.insertEnd(40);
myList.print();
cout<<" Insert at 1 index position 15 ";
myList.insetAt(1, 15);
myList.print();
cout<<" Current size: "<<myList.listSize();
cout<<" Insert at 12 index position 85 ";
myList.insetAt(12, 15);
cout<<" Remove at 2 index position ";
myList.removeAt(2);
myList.print();
cout<<" Remove at 20 index position ";
myList.removeAt(20);
cout<<" Current size: "<<myList.listSize();
cout<<" Is FULL: "<<myList.isFull();
cout<<" Is EMPTY: "<<myList.isEmpty();
cout<<" Replace at 3 index position 90 ";
myList.replaceAt(3, 90);
myList.print();
cout<<" Checks 2 index position contains 20 or not: "<<myList.isItemAtEqual(2, 20);
cout<<" Checks 3 index position contains 90 or not: "<<myList.isItemAtEqual(3, 90);
cout<<" Clears the list ";
myList.clearList();
cout<<" Current size: "<<myList.listSize();
ArrayList <double>myListD;
cout<<" *********** Double operation *********** ";
cout<<" Maximum size: "<<myListD.maxListSize();
cout<<" Current size: "<<myListD.listSize();
cout<<" Insert at end 11.23 ";
myList.insertEnd(11.23);
cout<<" Insert at end 22.45 ";
myList.insertEnd(22.45);
cout<<" Insert at end 33.66 ";
myList.insertEnd(33.66);
cout<<" Insert at end 44.89 ";
myList.insertEnd(44.89);
myList.print();
cout<<" Insert at 1 index position 15.44 ";
myList.insetAt(1, 15.44);
myList.print();
cout<<" Current size: "<<myList.listSize();
cout<<" Insert at 12 index position 85.23 ";
myList.insetAt(12, 85.23);
cout<<" Remove at 2 index position ";
myList.removeAt(2);
myList.print();
cout<<" Remove at 20 index position ";
myList.removeAt(20);
cout<<" Current size: "<<myList.listSize();
cout<<" Is FULL: "<<myList.isFull();
cout<<" Is EMPTY: "<<myList.isEmpty();
cout<<" Replace at 3 index position 99.55 ";
myList.replaceAt(3, 99.55);
myList.print();
cout<<" Checks 3 index position contains 99.55 or not: "<<myList.isItemAtEqual(3, 99.55);
cout<<" Checks 2 index position contains 99.11 or not: "<<myList.isItemAtEqual(2, 99.11);
cout<<" Clears the list ";
myList.clearList();
cout<<" Current size: "<<myList.listSize();
}// End of main
Sample Output:
*********** Integer operation ***********
Maximum size: 10
Current size: 0
Insert at end 10
Insert at end 20
Insert at end 30
Insert at end 40
************** List Contents **************
10, 20, 30, 40,
Insert at 1 index position 15
************** List Contents **************
10, 15, 20, 30, 40,
Current size: 5
Insert at 12 index position 85
ERROR: Invalid position.
Remove at 2 index position
************** List Contents **************
10, 15, 30, 40,
Remove at 20 index position
ERROR: Invalid position.
Current size: 4
Is FULL: 0
Is EMPTY: 0
Replace at 3 index position 90
************** List Contents **************
10, 15, 30, 90,
Checks 2 index position contains 20 or not: 0
Checks 3 index position contains 90 or not: 1
Clears the list
Current size: 0
*********** Double operation ***********
Maximum size: 10
Current size: 0
Insert at end 11.23
Insert at end 22.45
Insert at end 33.66
Insert at end 44.89
************** List Contents **************
11, 22, 33, 44,
Insert at 1 index position 15.44
************** List Contents **************
11, 15, 22, 33, 44,
Current size: 5
Insert at 12 index position 85.23
ERROR: Invalid position.
Remove at 2 index position
************** List Contents **************
11, 15, 33, 44,
Remove at 20 index position
ERROR: Invalid position.
Current size: 4
Is FULL: 0
Is EMPTY: 0
Replace at 3 index position 99.55
************** List Contents **************
11, 15, 33, 99,
Checks 3 index position contains 99.55 or not: 1
Checks 2 index position contains 99.11 or not: 0
Clears the list
Current size: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.