1
0
Fork 0

Ex 3.2: Dynamic Stack

This commit is contained in:
Eric Teunis de Boone 2019-12-12 15:56:38 +01:00
parent 91a78e2d74
commit fa61fdaf21
5 changed files with 157 additions and 7 deletions

View file

@ -5,7 +5,8 @@
void Stack::push(double c) { void Stack::push(double c) {
if (full()) { if (full()) {
std::cout << "Stack::push() Error: stack is full" << std::endl ; std::cout << "Stack::push() Error: stack is full" << std::endl ;
return ;
grow( DELTA );
} }
s[count++] = c ; s[count++] = c ;
} }
@ -24,3 +25,26 @@ void Stack::inspect() {
std::cout << i << ": " << s[i] << std::endl; 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;
}

View file

@ -3,25 +3,32 @@
#ifndef STACK_H #ifndef STACK_H
#define STACK_H #define STACK_H
const int LEN = 80 ; // default stack length const int DELTA = 5 ; // default growing size
class Stack { class Stack {
// Interface // Interface
public: public:
Stack() { init(); } Stack() { init( DELTA ); }
~Stack() {} Stack( int in_size ) { init(in_size); }
~Stack() {
delete[] s;
}
int nitems() { return count ; } int nitems() { return count ; }
bool full() { return (count==LEN) ; } bool full() { return (count==bufsize) ; }
bool empty() { return (count==0) ; } bool empty() { return (count==0) ; }
void push( double c ); void push( double c );
double pop(); double pop();
void inspect(); void inspect();
void grow( int delta );
// Implementation // Implementation
private: private:
void init() { count = 0 ; } void init( int in_size );
double s[LEN] ;
double* s ;
int count ; int count ;
int bufsize ;
}; };
#endif #endif

50
ex3.2/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;
}

34
ex3.2/Stack.hh Normal file
View file

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

35
ex3.2/main.cpp Normal file
View file

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