1
0
Fork 0

Ex3.3 feedback: The Stack's bufsize was not updated on Stack::grow() so the stack would not actually grow

This commit is contained in:
Eric Teunis de Boone 2020-06-30 11:18:10 +02:00
parent c49a9f3e5b
commit 415032e0ef
2 changed files with 11 additions and 6 deletions

View file

@ -4,8 +4,6 @@
void Stack::push(double c) { void Stack::push(double c) {
if (full()) { if (full()) {
std::cout << "Stack::push() Error: stack is full" << std::endl ;
grow( DELTA ); grow( DELTA );
} }
s[count++] = c ; s[count++] = c ;
@ -34,7 +32,10 @@ void Stack::init( int in_size ) {
} }
void Stack::grow( int delta ) { void Stack::grow( int delta ) {
std::cout << "Growing by " << delta << std::endl; std::cout << "Growing by " << delta << std::endl;
double* newbuf = new double[ bufsize + delta ];
int newbufsize = bufsize + delta;
double* newbuf = new double[ newbufsize ];
// Copy elements // Copy elements
for ( int i=0; i < count ; i++ ) for ( int i=0; i < count ; i++ )
@ -47,4 +48,8 @@ void Stack::grow( int delta ) {
// Assign pointer // Assign pointer
s = newbuf; s = newbuf;
// Update bufsize
bufsize = newbufsize;
} }

View file

@ -7,7 +7,7 @@
using namespace std ; using namespace std ;
int main() { int main() {
Stack s(10) ;// initialize Stack Stack s(5) ;// initialize Stack
// Write doubles into Stack // Write doubles into Stack
int i ; int i ;
@ -17,7 +17,7 @@ int main() {
} }
// Count doubles in fifo // Count doubles in fifo
cout << s.nitems() << " value in stack" << endl ; cout << s.nitems() << " values in Stack" << endl ;
cout << "Inspect the FIFO" << endl; cout << "Inspect the FIFO" << endl;
s.inspect(); s.inspect();
@ -26,7 +26,7 @@ int main() {
// Read doubles back from fifo // Read doubles back from fifo
while (!s.empty()) { while (!s.empty()) {
double val = s.pop() ; double val = s.pop() ;
cout << "popping value " << val << " from stack" << endl ; cout << "popping value " << val << " from Stack" << endl ;
} }
cout << "Inspect the FIFO" << endl; cout << "Inspect the FIFO" << endl;