//**********************************************************************
// ArrayBasedLists.h by hubo
// 6/22/2014
// Description:
// The class specifies the members to implement the basic properties
// of array-based lists.
//**********************************************************************
namespace DataStructure
{
//**********************************************************************
// class template ArrayListType.
//**********************************************************************
template<typename ElemType>
class ArrayListType
{
//1. Type definition or declaration.
public:
using value_type = ElemType;
//2. The definition of data member.
protected:
value_type* m_pElem;
int m_iSize;
int m_iCapacity;
//3. Constructor.
public:
ArrayListType(int defaultSize = 100);
//deep copy.
ArrayListType(const ArrayListType<value_type>& otherList);
ArrayListType(ArrayListType<value_type>&& otherList);
//4. Destructor.
public:
~ArrayListType();
//5. Assignment.
public:
ArrayListType<value_type>& operator=(const ArrayListType<value_type>& otherList);
ArrayListType<value_type>& operator=(ArrayListType<value_type>&& otherList);
//6. Interface.
public:
//Function: Clear all elements in the list.
//Precondition: The list exists. m_pElem != nullptr.
//Postcondition: Return true if succeeds. m_iSize = 0. otherwise, return false.
bool clearList();
//Function: Determine whether the list is empty.
//Precondition: The list exists. m_pElem != nullptr.
//Postcondition: According to the truth of m_iSize equals or not equals 0, return true if m_iSize = 0; otherwise,
// return false.
bool isEmpty() const;
//Function: Determine whether the list is full.
//Precondition: The list exists. m_pElem != nullptr.
//Postcondition: According to the truth of m_iSize equals or not equals m_iCapacity, return true
// if m_iSize = m_iCapacity; otherwise, return false.
bool isFull() const;
//Function: Determine the number of elements in the list.
//Precondition: The list exists. m_pElem != nullptr.
//Postcondition: Return the value of m_iSize.
int listSize() const;
//Function: Determine the capacity of the list.
//Precondition: The list exists. m_pElem != nullptr.
//Postcondition: Return the capacity of the list.
int listCapacity() const;
//Function: Determine whether the item is the same as the item in the list at the position specified by location.
//Precondition: The list exists, m_pElem != nullptr, and is not empty, and the value of location
// fall into [0,m_iSize).
//Postcondition: Return ture if list[location] is the same as the item; otherwise, return false.
bool isItemAtEqual(int location, const value_type& item) const;
//Function: Insert an item in the list at the position specified by location.
//Precondition: The list exists, m_pElem != nullptr, and is not full, and the value of location fall into [0,m_iSize].
//Postcondition: Return true if succeeds; otherwise, return false. list[location] = Item, ++m_iSize.
bool insertAt(int location, const value_type& item);
//Function: Insert an item at the end of the list.
//Precondition: The list exists, m_pElem != nullptr, and is not full.
//Postcondition: Return true if succeeds; otherwise, return false. list[m_iSize] = item, ++m_iSize.
bool insertEnd(const value_type& item);
//Function: Remove an item in the list at the position specified by location.
//Precondition: The list exists, m_pElem != nullptr, and is not empty, the value of location fall into [0,m_iSize).
//Postcondition: Return true if succeeds; otherwise, return false. --m_iSize.
bool removeAt(int location);
//Function: Replace an item in the list at the position specified by location.
//Precondition: The list exists, m_pElem != nullptr, and is not empty, and the value of location fall into [0,m_iSize).
//Postcondition: Return true if secceeds; otherwise, return false. list[location] = item.
bool replaceAt(int location, const value_type& item);
//Function: Retrieve an item in the list at the position specified by location.
//Precondition: The list exists, m_pElem != nullptr, and is not empty, and the value of location fall into [0,m_iSize).
//Postcondition: Return true if secceeds; otherwise, return false. item = list[location].
bool retrieveAt(int location, value_type& item) const;
//Function: Search the list for a given item.
//Precondition: The list exists , m_pElem != nullptr, and is not empty.
//Postcondition: If the item is found, returns the location in the array where the item is found;
// otherwise, returns -1.
int seqSearch(const value_type& item) const;
//Function: Insert the item specified by the parameter item.
//Precondition: The list exists , m_pElem != nullptr, and is not full.
//Postcondition: If the item specified by the parameter item doesn's exist in the list, insert the item at
// the end of the list, return true. list[m_iSize] = item, ++m_iSize. otherwise, return false.
bool insert(const value_type& item);
//Function: Remove the item specified by the parameter item.
//Precondition: The list exists, m_pElem != nullptr, and is not empty.
//Postcondtion: If the item specified by the parameter doesn's exist in the list, return false; otherwise,
// return true, --m_iSize.
bool remove(const value_type& item);
//Function: Resize the capacity of the list.
//Postcondition: Return true, if succeeds. m_iCapacity = size. otherwise, return false.
bool resize(int size);
//7. Help Function.
private:
//Function: Expand the capacity of the list.
//Postcondition: Return the address of allocated memory.
value_type* expand(int size);
//Function:
}; // class template ArrayListType
} // DataStructure
#include "ArrayBasedLists.inl"
//**********************************************************************
// ArrayBasedLists.inl by hubo
// 6/22/2014
// Description:
// The implement to class template ArrayListType.
//**********************************************************************
namespace DataStructure
{
template<typename ElemType>
ArrayListType<ElemType>::ArrayListType(int defaultSize/* 100 */)
{
m_pElem = new value_type[defaultSize];
m_iCapacity = defaultSize;
m_iSize = 0;
}
template<typename ElemType>
ArrayListType<ElemType>::ArrayListType(const ArrayListType<value_type>& otherList)
{
if (m_pElem != nullptr)
{
delete[] m_pElem;
}
m_iCapacity = m_iCapacity;
m_pElem = new value_type[m_iCapacity];
m_iSize = otherList.m_iSize;
for (int i = 0; i < m_iSize; ++i)
{
*(m_pElem + i) = *(otherList.m_pElem + i);
}
}
template<typename ElemType>
ArrayListType<ElemType>::ArrayListType(ArrayListType<value_type>&& otherList)
{
if (m_pElem != nullptr)
{
delete[] m_pElem;
}
m_pElem = otherList.m_pElem;
m_iSize = otherList.m_iSize;
m_iCapacity = otherList.m_iCapacity;
otherList.m_pElem = nullptr;
otherList.m_iSize = 0;
otherList.m_iCapacity = 0;
}
template<typename ElemType>
ArrayListType<ElemType>::~ArrayListType()
{
if (m_pElem != nullptr)
{
delete[] m_pElem;
}
}
template<typename ElemType>
ArrayListType<ElemType>& ArrayListType<ElemType>::operator=(const ArrayListType<value_type>& otherList)
{
if (m_pElem != nullptr)
{
delete[] m_pElem;
}
m_iSize = otherList.m_iSize;
m_iCapacity = otherList.m_iCapacity;
m_pElem = new value_type[m_iCapacity];
for (int i = 0; i < m_iSize; ++i)
{
*(m_pElem + i) = *(otherList.m_pElem + i);
}
}
template<typename ElemType>
ArrayListType<ElemType>& ArrayListType<ElemType>::operator=(ArrayListType<value_type>&& otherList)
{
if (m_pElem != nullptr)
{
delete[] m_pElem;
}
m_pElem = otherList.m_pElem;
m_iSize = otherList.m_iSize;
m_iCapacity = otherList.m_iCapacity;
otherList.m_pElem = nullptr;
otherList.m_iSize = 0;
otherList.m_iCapacity = 0;
}
template<typename ElemType>
bool ArrayListType<ElemType>::clearList()
{
if (m_pElem != nullptr)
{
m_iSize = 0;
return true;
}
return false;
}
template<typename ElemType>
bool ArrayListType<ElemType>::isEmpty() const
{
if (m_pElem != nullptr)
{
if (m_iSize == 0)
{
return true;
}
else
{
return false;
}
}
return false;
}
template<typename ElemType>
bool ArrayListType<ElemType>::isFull() const
{
if (m_pElem != nullptr)
{
if (m_iSize >= m_iCapacity)
{
return true;
}
else
{
return false;
}
}
return false;
}
template<typename ElemType>
int ArrayListType<ElemType>::listSize() const
{
if (m_pElem != nullptr)
{
return m_iSize;
}
return -1;
}
template<typename ElemType>
int ArrayListType<ElemType>::listCapacity() const
{
if (m_pElem != nullptr)
{
return m_iCapacity;
}
return -1;
}
template<typename ElemType>
bool ArrayListType<ElemType>::isItemAtEqual(int location, const value_type& item) const
{
if (m_pElem != nullptr)
{
if (location >= 0 && location < m_iSize)
{
if (m_pElem[location] == item) return true;
else return false;
}
return false;
}
return false;
}
template<typename ElemType>
bool ArrayListType<ElemType>::insertAt(int location, const value_type& item)
{
if (m_pElem != false)
{
if (!isFull())
{
if (location >= 0 && location <= m_iSize)
{
for (int i = m_iSize; i > location; --i)
{
m_pElem[i] = m_pElem[i - 1];
}
m_pElem[location] = item;
++m_iSize;
return true;
}
return false;
}
return false;
}
return false;
}
template<typename ElemType>
bool ArrayListType<ElemType>::insertEnd(const value_type& item)
{
if (m_pElem != nullptr)
{
if (!isFull())
{
m_pElem[m_iSize] = item;
++m_iSize;
return true;
}
return false;
}
return false;
}
template<typename ElemType>
bool ArrayListType<ElemType>::removeAt(int location)
{
if (m_pElem != nullptr)
{
if (!isEmpty())
{
if (location >= 0 && location < m_iSize)
{
for (int i = location; i < m_iSize - 1; ++i)
{
m_pElem[i] = m_pElem[i + 1];
}
--m_iSize;
return true;
}
return false;
}
return false;
}
return false;
}
template<typename ElemType>
bool ArrayListType<ElemType>::replaceAt(int location, const value_type& item)
{
if (m_pElem != nullptr)
{
if (!isEmpty())
{
if (location >= 0 && location < m_iSize)
{
m_pElem[location] = item;
return true;
}
return false;
}
return false;
}
return false;
}
template<typename ElemType>
bool ArrayListType<ElemType>::retrieveAt(int location, value_type& item) const
{
if (m_pElem != nullptr)
{
if (!isEmpty())
{
if (location >= 0 && location < m_iSize)
{
item = m_pElem[location];
return true;
}
return false;
}
return false;
}
return false;
}
template<typename ElemType>
int ArrayListType<ElemType>::seqSearch(const value_type& item) const
{
if (m_pElem != nullptr)
{
if (!isEmpty())
{
for (int i = 0; i < m_iSize; ++i)
{
if (m_pElem[i] == item) return i;
}
return -1;
}
return -1;
}
return -1;
}
template<typename ElemType>
bool ArrayListType<ElemType>::insert(const value_type& item)
{
if (m_pElem != nullptr)
{
if (!isFull())
{
for (int i = 0; i < m_iSize; ++i)
{
if (m_pElem[i] == item) return false;
}
m_pElem[m_iSize] = item;
++m_iSize;
return true;
}
return false;
}
return false;
}
template<typename ElemType>
bool ArrayListType<ElemType>::remove(const value_type& item)
{
if (m_pElem != nullptr)
{
if (!isEmpty())
{
for (int i = 0; i < m_iSize; ++i)
{
if (m_pElem[i] == item)
{
for (int j = i; j < m_iSize - 1; ++j)
{
m_pElem[j] = m_pElem[j + 1];
}
--m_iSize;
return true;
}
}
return false;
}
return false;
}
return false;
}
template<typename ElemType>
bool ArrayListType<ElemType>::resize(int size)
{
if (expand(size))
{
return true;
}
else
{
return false;
}
}
template<typename ElemType>
auto ArrayListType<ElemType>::expand(int size)->value_type*
{
void* pElem = nullptr;
if (m_pElem != nullptr)
{
pElem = new value_type[size];
for (int i = 0; i < m_iSize; ++i)
{
pElem[i] = m_pElem[i];
}
delete[] m_pElem;
m_iCapacity = size;
if (size < m_iSize) m_iSize = size;
return pElem;
}
else
{
m_pElem = new value_type[size];
m_iSize = 0;
m_iCapacity = size;
return pElem;
}
}
} // DataStructure