diff --git a/Stack/Stack.hh b/Stack/Stack.hh index 4cc7467..7a9709f 100644 --- a/Stack/Stack.hh +++ b/Stack/Stack.hh @@ -9,6 +9,26 @@ class Stack { public: Stack() { init( DELTA ); } Stack( int in_size ) { init(in_size); } + + Stack(const Stack& other) { + // Use init to set all vars naturally + init( other.bufsize ) ; + + if ( s == other.s ) + { + std::cout << "sAmE" << std::endl; + } + + // Then update the count as this is dependant on the copied Stack + count = other.count ; + + // Copy elements + for ( int i=0 ; i < bufsize ; i++ ) { + s[i] = other.s[i] ; + } + + } + ~Stack() { delete[] s; } diff --git a/ex3.3/Stack.cc b/ex3.3/Stack.cc new file mode 100644 index 0000000..71dda3f --- /dev/null +++ b/ex3.3/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.3/Stack.hh b/ex3.3/Stack.hh new file mode 100644 index 0000000..fb1e08b --- /dev/null +++ b/ex3.3/Stack.hh @@ -0,0 +1,54 @@ +//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(const Stack& other) { + // Use init to set all vars naturally + init( other.bufsize ) ; + + if ( s == other.s ) + { + std::cout << "sAmE" << std::endl; + } + + // Then update the count as this is dependant on the copied Stack + count = other.count ; + + // Copy elements + for ( int i=0 ; i < bufsize ; i++ ) { + s[i] = other.s[i] ; + } + + } + + ~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.3/main.cpp b/ex3.3/main.cpp new file mode 100644 index 0000000..e4bcace --- /dev/null +++ b/ex3.3/main.cpp @@ -0,0 +1,61 @@ +#include +#include "Stack.hh" + +/* + * Copiable Dynamically Sized Stack Implementation + */ + +/* + * Initially this doesn't work because the s is a pointer to dynamic memory. This pointer is copied aswell so sclone operates on the same dynamic memory. + * By allocating new memory and copying the data the stacks can be actually independent. + */ + +using namespace std ; + +int main() { + Stack s ;// initialize Stack + + // Write doubles into Stack + int i ; + for (i=0 ; i<10 ; i++) { + cout << "pushing value " << i*i << " in stack" << endl ; + s.push(i*i) ; + } + + cout << "Clone s to sclone" << endl; + Stack sclone = s; + + cout << "Inspect s's FIFO" << endl; + s.inspect(); + + cout << "Inspect sclone's FIFO" << endl; + sclone.inspect(); + //The Same Data + + + while (!s.empty()) { + double val = s.pop() ; + cout << "popping value " << val << " from stack" << endl ; + } + + cout << "Inspect s's FIFO" << endl; + s.inspect(); + + cout << "Inspect sclone's FIFO" << endl; + sclone.inspect(); + //Not the Same Data after copy constructor + + for (int i = 0; i < 5 ; i++ ) + { + s.push(100*i) ; + } + + cout << "Inspect s's FIFO" << endl; + s.inspect(); + + cout << "Inspect sclone's FIFO" << endl; + sclone.inspect(); + //Not the Same Data after copy constructor + + return 0 ; +}