Question: #include #include using namespace std; template struct NodeType { ItemType info; NodeType *next; }; template class Stack { private: NodeType *topPtr; // It points to
#include
#include
using namespace std;
template
struct NodeType
{
ItemType info;
NodeType
};
template
class Stack
{
private:
NodeType
public:
Stack() { topPtr = NULL; } // default constructor: Stack is created and empty
Stack(const Stack
NodeType
if (anotherStack.topPtr == NULL)
topPtr = NULL;
else // allocate memory for first node
{
topPtr = new NodeType
topPtr->info = anotherStack.topPtr->info;
ptr1 = anotherStack.topPtr->next;
ptr2 = topPtr;
while (ptr1 != NULL)// deep copy other nodes
{
ptr2->next = new NodeType
ptr2 = ptr2->next;
ptr2->info = ptr1->info;
ptr1 = ptr1->next;
}
ptr2->next = NULL;
}
}
void MakeEmpty() // Stack is made empty; you should deallocate all the the nodes of the linked list
{
NodeType
while (topPtr != NULL) //makes sure stack isn't already empty
{
temp = topPtr;
topPtr = topPtr->next; //next node
delete temp;
}
}
bool IsEmpty() {return (topPtr == NULL); } // test if the stack is empty
bool IsFull() // test if the stack is full; assume MAXITEM=5
{
int MAXITEM = 5, sum = 0;
while (topPtr != NULL) // count all the values, adds sum by one for every loop
{
sum++;
topPtr = topPtr->next;
}
return (sum >= MAXITEM);
}
int length() // return the number of elements in the stack
{
int sum = 0;
NodeType
while (temp != NULL)
{
sum = sum + 1;
temp = temp->next;
}
return sum;
}
void Print() // print the value of all elements in the stack in the sequence, from the top to bottom
{
NodeType
p = topPtr;
while (p != NULL)
{
cout << p->info << endl;
p = p->next;
}
}
void Push(ItemType x) // insert x onto the stack
{
if (IsFull())
cout << "Is Full" << endl;
else
{
NodeType
newNode = new NodeType
newNode->info = x; //stores x into the information for that node
newNode->next = topPtr;
topPtr = newNode; //set the top pointer to the new node
}
}
void Pop(ItemType &x) // delete the top element from the stack, Precondition: the stack is not empty
{
NodeType
if (topPtr != NULL)
{
temp = topPtr;
topPtr = topPtr->next;
delete temp;
}
else
cout << "Stack is empty" << endl;
}
~Stack() // Destructor: memory for nodes needs to be deallocated
{ NodeType
while (temp != NULL)
{
NodeType
delete temp;
temp = next;
}
}
};
int main() {
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();
}
----This is a stack using a linked list but it isnt giving the correct results-----
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
