The algorithm and data structure, from the very beginning, the realization of dynamic array is first seen.
Array.h
#include <iostream> #include <cstring> #include <cstdlib>using namespace Std;class array{public://* * Four functions of a class: constructor, copy constructor, overloaded assignment operator, destructor//* * The default constructor is array (int size, int val = 0);// Copy constructor Array (const array &array);//overloaded assignment operator array& operator= (const array &array);//destructor ~array ();//******** Heavy Duty//*************************************** int operator[] (int index) const;//******************************************** Additions/deletions//************************************************************************* *void insertat (int index, int val), void pushback (int val), void deleteat (int index), void SetAt (int index, int val), int geta T (int index), void SetSize (int size), int getsize (), void PrintArray ();p rivate:// initialize void Init ();//release dynamic memory void free ();//Determine if the following table is legal bool Isvalidindex (int index);//Expand Memory space void Getmoremememory ();p rivate: int *m_array;int M_size;int m_max;};/ /**************************************************************************//Private method//**************************** void Array::init () {m_size = 0;m_max = 1;m_array = new Int[m_max];} void Array::free () {delete[] M_array;} BOOL Array::isvalidindex (int index) {if (index >= 0 && Index < m_size) {return true;} return false;} void Array::getmoremememory () {M_max *= 2;int* tmp = new int[m_max];memcpy (tmp, M_array, m_size * sizeof (int));d elete[] M_a Rray;m_array = tmp;} Four functions of a class: constructor, copy constructor, overloaded assignment operator, destructor/ /**************************************************************************array::array (int size, int val) {if ( Size > 0) {m_size = Size;m_max = Size;m_array = new Int[m_max];for (int i=0; i < m_size; ++i) {M_array[I] = val;}} else if (size = = 0) {Init ();} Else{cout << "Invalid size:" << size << endl;exit (0);}} Array::array (const array& Array) {m_size = Array.m_size;m_max = Array.m_max;m_array = new int[array.m_max];memcpy (m_ Array, Array.m_array, Array.m_max * sizeof (int)); array& array::operator= (const array& Array) {if (this = = &array) {return *this;} M_size = Array.m_size;m_max = Array.m_size;m_array = new int[array.m_max];memcpy (M_array, Array.m_array, Array.m_max * si zeof (int)); return *this;} Array::~array () {free ();} Heavy Duty//***************************** int array::operator[] (int index) const{//This function constant function, cannot call ISVALIDINDEXIF ( Index >= 0 && Index < m_size) {return m_array[index];} Else{cout << "Invalid index:" << index << endl;exit (0);} Delete and Change//void Array::insertat (int index, int val) {if (!isvalidindex (index)) {cout << "Invalid index:" << index << endl;exit (0);} if (m_size >= m_max) {getmoremememory ();} for (int i = index; i < m_size; ++i) {m_array[i + i] = m_array[i];} M_array[index] = val;++m_size;} void Array::P ushback (int val) {if (m_size >= m_max) {getmoremememory ();} M_array[m_size] = val;++m_size;} void Array::D eleteat (int index) {if (!isvalidindex (index)) {cout << "Invalid index:" << index << Endl;ex It (0);} for (int i = index; i < m_size; ++i) {m_array[i] = m_array[i+1];m_array[m_size-1] = 0;--m_size;}} void Array::setat (int index, int val) {if (!isvalidindex (index)) {cout << "Invalid index:" << index << E Ndl;exit (0);} M_array[index] = val;} int array::getat (int index) {if (!isvalidindex (index)) {cout << Invalid index: "<< index << endl;exit (0 );} return M_array[index];} void ARRay::setsize (int size) {if (size < m_size) {for (int i = size; i < m_size; ++i) {m_array[i] = 0;}} else if (size >= m_size) {if (Size > M_max) {m_max = size;int* tmp = new int[m_max];memcpy (tmp, M_array, M_size*sizeo f (int)); m_array = tmp;} for (int i = m_size; i < size; ++i) {m_array[i] = 0;}} m_size = size;} int Array::getsize () {return m_size;} void Array::p rintarray () {if (m_size = = 0) {cout << "this Array is empty." << Endl; exit (0);} else{for (int i= 0; i<m_size; ++i) cout << "[" << I << "]" << m_array[i] << Endl;} cout << Endl;}
Main.cpp
#include "Array.h" int main () {Array arr1 (2); Arr1.printarray (); arr1. InsertAt (1, 1); Arr1.printarray (); arr1. Deleteat (1); Array arr2 (ARR1); Arr2.printarray (); arr1. SetAt (1, 1); Array ARR3 = Arr1;arr3.printarray (); Arr3. Pushback (2); cout << "Size:" << arr3. GetSize () << endl;cout << "GetAt (2):" << arr3. GetAt (2) << endl;cout << "[2]:" << arr3[2] << endl;system ("pause"); return 0;}
Output results
Come on, that's it.
Fundamentals of Algorithms and Data Structures 1: Dynamic arrays