So I'm trying to add and remove one element one-by-one from a dynamically allocated array. My code below 100% works for add_entry, but not really works for remove_entry. What I did was from a list, I copied the elements from that list to new_list. Some of the steps seem not necessary but I had to put there because we're learning about pointers.
The add_entry code works. The remove_entry works for "deleting Erika" but not since "deleting Bo". I would love to get your feed back on this. Thank you very much!
#include <iostream>
#include <string>
using namespace std;
typedef string T;
T* add_entry(T* list, const T& new_entry, //
int& size, int& capacity);
T* remove_entry(T* list, const T& delete_me,
int& size, int& capacity);
T* allocate(int capacity);
void copy_list(T *dest, T* src, int many_to_copy);
T* search_entry(T* list, const T& find_me, int size);
void print_list(T* list, int size);
int main(){
int capacity = 3;
int size = 0;
T* list = allocate(capacity);
list = add_entry(list, "Erika", size, capacity);
print_list(list, size);
list = add_entry(list, "Red", size, capacity);
print_list(list, size);
list = add_entry(list, "Bo", size, capacity);
print_list(list, size);
list = add_entry(list, "Pierson", size, capacity);
print_list(list, size);
list = add_entry(list, "Maher", size, capacity);
print_list(list, size);
list = add_entry(list, "Mac", size, capacity);
print_list(list, size);
list = add_entry(list, "Paula", size, capacity);
print_list(list, size);
cout<<"Deleting Erika"<<endl;
list = remove_entry(list, "Erika", size, capacity);
print_list(list, size);
cout<<"Deleting Bo"<<endl;
list = remove_entry(list, "Bo", size, capacity);
print_list(list, size);
cout<<"Deleting Maher"<<endl;
list = remove_entry(list, "Maher", size, capacity);
print_list(list, size);
cout<<"Deleting Pierson"<<endl;
list = remove_entry(list, "Pierson", size, capacity);
print_list(list, size);
cout<<"Deleting Red"<<endl;
list = remove_entry(list, "Red", size, capacity);
print_list(list, size);
}
T* add_entry(T* list, const T& new_entry,
int& size, int& capacity) {
if (size == capacity) capacity*=2;
T* new_list = allocate(capacity);
copy_list(new_list, list, size);
new_list[size] = new_entry;
delete[] list;
size++;
return new_list;
}
T* remove_entry(T* list, const T& delete_me,
int& size, int& capacity) {
if (size == (int)(capacity/4)) {
capacity=(int)(capacity/2);
}
T* new_list = allocate(capacity);
copy_list(new_list, list, size);
for (int i, j; j < size; i++) {
if (list[i] != *search_entry(list, delete_me, size)) {
new_list[j] = list[i];
j++;
}
}
delete[] list;
size--;
return new_list;
}
T* allocate(int capacity) {
const bool debug = false;
if(debug) cout<<"Allocate: capacity: "<<capacity<<endl;
return new T[capacity];
}
void copy_list(T *dest, T* src, int many_to_copy) {
for (int i; i< many_to_copy; i++)
dest[i] = src[i];
}
T* search_entry(T* list, const T& find_me, int size) {
for (int i; i< size; i++) {
if (list[i] == find_me)
return list;
}
return nullptr;
}
void print_list(T* list, int size) {
for (int i; i < size; i++)
cout << list[i] << " ";
cout << endl;
}
capacity > sizeexactly because you don't want to reallocate every time. Just add a new element at a free place.typedef string T;, I was looking for templates.Tis a common generic template parameter.