Question: Modify the Stack class given in the book in order for it to store double numbers (instead of void* pointers). Add a destructor that will

Modify the Stack class given in the book in order for it to store double numbers (instead of void* pointers). Add a destructor that will delete all the stack by making calls to pop(), and for each element destroyed the element will be printed to the output. Now add two constructors: the default one that will create an empty stack, and another one that will receive an integer n and will build a stack of n elements, such that the first element is 1.0, and the next ones are incremented of 0.1. Upload your Stack class. It will be then tested for correctness with stacks.cpp.

Stack.h:

//: C05:Stack2.h

// Nested structs via linked list

#ifndef STACK2_H #define STACK2_H class Stack { struct Link {

void* data; Link* next; void initialize(void* dat, Link* nxt); }* head; public: void initialize(); void push(void* dat); void* peek(); void* pop(); void cleanup(); };

#endif // STACK2_H ///:~

Stack.cpp:

#include  #include "Stack.h" using namespace std; int main(int argc, const char * argv[]) { { Stack s1; for ( int i=1; i<5; i++ ) s1.push ( double(i)/2.0 ); } cout< 

Sample Output:

2 1.5 1 0.5 1.3 1.2 1.1 1 end.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!