Question

I'm just not sure how to tackle all the template class this header wants me to...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1


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

Add a comment
Know the answer?
Add Answer to:
I'm just not sure how to tackle all the template class this header wants me to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    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...

    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...

    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...

    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...

  • (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

    (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...

  • 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() =...

    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...

  • This is a c++ class utilizing class templates and linked lists and Nodes. I need to...

    This is a c++ class utilizing class templates and linked lists and Nodes. I need to implement the following member function(s) to LinkedBag.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. In this case, I need to join the original items with the user items(a_bag). So if the original has {1,2,3} and a_bag has...

  • / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false,...

    / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false, bool predator=false); std::string getName() const; bool isDomestic() const; bool isPredator() const; void setName(std::string); void setDomestic(); void setPredator(); protected: // protected so that derived class can directly access them std::string name_; bool domestic_; bool predator_; }; #endif /* ANIMAL_H_ */ //end of Animal.h // /////////////////////////////////////////////////////////////////Animal.cpp #include "Animal.h" Animal::Animal(): name_(""),domestic_(false), predator_(false){ } Animal::Animal(std::string name, bool domestic, bool predator): name_(name),domestic_(domestic), predator_(predator) { } std::string Animal::getName() const{ return...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT