Question: . Write your Stack implementation inside of your Stack.cpp file that goes with header file below(Stack.h) .Inside each of the Stack functions, you should call

. Write your Stack implementation inside of your Stack.cpp file that goes with header file below(Stack.h)

.Inside each of the Stack functions, you should call the appropriate List functions to update stack.

.For example:

void Stack::push(int data)

{

stack.insert_head(data);

}

.You should not be re-writing any of your List functions here, just calling them!

Stack constructor and destructor

.The body of these two functions should simply be left blank. Why?

.The constructor body should be left blank, because there are no new fields to initialize in the constructor

.The private List name stack is already initialized inside the Stack.h file, with this line of code:

List stack; //calls the List default constructor

.The body of the destructor also should be left blank. For example, you would NOT wish to write the following:

Stack::~Stack() {

stack.~List(); //<--don't do this!!! //you never need to explicitly call a destructor in this class!

}

.Instead, leave the body of this function empty.

.The reason?

.Only in the rarest of circumstances would you need to call a destructor explicitly.

.Instead, the destructor is called automatically once an object goes out of scope.

.For our Stack, once its private List named stack goes out of scope, the List destructor will be called automatically.

Stack Copy Constructor

.Here, you will want to call the copy constructor of the List class:

Stack::Stack(const Stack &S):stack(S.stack){}

.How do you test this copy constructor?

//make a new Stack object with the default constructor

Stack s1;

//add data to s1

s1.push(20); //can add more data below

...

//make another new Stack object named s2 by calling the copy constructor

Stack s2(s1); // passing in s1 to make a copy of

//Now, print s2 to make sure it contains the same data as s1

s2.print();

Don't forget to test all of your functions inside of your main function in a file called StackTest.cpp

Also, don't forget to print out the results of your tests in your StackTest.cpp in your comment at the bottom of main.

HEADER FILE (Stack.h). Note that you should not change this file in any way.

#ifndef Stack_H_ #define Stack_H_ #include "List.h" #include #include

using namespace std; class Stack { public: /**constructors and destructors*/

Stack();

//initializes an empty stack //postcondition: an empty stack Stack(const Stack &S); //initializes this stack to have same elements as S //postcondition: a copy of stack

~Stack();

//frees memory allocated to the stack //postcondition: memory for stack has been freed /**manipulation procedures*/ void pop(); //removes an element from the top of the stack //precondition: the stack isn't empty //postcondition: an element has been removed from the top of the stack void push(int data); //adds an element to the top of the stack //postcondition: an element added to the top of the stack /**accessors*/

bool operator==(const Stack &S); //returns whether this stack is equal to another stack S int get_top(); //returns the element at the top of the stack //precondition: the stack is not empty int get_size(); //returns the size of the stack int linear_search(int value); //returns the index at which value is located

//pre: Stack is not empty

/**additional operations*/

void print(); //prints the elements in the stack in a programmer-specified format to stdout void printReverse(); //prints a stack in reverse order

private: List stack; }; #endif /* Stack_H_ */

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!