Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago