Any clue why my delete function isnt working?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "SortedList.h"
#include "List.h"
using namespace std;
const int LISTLENGTH = 10000;

int main()
{
	List randList;

	for(int i = 0; i < LISTLENGTH; i++)
	{
        srand(time(0));
        randList.Insert(rand() % 10000);
	}

    while (randList.HasNext())
	{
        int y = randList.GetNextItem();
        if (y % 1000 != 0)
            randList.Delete(y);
    }
    randList.Sort();
    while (randList.HasNext())
    {
        int x = randList.GetNextItem();
        cout << x << ' ';
    }
    randList.Reset();


	cin.get();
	return 0;
}


So that is my main...

1
2
3
4
5
6
7
8
9
10
11
void List::Delete(ItemType item)
{
    int i = 0;
        while (i < length && data[i] != item)
            i++;
        if(i < length)
        {
            data[i] = data[length - 1];
            length--;
        };
}


This is my delete function, it has an appropriate header and class and such, but I cant see why this isn't working.

The intent of the code is to create a randomly generated list of 10000 integers, then remove any that are not multiples of 1000. Instead it deletes some, which is only obvious due to the variance in list size after execution. It doesnt seem to delete many at all, however... and I end up with a very long list of integers ranging from 1 to 9999
Last edited on
I'm going to go out on a limb and say something is wrong with your List class.
Topic archived. No new replies allowed.