From fa61fdaf21e149cfc0fe3daab1caa2baa2c646ff Mon Sep 17 00:00:00 2001 From: Eric Teunis de Boone Date: Thu, 12 Dec 2019 15:56:38 +0100 Subject: [PATCH] Ex 3.2: Dynamic Stack --- Stack/Stack.cc | 26 +++++++++++++++++++++++++- Stack/Stack.hh | 19 +++++++++++++------ ex3.2/Stack.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ ex3.2/Stack.hh | 34 ++++++++++++++++++++++++++++++++++ ex3.2/main.cpp | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 7 deletions(-) create mode 100644 ex3.2/Stack.cc create mode 100644 ex3.2/Stack.hh create mode 100644 ex3.2/main.cpp diff --git a/Stack/Stack.cc b/Stack/Stack.cc index a87d8b2..697da35 100644 --- a/Stack/Stack.cc +++ b/Stack/Stack.cc @@ -5,7 +5,8 @@ void Stack::push(double c) { if (full()) { std::cout << "Stack::push() Error: stack is full" << std::endl ; - return ; + + grow( DELTA ); } s[count++] = c ; } @@ -24,3 +25,26 @@ void Stack::inspect() { std::cout << i << ": " << s[i] << std::endl; } } + +void Stack::init( int in_size ) { + bufsize = in_size ; + count = 0 ; + + s = new double[in_size]; +} +void Stack::grow( int delta ) { + std::cout << "Growing by " << delta << std::endl; + double* newbuf = new double[ bufsize + delta ]; + + // Copy elements + for ( int i=0; i < count ; i++ ) + { + newbuf[i] = s[i]; + } + + // Delete old Stack + delete[] s; + + // Assign pointer + s = newbuf; +} diff --git a/Stack/Stack.hh b/Stack/Stack.hh index 1cae03e..4cc7467 100644 --- a/Stack/Stack.hh +++ b/Stack/Stack.hh @@ -3,25 +3,32 @@ #ifndef STACK_H #define STACK_H -const int LEN = 80 ; // default stack length +const int DELTA = 5 ; // default growing size class Stack { // Interface public: - Stack() { init(); } - ~Stack() {} + Stack() { init( DELTA ); } + Stack( int in_size ) { init(in_size); } + ~Stack() { + delete[] s; + } int nitems() { return count ; } - bool full() { return (count==LEN) ; } + bool full() { return (count==bufsize) ; } bool empty() { return (count==0) ; } void push( double c ); double pop(); void inspect(); + void grow( int delta ); + // Implementation private: - void init() { count = 0 ; } - double s[LEN] ; + void init( int in_size ); + + double* s ; int count ; + int bufsize ; }; #endif diff --git a/ex3.2/Stack.cc b/ex3.2/Stack.cc new file mode 100644 index 0000000..71dda3f --- /dev/null +++ b/ex3.2/Stack.cc @@ -0,0 +1,50 @@ +//Stack.cc +#include +#include "Stack.hh" + +void Stack::push(double c) { + if (full()) { + std::cout << "Stack::push() Error: stack is full" << std::endl ; + + grow( DELTA ); + } + s[count++] = c ; +} + +double Stack::pop() { + if (empty()) { + std::cout << "Stack::pop() Error: stack is empty" << std::endl ; + return 0 ; + } + return s[--count] ; +} + +void Stack::inspect() { + for ( int i = nitems() - 1; i >= 0; i-- ) + { + std::cout << i << ": " << s[i] << std::endl; + } +} + +void Stack::init( int in_size ) { + bufsize = in_size ; + count = 0 ; + + s = new double[in_size]; +} +void Stack::grow( int delta ) { + std::cout << "Growing by " << delta << std::endl; + double* newbuf = new double[ bufsize + delta ]; + + // Copy elements + for ( int i=0; i < count ; i++ ) + { + newbuf[i] = s[i]; + } + + // Delete old Stack + delete[] s; + + // Assign pointer + s = newbuf; +} diff --git a/ex3.2/Stack.hh b/ex3.2/Stack.hh new file mode 100644 index 0000000..b8f8a74 --- /dev/null +++ b/ex3.2/Stack.hh @@ -0,0 +1,34 @@ +//Stack.h + +#ifndef STACK_H +#define STACK_H + +const int DELTA = 5 ; // default growing size +class Stack { + // Interface + public: + Stack() { init( DELTA ); } + Stack( int in_size ) { init(in_size); } + ~Stack() { + delete[] s; + } + int nitems() { return count ; } + bool full() { return (count==bufsize) ; } + bool empty() { return (count==0) ; } + + void push( double c ); + double pop(); + void inspect(); + + void grow( int delta ); + + // Implementation + private: + void init( int in_size ); + + double* s ; + int count ; + int bufsize ; + +}; +#endif diff --git a/ex3.2/main.cpp b/ex3.2/main.cpp new file mode 100644 index 0000000..0f2840c --- /dev/null +++ b/ex3.2/main.cpp @@ -0,0 +1,35 @@ +#include +#include "Stack.hh" + +/* + * Dynamically Sized Stack Implementation + */ +using namespace std ; + +int main() { + Stack s(10) ;// initialize Stack + + // Write doubles into Stack + int i ; + for (i=0 ; i<15 ; i++) { + cout << "pushing value " << i*i << " in stack" << endl ; + s.push(i*i) ; + } + + // Count doubles in fifo + cout << s.nitems() << " value in stack" << endl ; + + cout << "Inspect the FIFO" << endl; + s.inspect(); + + + // Read doubles back from fifo + while (!s.empty()) { + double val = s.pop() ; + cout << "popping value " << val << " from stack" << endl ; + } + + cout << "Inspect the FIFO" << endl; + s.inspect(); + return 0 ; +}