Question
Have issues with this code . The NULL identifier not found ArrayList.h #pragma once template class ArrayList { private: // attributes const static int DEFAULT_SIZE
Have issues with this code . The NULL identifier not found
ArrayList.h
#pragma once
template
class ArrayList
{
private:
// attributes
const static int DEFAULT_SIZE = 5; // constant for the initial size
T* list; // pointer to the array
int count; // number of items in the list
int capacity; // current size in memory
public:
// constructors
ArrayList(void)
{
this->list = new T[DEFAULT_SIZE];
this->capacity = DEFAULT_SIZE;
this->count = 0;
}
ArrayList(int initialCapacity)
{
this->list = new T[initialCapacity];
this->capacity = initialCapacity;
this->count = 0;
}
// destructor
~ArrayList(void)
{
// delete the array pointer
if (this->list != nullptr)
{
delete[] this->list;
this->list = nullptr;
}
}
/// Determine if the ArrayList is empty
bool isEmpty(void)
{
return count == 0; // array is empty if it has zero items
}
/// Get the item at the given position
T get(int position)
{
if (position < count)
return list[position];
else
return NULL;
}
/// Add an item to the ArrayList
void add(T data)
{
// if the array is full, double the size
if (count == capacity)
{
// bigger array
capacity = 2 * capacity;
T* temp = new T[capacity];
// copy items from current array to bigger array
for (int i = 0; i < count; i++)
{
temp[i] = list[i];
}
// delete the current array
delete[] list;
// rename the bigger array to the current array name
list = temp;
}
// add the data item to the array
list[count] = data;
// increment the count
count++;
}
/// Remove item at the given position
void removeAt(int position)
{
// replace every item from that position on with the next item
for (int i = position; i < count - 1; i++) // notice "count - 1" to copy last item to next to last position
{
list[i] = list[i + 1];
}
// decrease the item count
count--;
}
/// Get the count of items in the ArrayList
int getCount(void)
{
return count;
}
/// Get the ArrayList current capacity
int getCapacity()
{
return capacity;
}
};
Source.cpp
#include
#include
#include
#include "ArrayList.h"
using namespace std;
/// Entry point to the application
int main(void)
{
// check for memory leaks
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
// an int ArrayList
ArrayList
intList.add(27);
intList.add(13);
intList.add(42);
intList.add(31);
intList.add(22);
intList.add(19);
for (int i = 0; i < intList.getCount(); i++)
{
cout << intList.get(i) << ", ";
}
cout << " " << endl;
cout << "Count: " << intList.getCount() << endl;
cout << "Capacity: " << intList.getCapacity() << endl;
cout << " " << endl;
// a string ArrayList
ArrayList
strList.add("Brendan");
strList.add("Maria");
strList.add("Ivan");
strList.add("Nathan");
// display list data
for (int i = 0; i < strList.getCount(); i++)
{
cout << strList.get(i) << ", ";
}
cout << " " << endl;
cout << "Count: " << strList.getCount() << endl;
cout << "Capacity: " << strList.getCapacity() << endl;
// pause
cout << " Press any key to continue...";
cin.sync();
_getch();
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started