Using for-loop to Implement deletion in Array-based List

Hi,
I’m writing a code to implement Array-based List in C++ using class and template.
Here’s the code:

template <typename T>
struct ArrayList
{
T arr[LIST_LEN];
int curPos;
int numOfData;
};
//declare base class
template <typename T>
class ListObj
{
public:
ListObj(ArrayList<T>* plist);
void insert(ArrayList<T>* plist, T data);
T remove(ArrayList<T>* plist);
int cnt(ArrayList<T>* plist);
};
//declare members
template <typename T>
ListObj<T>::ListObj(ArrayList<T>* plist)
{
plist->curPos = -1;
plist->numOfData = 0;
}
template <typename T>
void ListObj<T>::insert(ArrayList<T>* plist, T data)
{
if (plist->numOfData >= LIST_LEN)
{
cout << "Out of Capacity!" << endl;
return;
}
plist->arr[plist->numOfData] = data;
plist->numOfData++;
}
template <typename T>
T ListObj<T>::remove(ArrayList<T>* plist)
{
int rpos = plist->curPos;
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--;
plist->curPos--;
return rdata;
}
template <typename T>
int ListObj<T>::cnt(ArrayList<T>* plist)
{
return plist->numOfData;
}
//miscellaneous functions
template <typename T>
bool LFirst(ArrayList<T>* plist, T* pdata)
{
if (plist->numOfData == 0)
return false;
plist->curPos = 0;
*pdata = plist->arr[0];
return true;
}
template <typename T>
bool LNext(ArrayList<T>* plist, T* pdata)
{
if (plist->curPos >= plist->numOfData - 1)
return false;
plist->curPos++;
*pdata = plist->arr[plist->curPos];
return true;
}
And, here’s the code for main function.
int main(void)
{
//initialization
ArrayList<int> list;
ListObj<int> l(&list);
int data;

//insert 5 integers
l.insert(&list, 11);
l.insert(&list, 22);
l.insert(&list, 22);
l.insert(&list, 33);
l.insert(&list, 44);

//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.
closed account (48bpfSEw)
just an idea:

if you delete an item within a loop

for (int i = 0; i < plist->numOfData; i++)

run the loop reverse:

for (int i = plist->numOfData-1; i>=0 ;i--)


reason: index mixed up!
Last edited on
Topic archived. No new replies allowed.