Hey guys. I've been having this problem with a Dynamic list using pointers. I have built the class, then defined a MAX size to the list when first created. Then I thought about maybe the user wants to enter more values, so I made some changes. Here's the code:
class list
{
private:
int *buf;
int counter;
public:
list();
~list();
bool IsFull();
bool IsEmpty();
void AddFirst(int x);
void AddLast(int x);
int DeleteFirst();
int DeleteLast();
bool DeleteItem(int x);
int Length();
void Print();
};
list:: list()
{
buf = NULL;
counter = 0;
}
list:: ~list()
{
delete [] buf;
}
bool list:: IsFull()
{
return false;
}
bool list:: IsEmpty()
{
return counter == 0;
}
void list:: AddFirst(int x)
{
if(IsEmpty())
{
counter++;
buf = new int[counter];
buf[0] = x;
}
else
{
for (int i = counter; i > 0; i--)
buf[i] = buf[i-1];
counter++;
buf[0] = x;
}
}
void list:: AddLast(int x)
{
if(IsEmpty())
{
counter++;
buf = new int[counter];
buf[0] = x;
}
else
{
counter++;
buf[counter-1] = x;
}
}
void list:: Print()
{
for(int i = 0; i < counter; i++)
cout << buf[i] << endl;
}
int main(int argc, char *argv[])
{
list J;
J.AddFirst(-100);
J.AddLast(100);
J.AddFirst(-99);
J.AddLast(101);
J.Print();
system("PAUSE");
return EXIT_SUCCESS;
}
Now I know what I made is wrong, but my classteacher won't let me know, or give me an example, and I can't think of any other way. So, can anyone help me figure it out?
(I can't use vectors)