/*FileName:Array.h
*Author:Lupin
* Description:A simple array with iterator for int
*/
#ifndef ARRAY_H_
#define ARRAY_H_
#include<stdexcept>
namespace DataStruct
{
class Array
{
class Iterator
{
private:
Array* _array;
int _length;
int _current;
public:
Iterator( Array* array, int length );
//Get the next element
const int Next();
//Reset the iterator
void Reset();
};
private:
int _length, _maxSize;
int* _list;
public:
Array();
Array( int maxSize );
virtual ~Array();
//Return the length of the array
int Length() const;
//Return the Max Size of the array
int MaxSize() const;
//If the array is full
bool IsFull() const;
//If the array is empty
bool IsEmpty() const;
//To append the @elem to the end of the array
void Append( int elem );
//To Get the element at the position of @pos
int Get( int pos ) const;
//To remove the element at the position of @pos
void Remove( int pos );
//To find the position of the @elem in the array
int Find( int elem );
};
}
#endif /*ARRAY_H_*/
1 /*FileName:Array.cpp
2 *Author:Lupin
3 * Description:A simple array with iterator for int
4 */
5 #include "Array.h"
6
7 namespace DataStruct
8 {
9
10 Array::Array()
11 {
12 _maxSize = 100;
13 _list = new int[ 100 ];
14 }
15 Array::Array( int maxSize )
16 {
17 _maxSize = maxSize;
18 _list = new int[ _maxSize ];
19 }
20
21 Array::~Array()
22 {
23 delete[] _list;
24 }
25
26 int Array::Length() const
27 {
28 return _length;
29 }
30
31 int Array::MaxSize() const
32 {
33 return _maxSize;
34 }
35
36 bool Array::IsFull() const
37 {
38 return ( _length == _maxSize );
39 }
40
41 bool Array::IsEmpty() const
42 {
43 return ( _length == 0 );
44 }
45
46 void Array::Append( int elem )
47 {
48 if( IsFull() )
49 throw range_error( "The array is full" );
50
51 _list[ _length++ ] = elem;
52 }
53
54 int Array::Get( int pos ) const
55 {
56 if( pos < 0 || pos >= _length )
57 throw out_of_range( "You can't get a element isn's exsits" );
58
59 return _list[ pos ];
60 }
61
62 void Array::Remove( int pos )
63 {
64 if( pos < 0 || pos > _length )
65 throw out_of_range( "No element at the position" );
66
67 _length--;
68 int i = pos;
69 while( i < _length )
70 _list[ i++ ] = _list[ i + 1 ];
71 }
72
73 int Array::Find( int elem )
74 {
75 int i = 0;
76 while( i < _length )
77 {
78 if( _list[ i++ ] == elem )
79 return i-1;
80 }
81 return -1;
82 }
83
84 Array::Iterator::Iterator( Array* array, int length )
85 {
86 _array = array;
87 _length = length;
88 _current = 0;
89 }
90
91 const int Array::Iterator::Next()
92 {
93 if( _current < _length )
94 return _array->Get( _current++ );
95
96 return NULL;
97 }
98
99 void Array::Iterator::Reset()
100 {
101 _current = 0;
102 }
103
104 }
105
2 *Author:Lupin
3 * Description:A simple array with iterator for int
4 */
5 #include "Array.h"
6
7 namespace DataStruct
8 {
9
10 Array::Array()
11 {
12 _maxSize = 100;
13 _list = new int[ 100 ];
14 }
15 Array::Array( int maxSize )
16 {
17 _maxSize = maxSize;
18 _list = new int[ _maxSize ];
19 }
20
21 Array::~Array()
22 {
23 delete[] _list;
24 }
25
26 int Array::Length() const
27 {
28 return _length;
29 }
30
31 int Array::MaxSize() const
32 {
33 return _maxSize;
34 }
35
36 bool Array::IsFull() const
37 {
38 return ( _length == _maxSize );
39 }
40
41 bool Array::IsEmpty() const
42 {
43 return ( _length == 0 );
44 }
45
46 void Array::Append( int elem )
47 {
48 if( IsFull() )
49 throw range_error( "The array is full" );
50
51 _list[ _length++ ] = elem;
52 }
53
54 int Array::Get( int pos ) const
55 {
56 if( pos < 0 || pos >= _length )
57 throw out_of_range( "You can't get a element isn's exsits" );
58
59 return _list[ pos ];
60 }
61
62 void Array::Remove( int pos )
63 {
64 if( pos < 0 || pos > _length )
65 throw out_of_range( "No element at the position" );
66
67 _length--;
68 int i = pos;
69 while( i < _length )
70 _list[ i++ ] = _list[ i + 1 ];
71 }
72
73 int Array::Find( int elem )
74 {
75 int i = 0;
76 while( i < _length )
77 {
78 if( _list[ i++ ] == elem )
79 return i-1;
80 }
81 return -1;
82 }
83
84 Array::Iterator::Iterator( Array* array, int length )
85 {
86 _array = array;
87 _length = length;
88 _current = 0;
89 }
90
91 const int Array::Iterator::Next()
92 {
93 if( _current < _length )
94 return _array->Get( _current++ );
95
96 return NULL;
97 }
98
99 void Array::Iterator::Reset()
100 {
101 _current = 0;
102 }
103
104 }
105