Description:
Implement a LIFO, Last In First Out, data structure,
also known as a "stack."
Create a class Stack to implement a stack
and use Push() and Pop() as its primary
methods adding and removing items from it.
Here is the prototype for the class Stack:
#define SIZE 10
class Stack
{
private:
double data[SIZE];
int top_of_stack; // keeps track of recent pushes
public:
void Init(); // or we could use a constructor
void Push(double item_to_push);
double Pop();
};
The client creates an empty stack of
doubles with:
Stack stk;
stk.Init();
Then, whenever the client wants to "push" something onto the stack, it calls
stk.Push( some_number );
and when the client wants to remove an item
and look at it, it calls:
cout << stk.Pop();
If the client tries to Pop() from a stack with nothing on it, then an error should be generated. If it tries
to Push() so that more than SIZE (or whatever the size of data is) items would end up on the stack, it also
generates an error.
Design a test main that creates at least two separate stack objects and pushes and pops items from each,
printing as necessary.
Deliverables: Here is an example of an
acceptable main():
int main()
{
Stack s1, s2;
int k;
// Initialize stacks ------------
s1.Init();
s2.Init();
// Test the Stack -----
cout << s1.Pop();
s1.Push( 2.34 );
s1.Push( 3.56 );
s1.Push( 7.89 ); // just for fun
s2.Push( -1.003 );
s1.Push( 5000700 );
s1.Push( 123456789.123456789 );
s2.Push( 1 );
s2.Push( 2 );
s1.Push( 3 );
s2.Push( 4 );
s1.Push( 5 );
s2.Push( 6 );
cout << "\n------------ First Stack ------------\n";
for (k=0; k<8; k++)
cout << s1.Pop() << endl;
cout << "\n------------ Second Stack ------------\n";
for (k=0; k<8; k++)
cout << s2.Pop() << endl;
return 0;
}
and associated sample run:
/* ------------------- SAMPLE RUN ---------------
Stack Empty
0
------------ First Stack ------------
5
3
1.23457e+08
5.0007e+06
7.89
3.56
2.34
Stack Empty
0
------------ Second Stack ------------
6
4
2
1
-1.003
Stack Empty
0
Stack Empty
0
Stack Empty
0
---------------------------------------------- */
Use this to test that your class works, but submit a different
main and run
Complete and fully-functional working program(s) in executable form as well as complete source code of all work done. Complete copyrights to all work purchased.
Remember that contacting the other party outside of the site (by email, phone, etc.) on all business projects < $500 (before the buyer's money is escrowed) is a violation of both the software buyer and seller agreements.
We monitor all site activity for such violations and can instantly expel transgressers on the spot, so we thank you in advance for your cooperation.
If you notice a violation please help out the site and report it. Thanks for your help.
|