1
0
Fork 0

Ex3.3: Copiable Dynamical Size Stack

This commit is contained in:
Eric Teunis de Boone 2019-12-12 16:56:16 +01:00
parent fa61fdaf21
commit 2ecd14cf94
4 changed files with 185 additions and 0 deletions

View File

@ -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;
}

50
ex3.3/Stack.cc Normal file
View File

@ -0,0 +1,50 @@
//Stack.cc
#include <iostream>
#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;
}

54
ex3.3/Stack.hh Normal file
View File

@ -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

61
ex3.3/main.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <iostream>
#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 ;
}