51 lines
858 B
C++
51 lines
858 B
C++
|
//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;
|
||
|
}
|