Problem 3 a. Add the following operation to the class stackType: void reverseSta
ID: 667866 • Letter: P
Question
Problem 3
a. Add the following operation to the class stackType:
void reverseStack(stackType<Type> &otherStack);
This operation copies the elements of a stack in reverse order onto
another stack.
Consider the following statements:
stackType<int> stack1;
stackType<int> stack2;
The statement
stack1.reverseStack(stack2);
copies the elements of stack1 onto stack2 in reverse order. That is,
the top element of stack1 is the bottom element of stack2, and so on.
The old contents of stack2 are destroyed and stack1 is unchanged.
b. Write the definition of the function template to implement the operation
reverseStack.
This is what I have for part 3
#define H_StackType
#define H_StackType
#include <iostream>
#include<cassert>
using namespace std;
template <class Type>
class stackType
{
private:
int maxStackSize;
int stackTop;
Type *list;
public:
void initializeStack();
bool isFullStack() const;
bool isEmptyStack() const;
void push(const Type&);
void pop();
Type top() const;
stackType(int = 20);
~stackType();
void reverseStack(stackType<Type> &);
};
template <class Type>
void stackType<Type>::initializeStack()
{
stackTop = 0;
}
template <class Type>
bool stackType<Type>::isFullStack() const
{
return (stackTop == maxStackSize);
}
template <class Type>
bool stackType<Type>::isEmptyStack() const
{
return (stackTop == 0);
}
template <class Type>
void stackType<Type>::push(const Type& newItem)
{
if (!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
cout << " Can not add to a full stack";
}
template <class Type>
void stackType<Type>::pop()
{
if (!isEmptyStack())
stackTop--;
else
cout << " Can not remove from an empty stack";
}
template <class Type>
Type stackType<Type>::top() const
{
assert(stackTop != 0);
return list[stackTop - 1];
}
template <class Type>
stackType<Type>::stackType(int stackSize)
{
if (stackSize <= 0)
{
cout << "Invalid size";
stackSize = 10;
}
else
maxStackSize = stackSize;
stackTop = 0;
list = new Type[maxStackSize];
}
template <class Type>
stackType<Type>::~stackType()
{
delete[] list;
}
template <class Type>
void stackType<Type>::reverseStack(stackType<Type>& right)
{
right.initializeStack();
for (int i = stackTop; i > 0; i--)
right.push(this->list[i - 1]);
}
int main()
{
stackType<int> stack1(12);
stackType<int> stack2(15);
cout << " Inserting elements 5, 10, 15 ... to both the stacks.";
for (int i = 5; i < 50; i += 5)
stack1.push(i);
stack1.reverseStack(stack2);
cout << " The elements in the first stack are: ";
while (!stack1.isEmptyStack())
{
cout << stack1.top() << " ";
stack1.pop();
}
cout << " The elements in the second stack are: ";
while (!stack2.isEmptyStack())
{
cout << stack2.top() << " ";
stack2.pop();
}
system("pause");
return 0;
}
I need the solution for this problem. Must not be listed on the general internet.
4. Repeat Exercises 3a and 3b for the class linkedStackType.
Explanation / Answer
reverseStack.cpp
4.
linkedStackType.h
stackADT.h
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.