I'm just not sure how to tackle all the template class this header wants me to write. I got this far into making the template for public and private and would like help on the template functions. Thank you! This is in C++ by the way.
#include <iostream>
#include <cassert>
using namespace std;
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
template<typename T>
class arrayList
{
public:
arrayList();
//Constructor with default parameter.
//Sets maxSize = 100 and length = 0 if no parameter is
provided
//Sets maxSize = <n> and length = 0 if <n> is provided
and it is larger than 0 (otherwise use default value)
arrayList(int = 100);
//Copy constructor.
//Must achieve deep copy of original arrayList
arrayList(const arrayList<T>&);
//Overloaded assignment operator =.
//Must achieve deep copy of original arrayList
arrayList<T>& operator=(const
arrayList<T>&);
//isEmpty returns true if list is empty, false
otherwise
bool isEmpty() const;
//isEmpty returns true if list is full, false
otherwise
bool isFull() const;
//Return length
int listSize( ) const;
//Return maxSize
int maxListSize( ) const;
//Print all elements in the list separated by a single
space
void print() const;
//Empty list
void clearList( );
//Remove from list item at a given position.
//If position is out of bounds, print an error
message.
void removeAt(int);
//Remove from list an item.
//If the item is not found, or if list is empty, print
an error message.
void remove(const T&);
//Add item to list preserving ascending order.
//If list is full, print an error message.
void insert(const T&);
//Access an item in the list at a given
position.
//If position is out of bounds, print an error
message.
//Otherwise, return found item in parameter d.
void retrieveAt(int pos, T& d) const;
//Search an item in the list and return its
position.
//If not found, return -1;
int search(const T&) const;
~arrayList( );
private:
T* list;
int length;
int maxSize;
};
#endif
//Write your functions below
template<typename T>
arrayList<T>::arrayList()
{
// no instruction given
}
template<typename T>
arrayList<T>::arrayList(int n = 100) {
maxSize = n;
length = 0;
list = new int[maxSize];
}
template<typename T>
arrayList<T>::arrayList(const arrayList<T>& x)
{
list = x.list;
maxSize = x.maxSize;
length = x.length;
list = new T[maxSize];
for (int k = 0; k<x.length; k++)
{
list[k] = x.list[k];
}
}
template <typename T>
arrayList<T>& arrayList<T>::operator=(const
arrayList<T>& rhs)
{
T *tmp;
tmp = new T[rhs.maxSize];
for (int i = 0; i< rhs.length; i++)
tmp[i] = rhs.list[i];
delete[] list;
list = tmp;
length = rhs.length;
maxSize = rhs.maxSize;
return *this;
}
template <typename T>
bool arrayList<T>::isEmpty() const
{
return (length == 0 ? true : false);
}
template <typename T>
bool arrayList<T>::isFull() const
{
return (length == maxSize ? true : false);
}
template <typename T>
int arrayList<T>::listSize() const
{
return length;
}
template <typename T>
void arrayList<T>::clearList()
{
maxSize = 0;
length = 0;
delete[] list;
list = NULL;
}
template<typename T>
void arrayList<T>::removeAt(int index)
{
for (int i = index; i<length; i++)
list[i] = list[i + 1];
length--;
}
template<typename T>
void arrayList<T>::remove(const T & t)
{
for (int i = 0; i < length; i++)
{
if (list[i] == t)
list[i] = list[i + 1];
}
length--;
}
template<typename T>
void arrayList<T>::insert(const T & val)
{
if (length < maxSize) {
list[length] = val;
length = length + 1;
for (int i = 1; i < length; i++)
{
int temp =
list[i];
int j = i -
1;
while (j >= 0
&& list[j] > temp) {
list[j + 1] = list[j];
j--;
}
list[j + 1] =
temp;
}
}
else {
cout << "Full" <<
endl;
}
}
template<typename T>
void arrayList<T>::retrieveAt(int pos, T & d) const
{
if (pos > length) {
cout < , "Error out of bound
";
return 0;
}
else {
d = list[pos];
return d;
}
}
template<typename T>
int arrayList<T>::search(const T & x) const
{
int found = 1;
for (int k = 0; k < length; k++)
{
if (list[k] == x)
{
found= k;
break;
}
else {
found=-1;
}
}
if (found == -1) {
return -1;
}
else {
return found;
}
}
}
template<typename T>
arrayList<T>::~arrayList()
{
}
template <typename T>
int arrayList<T>::maxListSize() const
{
return maxSize;
};
template <typename T>
void arrayList<T>::print() const {
for (int i = 0; i < length; i++) {
cout << list[i] << "
";
}
cout << endl;
}
IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE HIT THE LIKE BUTTON
I'm just not sure how to tackle all the template class this header wants me to...
The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public: ...
This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...
C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...
How do I do this? -> string print() const; Function to output the data, return the output in the form of string, with all elements in the hash table included and each seperated by a space this is my code: template <class elemType> string hashT<elemType>::print() const { for (int i = 0; i < HTSize; i++){ if (indexStatusList[i] == 1){ cout <<HTable[i]<< " " << endl; } } } **********Rest of the code...
PART 1 Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is, if the list is full when an item is being added to this list, the elements will be moved to a larger array. The new array should have twice the size of the original array. Using the new class ArrayList, write a program to store 1,000 random numbers, each in the interval [0, 500]. The initial size of the array in the class should...
This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...
Please help me finish my code. Before the final pause in the main function, create an ArrayList of another type such as double, char, short, bool, float, etc. Your choice. Add five data items to your array as we did for the int and string array lists. Print them out with a for loop as we did before. Print out the count and capacity of your new array list. Source.cpp #include #include #include #include "ArrayList.h" using namespace std; /// Entry...
template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...
(The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...