//show the number of data in array
cout << "Current # of Data:" << l.cnt(&list) << endl;
for (int i = 0; i < list.numOfData; i++)
{
cout << list.arr[i] << endl;
}
cout << endl;
//delete certain data value
if (LFirst(&list, &data))
{
if (data == 22)
l.remove(&list);
while (LNext(&list, &data))
{
if(data==22)
l.remove(&list);
}
}
cout << "Current # of Data:" << l.cnt(&list) << endl;
for (int i = 0; i < list.numOfData; i++)
{
cout << list.arr[i] << endl;
}
cout << endl;
system("pause");
return 0;
}
This code worked as I intended. But I changed the code for remove() like this using for-loop;
template <typename T>
T ListObj<T>::remove(ArrayList<T>* plist,T target)
{
for (int i = 0; i < plist->numOfData; i++)
{
if (plist->arr[i] == target)
{
int rpos = i;
int num = plist->numOfData;
T rdata = plist->arr[rpos];
for (int i = rpos; i < num - 1; i++)
plist->arr[i] = plist->arr[i + 1];
plist->numOfData--;
return rdata;
}
}
}
So, above function will be called like this;
l.remove(&list, 22);//22 is target value
In this code, remove() function is supposed to be called twice to delete target data (22 in this case). Therefore, if the code work properly, result should be 11 33 44. But the result is 11 22 33 44. This is, this function called only once. I tried several things to fix the problem but those didn’t work. If someone pick the bug or error in my code, I really appreciate.