9.6 A Add Entry 

 

NOTE: If you have already learned about templates, ignore all the typedef stuff.

 

void test_string(){
    int capacity = 3;
    int size = 0;
    T* list = allocate(capacity);
    list = add_entry(list, "Erika", size, capacity);
    print_array(list, size);

    list = add_entry(list, "Red", size, capacity);
    print_array(list, size);

    list = add_entry(list, "Bo", size, capacity);
    print_array(list, size);

    list = add_entry(list, "Pierson", size, capacity);
    print_array(list, size);

    list = add_entry(list, "Maher", size, capacity);
    print_array(list, size);

    list = add_entry(list, "Mac", size, capacity);
    print_array(list, size);

    list = add_entry(list, "Paula", size, capacity);
    print_array(list, size);


    cout<<"Deleting Erika"<<endl;

    list = remove_entry(list, "Erika", size, capacity);
    print_array(list, size);


    cout<<"Deleting Bo"<<endl;

    list = remove_entry(list, "Bo", size, capacity);
    print_array(list, size);

    cout<<"Deleting Maher"<<endl;

    list = remove_entry(list, "Maher", size, capacity);
    print_array(list, size);

    cout<<"Deleting Pierson"<<endl;

    list = remove_entry(list, "Pierson", size, capacity);
    print_array(list, size);

    cout<<"Deleting Red"<<endl;

    list = remove_entry(list, "Red", size, capacity);
    print_array(list, size);

}

 

 

typedef:

Gives the type string another name: T. From this point on, instead of string, you will use T. 

typedef int T;

Use this typedef so that your code is not quite as dependant on the string type.

 

The allocate function:

Very simple, but just to keep the confusion level down:

 

T* allocate(int capacity){
    const bool debug = false;
    if (debug) cout<<"allocate: capacity: "<<capacity<<endl;
    return new T[capacity];
}

Function Prototypes:  

template<class T>
T* add_entry(T* list, const T& new_entry,
                  int& size, int& capacity);
template<class T>
T* remove_entry(T* list, const T& delete_me,
                     int& size, int& capacity);

template <class T>
T *remove_last_entry(T *list, T &popped,
                     int &size, int &capacity);

template <class T>
T *insert_entry(T *list, const T &insert_this,
                int insert_here,
                int &size,
                int &capacity);

template <class T>
T *erase_entry(T *list, int index,
               int &size, int &capacity);