-1

I have the following homework assignment for data structures course.

Design and implement an ordered list class using an array of pointers. This class should be a template. The template is expected to have overloaded the >, < and == operators.

  1. The class should have an array that holds 20 items.
  2. The AddItem method should start at the front of the array when searching for a place to insert a new item.
  3. The RemoveItem method should ensure that the items in the array are still in order and there are no empty spots between items.
  4. The class should include IsEmpty, IsFull and MakeEmpty methods.

I have a working implementation of an ordered list class, but the assignment says that my template class should overload some comparison operators, and I'm not sure why I would need to do that. I've read What are the basic rules and idioms for operator overloading?, but not sure how to apply that here.

Here is my current solution:

#include <cstddef>
#include <iostream>

using namespace std;

#define LEN 20

template <class T> class orderedList
{
  protected:
    T *arr[LEN] = {}; // Array of pointers
  public:
    void addItem(T item);
    void removeItem(T item);
    bool isEmpty();
    bool isFull();
    void makeEmpty();
    bool operator<(const T& rhs) const;
    bool operator>(const T& rhs) const;
    bool operator==(const T& rhs) const;
};
// Overloaded comparison operators
template <class T> bool orderedList<T>::operator<(const T& rhs) const {
  return this < rhs;
}
template <class T> bool orderedList<T>::operator>(const T& rhs) const {
  return rhs < this;
}
template <class T> bool orderedList<T>::operator==(const T& rhs) const {
  return this == rhs;
}

template <class T> void orderedList<T>::addItem(T item)
{
  T temp1 = item;
  T temp2;
  for (int i=0; i<LEN; i++) {
    if(arr[i] == NULL) {
      arr[i] = new T;
      *arr[i] = temp1;
      return;
    } else if (*arr[i] > item) {
      temp2 = *arr[i];
      *arr[i] = temp1;
      temp1 = temp2;
    } else {
      continue;
    }
  }
  cout << "full error!" << endl;
}
template <class T> void orderedList<T>::removeItem(T item)
{
  int cur = 0;
  while( cur<LEN && arr[cur] != NULL && item > *arr[cur]) {
    cur++;
  }
  if (*arr[cur] == item) {
    while(cur+1<LEN && arr[cur+1] != NULL) {
      *arr[cur] = *arr[cur+1];
      cur++;
    }
    delete arr[cur];
    arr[cur] = NULL;
  } else {
    cout << "not found error!" << endl;
  }
}
template <class T> bool orderedList<T>::isEmpty()
{
  for(int i=0; i<LEN; i++) {
    if (arr[i] != NULL)
      return false;
  }
  return true;
}
template <class T> bool orderedList<T>::isFull()
{
  // Traverse in reverse for speed
  for(int i=LEN-1; i>0; i--) {
    if (arr[i] == NULL)
      return false;
  }
  return true;
}
template <class T> void orderedList<T>::makeEmpty()
{
  for(int i=0; i<LEN; i++) {
    if (arr[i] != NULL) {
      delete arr[i];
      arr[i] = NULL;
    }
  }
}

This seems to be working, but the overloaded comparison operators are not doing anything special nor do I see any reason they need to. My best guess is that I should try include checks for NULL in the comparison operators, but I'm not sure if that's even possible.

Community
  • 1
  • 1
Robert
  • 774
  • 4
  • 12
  • StackOverflow will not do your homework for you. – Charles Sep 17 '17 at 18:01
  • 1
    You don't want to have things like _arrays of pointers_ when programming in c++. There's no need to have such. – user0042 Sep 17 '17 at 18:04
  • Thanks, @Charles. I'm just trying to understand **why** I would need to overload operators here. I've read https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions and believe I am following all of the guidelines. I made a good faith effort to solve, am asking a specific question about my implementation, and clearly identified this as homework. – Robert Sep 17 '17 at 18:05
  • @user0042 I can't change that. This is the assignment as it was given, so :shrug:. – Robert Sep 17 '17 at 18:06
  • I don't understand why you'd want to compare an `orderedList` to something of type `T`. – Charles Sep 17 '17 at 18:07
  • @Charles, is that what I'm doing here? My best guess is that I'm supposed to be comparing objects of type `T` (and that's what I'm trying to do), but maybe I'm doing that wrong here. I'm in the same boat, I don't understand why this is necessary. – Robert Sep 17 '17 at 18:12
  • @Rob, you need to compare `orderedList`s. – Charles Sep 17 '17 at 18:15
  • @user0042 You also don't want to implement your own list of up to 20 elements, but sometimes you have to because there's an assignment to turn in. An array of pointers would be one easy way to implement such a thing if your element type is allowed to roam without having a default constructor. – n. m. could be an AI Sep 17 '17 at 18:24

1 Answers1

1

Disclaimer: I cannot read your instructors' minds, below is my best interpretation of your assignment.

The assignment wants you to implement the equality test. Checking any two things for equality should be intuitively obvious. Two lists are equal if they contain the same elements in the same order. Think of your lists as words, and your list elements as letters. If you can compare letters, how would you decide whether two words are the same?

The assignment also wants you to implement ordering tests < and >. This might be less intuitive, but there's in fact a well defined notion of ordering for lists, called lexicographical ordering. Again, think of your lists as words, and your list elements as letters. How do you decide which of any two words comes before the other in a dictionary? That's lexicographical ordering.

It makes sense to compare two words, but it makes little sense to compare a word with a letter. Look at your overloaded operator signatures. How do you modify them so that they more resemble comparing two words?

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243