# HG changeset patch # User Eris Caffee # Date 1432875226 18000 # Node ID 310618f5e32a9bfe777ac4bd0642a2249c65d02b # Parent ca59e5f5b29e29a77bc0d002bc607b631856b9e3 Fleshed out ResizingArrayStack with an iterator. diff -r ca59e5f5b29e -r 310618f5e32a algs4-c++/src/ResizingArrayStack.cpp --- a/algs4-c++/src/ResizingArrayStack.cpp Tue May 26 18:18:16 2015 -0500 +++ b/algs4-c++/src/ResizingArrayStack.cpp Thu May 28 23:53:46 2015 -0500 @@ -1,5 +1,5 @@ -// g++ -std=c++0x ResizingArrayStack.cpp -// g++ -std=c++11 ResizingArrayStack.cpp +// g++ -D RESIZINGARRAYSTACK_MAIN -std=c++0x ResizingArrayStack.cpp +// g++ -D RESIZINGARRAYSTACK_MAIN -std=c++11 ResizingArrayStack.cpp #include "ResizingArrayStack.hpp" @@ -9,28 +9,31 @@ #include -template ResizingArrayStack::ResizingArrayStack( void ) : +template +ResizingArrayStack::ResizingArrayStack( void ) : N (0), max(1) { data = new T[1]; } -template ResizingArrayStack::~ResizingArrayStack( void ) { +template +ResizingArrayStack::~ResizingArrayStack( void ) { assert( N == 0 ); // Catch the potential memory leak. if ( data ) - delete data; + delete[] data; } -template void ResizingArrayStack::push( T &item ) { +template +void ResizingArrayStack::push( T &item ) { if ( max == N ) resize( 2 * max ); data[N++] = item; } -template T ResizingArrayStack::pop( void ) { +template +T ResizingArrayStack::pop( void ) { assert( N > 0 ); T item = data[N-1]; - data[N-1] = NULL; N--; if ( N <= max/4 ) @@ -39,24 +42,77 @@ return item; } -template bool ResizingArrayStack::is_empty( void ) { +template +bool ResizingArrayStack::is_empty( void ) { return N == 0; } -template size_t ResizingArrayStack::size( void ) { +template +size_t ResizingArrayStack::size( void ) { return N; } -template void ResizingArrayStack::resize( size_t new_max ) { +template +void ResizingArrayStack::resize( size_t new_max ) { T *new_data = new T[new_max]; for ( size_t i = 0; i < N; ++i ) new_data[i] = data[i]; T *old_data = data; data = new_data; - delete old_data; + delete[] old_data; max = new_max; } +template +typename ResizingArrayStack::iterator ResizingArrayStack::begin( void ) { + return ResizingArrayStack::iterator( data+N-1, data-1 ); + } + +template +typename ResizingArrayStack::iterator ResizingArrayStack::end( void ) { + return ResizingArrayStack::iterator( data-1, data-1 ); + } + + +//////////////////////////////////////////////////////////////////////////////// + +template +ResizingArrayStack::iterator::iterator( T *c, T *e ) : + curr (c), + end (e) + { + } + +template +typename ResizingArrayStack::iterator & ResizingArrayStack::iterator::operator++() { + if ( this->curr > this->end ) { + this->curr -= 1; + } + return *this; + } + +template +typename ResizingArrayStack::iterator ResizingArrayStack::iterator::operator++( int ) { + auto t = ResizingArrayStack::iterator( *this ); + if ( this->curr > this->end ) { + this->curr -= 1; + } + return t; + } + +template +T ResizingArrayStack::iterator::operator*() { + return *(this->curr); + } + +template +bool ResizingArrayStack::iterator::operator!=( typename ResizingArrayStack::iterator ) { + return this->curr != this->end; + } + + +//////////////////////////////////////////////////////////////////////////////// + #ifdef RESIZINGARRAYSTACK_MAIN #include @@ -74,10 +130,19 @@ std::cout << "Stack has " << stack.size() << " entries." << std::endl; + for ( auto iter = stack.begin(); iter != stack.end(); ++iter ) { + std::cout << *iter << std::endl; + } + + std::cout << "Popping entries..." << std::endl; + while ( ! stack.is_empty() ) { i = stack.pop(); std::cout << i << std::endl; } + + std::cout << "Stack has " << stack.size() << " entries." << std::endl; + } #endif diff -r ca59e5f5b29e -r 310618f5e32a algs4-c++/src/ResizingArrayStack.hpp --- a/algs4-c++/src/ResizingArrayStack.hpp Tue May 26 18:18:16 2015 -0500 +++ b/algs4-c++/src/ResizingArrayStack.hpp Thu May 28 23:53:46 2015 -0500 @@ -17,10 +17,28 @@ bool is_empty( void ); size_t size( void ); - // class iterator; - // friend class iterator; - // class iterator { - // }; + class iterator; + friend class iterator; + class iterator { + + public: + + iterator( T *c, T *e ); + + iterator& operator++(); + iterator operator++(int); + + T operator*(); + bool operator!=( typename ResizingArrayStack::iterator ); + + private: + + T *curr; + T *end; + }; + + iterator begin( void ); + iterator end( void ); private: