62 lines
1.2 KiB
C++
62 lines
1.2 KiB
C++
|
#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 ;
|
||
|
}
|