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* topPtr; // It points to a singly-linked list

public:

Stack( ); // default constructor: Stack is created and empty

Stack(const Stack &x); // copy constructor: implicitly called for a

// 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 IntStack;

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 IntStack2(IntStack);

cout << The int stack2 contains: << endl;

IntStack2.Print();

IntStack2.MakeEmpty();

cout << The int stack3 contains: << endl;

IntStack2.Print();

Stack FloatStack;

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 FloatStack2 = FloatStack;

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

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!