Question
You are to write a program that uses the stack class below Stack.h to create a stack of people, cars or any other simple class
You are to write a program that uses the stack class below Stack.h to create a stack of people, cars or any other simple class that you create except for a box class. Modify the code to add whatever you need in main to test the program with your own class.
// Stack.h
//Template Stack class definition.
#ifndef STACK_H
#define STACK_H
#include
#include"ListNode.h"//ListNode class definition
using namespace std;
template
class Stack {
public:
Stack();//constructor
~Stack();//destructor
void push(const T &);
T pop();
bool isEmpty() const;
void print() const;
private:
ListNode
//utility function to allocate new node
ListNode
};//end class Stack
//default constructor
template
Stack
//empty body
}//end Stack constructor
//destructor
template
Stack
if( !isEmpty() ) { //List is not empty
cout<<"Destroying nodes ... ";
ListNode
ListNode
while(currentPtr != 0) { //delete remaining nodes
tempPtr = currentPtr;
cout<
currentPtr = currentPtr->nextPtr;
delete tempPtr;
}//end while
}//end if
cout<<"All nodes destroyed ";
}//end List destructor
// insert node at front of list: push
template
void Stack
ListNode
if( isEmpty() )//List is empty
firstPtr=newPtr;//new list has only one node
else { //List is not empty
newPtr->nextPtr=firstPtr;//point new node to previous 1st node
firstPtr=newPtr;
} //end else
} //end insertAtFront
//delete node from front of list: pop
template
T Stack
if( isEmpty() )//Stack isempty
return NULL;//pop unsuccessful
else {
ListNode
firstPtr=firstPtr->nextPtr;//point to previous 2nd node
T value=tempPtr->data;//return data being removed
delete tempPtr;//reclaim p revious front node
return value;//delete successful
}//end else
}//end function pop
// isEmpty?
template
bool Stack
return firstPtr==NULL;
}//end function isEmpty
//return pointer to newly allocated node
template
ListNode
return new ListNode
}//end function getNewNode
//displaycontentsofList
template
void Stack
if( isEmpty() ) { //List isempty
cout<<"The list is empty ";
}//end if
else {
ListNode
cout<<"The list is: ";
while(currentPtr !=NULL) { //get element data
cout<
currentPtr=currentPtr->nextPtr;
}//endwhile
cout<<" ";
}
}//end function print
#endif
// ListNode.h
//Template ListNode class definition.
#ifndef LISTNODE_H
#define LISTNODE_H
template
template
class ListNode
{
friend class Stack
public:
ListNode(const T &); //constructor
T getData()const; //return data in node
T data; //data
ListNode
};//end class ListNode
//constructor
template
ListNode
: data( info ), nextPtr(NULL)
{
//empty body
}//end ListNode constructor
//return copy of data in node
template
T ListNode
{
return data;
}//end function getData
#endif
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