Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 *firstPtr;//pointer to first node

//utility function to allocate new node

ListNode *getNewNode(const T &);

};//end class Stack

//default constructor

template

Stack::Stack():firstPtr(NULL ) {

//empty body

}//end Stack constructor

//destructor

template

Stack::~Stack() {

if( !isEmpty() ) { //List is not empty

cout<<"Destroying nodes ... ";

ListNode *currentPtr=firstPtr;

ListNode *tempPtr;

while(currentPtr != 0) { //delete remaining nodes

tempPtr = currentPtr;

cout<getData()<< ' ';

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::push(const T &value ) {

ListNode *newPtr=getNewNode(value );//new node

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::pop() {

if( isEmpty() )//Stack isempty

return NULL;//pop unsuccessful

else {

ListNode *tempPtr=firstPtr;//hold temp Ptr to delete

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::isEmpty()const {

return firstPtr==NULL;

}//end function isEmpty

//return pointer to newly allocated node

template

ListNode*Stack::getNewNode(const T &value ) {

return new ListNode(value);

}//end function getNewNode

//displaycontentsofList

template

void Stack::print()const {

if( isEmpty() ) { //List isempty

cout<<"The list is empty ";

}//end if

else {

ListNode *currentPtr=firstPtr;

cout<<"The list is: ";

while(currentPtr !=NULL) { //get element data

cout<data<< ' ';

currentPtr=currentPtr->nextPtr;

}//endwhile

cout<<" ";

}

}//end function print

#endif

// ListNode.h

//Template ListNode class definition.

#ifndef LISTNODE_H

#define LISTNODE_H

template class Stack;

template

class ListNode

{

friend class Stack ; //make Stack a friend

public:

ListNode(const T &); //constructor

T getData()const; //return data in node

T data; //data

ListNode *nextPtr; //next node in list

};//end class ListNode

//constructor

template

ListNode ::ListNode(const T &info )

: data( info ), nextPtr(NULL)

{

//empty body

}//end ListNode constructor

//return copy of data in node

template

T ListNode::getData()const

{

return data;

}//end function getData

#endif

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

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

Show the properties and structure of allotropes of carbon.

Answered: 1 week ago