Please fill in the methods under the class templates to test the code in the int
ID: 3810680 • Letter: P
Question
Please fill in the methods under the class templates to test the code in the int main() function. You may add any libraries needed. This is a C++ code done on visual studio. If you are able to provide screenshots, it would be a huge help! Thank you in advance. PLEASE DO NOT COPY ANY CODE ALREADY ON THIS WEBSITE AS THEY ARE WRONG!. The code is as follows.
#include <iostream>
#include <iomanip>
using namespace std;
#define MAXITEM = 5;
template <class ItemType>
class Stack
{
private:
NodeType <ItemType>* topPtr; // points to singly-linked list
public:
Stack(); // default constructor: Stack is created and empty
Stack(const Stack <ItemType> &x); // copy constructor implicitly called for a deep copy
void MakeEmpty(); // Stack is made empty; you should deallocate all the nodes of the linked list
{
}
bool IsEmpty(); // Test if the stack is empty
bool IsFull(); // Test if stack is full; assume MAXITEM = 5
int length(); // return the number of elements in the stack
void Print(); // print the value of all elements in the stackin 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(); // Deconstructor: memory for nodes deallocated
};
template <class ItemType>
struct NodeType
{
ItemType info;
NodeType* next;
};
int main()
{
Stack <int> IntStack;
int x;
IntStack.Pop(x); //Should throw error message that stack is empty
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 <int> IntStack2(IntStack);
cout << "The int stack2 contains: " << endl;
IntStack2.Print();
IntStack2.MakeEmpty();
cout << "The int stack2 contains: " << endl;
IntStack2.Print();
Stack <float> FloatStack;
float y;
FloatStack.Pop(y); //Should throw error message that stack is empty
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 <float> FloatStack2 = FloatStack;
cout << "The float stack 2 contains: " << endl;
FloatStack2.Print();
FloatStack.MakeEmpty();
cout << "The float stack 3 contains: " << endl;
FloatStack2.Print();
return 0;
}
Explanation / Answer
I also made few more changes other than the implementation because of syntax error in main function and in macro definition. Pleae rate this if you find it useful. You can find the output below.
PROGRAM CODE:
#include <iostream>
#include <iomanip>
using namespace std;
#define MAXITEM 5 // changed this statement, there was syntax error
template <class ItemType>
struct NodeType
{
ItemType info;
NodeType* next;
};
template <class ItemType>
class Stack
{
private:
NodeType <ItemType>* topPtr; // points to singly-linked list
int size;
public:
Stack()
{
topPtr = NULL;
size = 0;
}
Stack(const Stack <ItemType> &x) // copy constructor implicitly called for a deep copy
{
topPtr = x.topPtr;
size = x.size;
}
void MakeEmpty() // Stack is made empty; you should deallocate all the nodes of the linked list
{
topPtr = NULL;
size = 0;
}
bool IsEmpty() // Test if the stack is empty
{
return size==0;
}
bool IsFull() // Test if stack is full; assume MAXITEM = 5
{
return size == MAXITEM;
}
int length() // return the number of elements in the stack
{
return size;
}
void Print() // print the value of all elements in the stackin the sequence from the top to bottom
{
NodeType <ItemType>* temp = topPtr;
cout<<"[ ";
while(temp != NULL)
{
cout<<temp->info<<" ";
temp = temp->next;
}
cout<<"]"<<endl;
}
void Push(ItemType x) // Insert x onto the stack
{
NodeType <ItemType>* newNode = new NodeType<ItemType>;
newNode->info = x;
newNode->next = NULL;
if(size ==0)
{
topPtr = newNode;
}
else if(size<MAXITEM)
{
newNode->next = topPtr;
topPtr = newNode;
}
else
cout<<"Stack is full ";
size++;
}
void Pop(ItemType &x) // delete the top element from the stack. Precondition: The stack is not empty
{
if(size==0)
{
cout<<"Stack is empty ";
return;
}
topPtr = topPtr->next;
size--;
}
~Stack() // Deconstructor: memory for nodes deallocated
{
delete topPtr;
}
};
int main()
{
Stack <int> IntStack;
int x;
IntStack.Pop(x); //Should throw error message that stack is empty
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 <int> IntStack2(IntStack);
cout << "The int stack2 contains: " << endl;
IntStack2.Print();
IntStack2.MakeEmpty();
cout << "The int stack2 contains: " << endl;
IntStack2.Print();
Stack <float> FloatStack;
float y;
FloatStack.Pop(y); //Should throw error message that stack is empty
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 <float> FloatStack2 = FloatStack;
cout << "The float stack 2 contains: " << endl;
FloatStack2.Print();
FloatStack.MakeEmpty();
cout << "The float stack 3 contains: " << endl;
FloatStack.Print();
return 0;
}
OUTPUT: