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)

Recommended Answers

All 4 Replies

The only real problem Edward sees is that you don't resize the dynamic array after the first call to AddFirst or AddLast. If you don't allocate the memory, you can't reliably write to it. Here is a simple implementation that carefully allocates more memory for each new item:

#include <iostream>
#include <stdexcept>

class dynamic_array {
  int *_buf;
  int _size;

  // Disable copying for simplicity
  dynamic_array(const dynamic_array& rhs);
public:
  dynamic_array();
  ~dynamic_array();

  int Count() const;
  void AddFirst(int x);

  int operator[](int i) const;
};

dynamic_array::dynamic_array(): _buf(0), _size(0) {}

dynamic_array::~dynamic_array()
{
  delete[] _buf;
}

int dynamic_array::Count() const
{
  return _size;
}

void dynamic_array::AddFirst(int x)
{
  // Grow the array by one
  int *temp = new int[_size + 1];

  temp[0] = x;

  for (int i = 0; i < _size; ++i)
    temp[i + 1] = _buf[i];

  delete[] _buf;

  _buf = temp;
  ++_size;
}

int dynamic_array::operator[](int i) const
{
  if (_size == 0 || i < 0 || i > _size)
    throw std::range_error("Invalid index");

  return _buf[i];
}

int main()
{
  dynamic_array a;

  for (int i = 0; i < 10; ++i)
    a.AddFirst(i);

  for (int i = 0; i < a.Count(); ++i)
    std::cout << a[i] << '\n';
}
commented: Here's your second green dot. You deserve to have one :) +6

Thanks for your help. I had connection problems so I couldn't reply before. I didn't find any other way expect for creating a new dynamic list each time and adding all the items in buf, then assign buf to it.

Right, there's no other way if you stick to new and delete. C++ inherited realloc from C, but it basically does the same thing behind the scenes with the added benefit of being confusing and having unpredictable behavior with aliases. ;) It's better to use the simpler pattern of allocate-copy-release.

Yeah. I had to keep it as simple as possible cause I just started taking this course and I can't use anything other than the ones I learned so far. Thanks again for you time!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.