Question
C++ Implement a template class Stack as defined by the following skeleton: template class Stack { private: NodeType * topPtr; // It points to a
C++
Implement a template class Stack as defined by the following skeleton:
template
class Stack
{
private:
NodeType
public:
Stack( ); // default constructor: Stack is created and empty
Stack(const Stack
// deep copy
void MakeEmpty(); // Stack is made empty; you should deallocate all the
// the nodes of the linked list
bool IsEmpty( ); // test if the stack is empty
bool IsFull( ); // test if the stack is full; assume MAXITEM=100
int length( ); // return the number of elements in the stack
void Print( ); // print the value of all elements in the stack in the sequence
// from the top to bottom
void Push(ItemType x); // insert x onto the stack
void Pop(ItemType &x); // delete the top element from the stack
// Precondition: the stack is not empty
~Stack(); // Destructor: memory for nodes needs to be deallocated
};
template
struct NodeType
{
ItemType info;
NodeType* next;
};
In you main( ) routine, you need to test your class in the following cases:
Stack
int x;
IntStack.Pop(x);
IntStack.Push(11);
IntStack.Push(22);
cout << "int length 1 = " << IntStack.length() << endl;
IntStack.Pop(x);
IntStack.Push(33);
cout << "int length 2 = " << IntStack.length() << endl;
cout << The int stack contains: << endl;
IntStack.Print();
IntStack.Push(44);
IntStack.Push(55);
IntStack.Push(66);
if(IntStack.IsFull() == false)
cout << The int stack is not full ! << endl;
else
cout << The int stack is full ! << endl;
Stack
cout << The int stack2 contains: << endl;
IntStack2.Print();
IntStack2.MakeEmpty();
cout << The int stack3 contains: << endl;
IntStack2.Print();
Stack
float y;
FloatStack.Pop(y);
FloatStack.Push(7.1);
cout << "float length 1 = " << FloatStack.length() << endl;
FloatStack.Push(2.3);
FloatStack.Push(3.1);
cout << "float length 2 = " << FloatStack.length() << endl;
FloatStack.Pop(y);
cout << The float stack contains: << endl;
FloatStack.Print();
Stack
cout << The float stack 2 contains: << endl;
FloatStack2.Print();
FloatStack.MakeEmpty();
cout << The float stack 3 contains: << endl;
FloatStack2.Print();
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started