Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Data **values = null; // values is initialized in my insert function so it is
- // populated
- int count; // this keeps track of values' length
- bool remove(Data * x) {
- Data **newArray = new Data *[count - 1];
- for (int i = 0; i < count; i++) {
- while (x != values[i]) {
- newArray[i] = values[i];
- }
- count -= 1;
- return true;
- }
- values = newArray;
- return false;
- }
- count=3 values=[5,6,7] // initial insertion of 5, 6, 7
- five is a member of collection? 0
- count=3 values=[5,6] // removal of 0th element aka 5, but doesn't work
- five is a member of collection? 0
- count=4 values=[5,6,5] // re-insertion of 0th element (which is stored in
- five is a member of collection? 0 // my v0 variable)
- Data** it = std::find(values, values + count, x);
- if (it != values + count) {
- std::copy(it + 1, values + count, it);
- --count;
- return true;
- }
- return false;
- bool remove(Data * x)
- {
- bool found = false;
- // See if x is in the array.
- for (int i = 0; i < count; i++) {
- if (x != values[i]) {
- found = true;
- break;
- }
- }
- if (!found)
- {
- return false;
- }
- // Only need to create the array if the item to be removed is present
- Data **newArray = new Data *[count - 1];
- // Copy the content to the new array
- int newIndex = 0;
- for (int i = 0; i < count; i++)
- {
- if (x != values[i])
- newArray[newIndex++] = values[i];
- }
- // Now change the pointers.
- delete[] values;
- count--;
- values = newArray;
- return true;
- }
Add Comment
Please, Sign In to add comment