Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help in writing a C++ program that uses the stack class to create a stack of people, cars or any other simple class

I need help in writing a C++ program that uses the stack class to create a stack of people, cars or any other simple class that I create except for a box class.

First I was to read the first example on templates:

The program below uses one function to swap two integers and a different function to swap two strings.

#include

#include

using namespace std;

void swapint(int& first, int& second) //passed by reference

{ int temp;

temp = first; //temp is a local variable

first = second; //first and second are references

second = temp; // to the variables that were passed

} //swap

void swapString(string& first, string& second) //passed by reference

{ string temp;

temp = first; //temp is a local variable

first = second; //first and second are references

second = temp; // to the variables that were passed

} //swap

void main() {

int x=5,y=2;

swapint(x,y);

cout<

string s1="Sam", s2="Bill";

swapString(s1,s2b);

cout<

system("pause");

} //main

Using this method, we would also need functions to swap two doubles or two chars or two Employees.

Instead, a Function template can be used:

#include

#include

using namespace std;

template

void swapAnything(T& first, T& second) {

T temp;

temp = first; //temp is a local variable

first = second; //first and second are references

second = temp; // to the variables that were passed

}

void main() {

int x=5,y=2;

swapAnything(x,y);

cout<

string s1="Sam", s2="Bill";

swapAnything(s1,s2);

cout<

double a=3.56,b=2.01;

swapAnything(a,b);

cout<

system("pause");

} //main

Second, I was to read this to see how a Stack work:

A stack should have methods Push and Pop. This is just a small start to illustrate the concept. In this there is no stack, just nodes. We implement the idea of pushing in main:

#include

#include

using namespace std;

class IntNode {

public:

int data; // will store information

IntNode *next;

IntNode(); //constructor

};

IntNode::IntNode() {

next=NULL;

} //constructor

void main() {

IntNode *top = NULL;

//make a list of 3 integers

int num,i;

for(i=0;i<3;i++) {

cout<<"Enter an integer: ";

cin>>num;

IntNode *temp; //create a temporary node

temp = (IntNode*)malloc(sizeof(IntNode)); //allocate space for node

temp->data = num; // store data(first field)

temp->next=top; // store the address of the pointer head(second field)

top = temp; // transfer the address of 'temp' to 'top'

} //3 numbers

//look at the list

IntNode *temp = top;

while( temp!=NULL ) {

cout<< temp->data<<" ";// show the data in the stack (linked list)

temp = temp->next; // tranfer the address of 'temp->next' to 'temp'

} //loop until the end, that is when next is null

system("pause");

}//main

If you run this and enter the numbers 8, 4, 3 it will print out 3, 4, 8. Each number is inserted at the front

of the list. When we print the list we start at the front and work our way to the end.

CS226: A Stack Class Part I Prof. J. Joy page 2

In step 2 we have taken the code to add to the front of the list and created a method for the node called

push(); We now use that in main and the result is the same.

#include

#include

using namespace std;

class IntNode {

public:

int data; // will store information

IntNode *next;

IntNode(); //constructor

IntNode* push(int num); //add to ftop of list

};

IntNode::IntNode() {

next=NULL;

} //constructor

IntNode* IntNode::push(int num) {

IntNode *cur;

cur = (IntNode*)malloc(sizeof(IntNode)); //allocate space for node

cur->data = num; // store data

cur->next=this; //next is the current top of the list

return cur;

} //push

void main() {

IntNode *top = NULL;

//make a list of 3 integers

int num,i;

for(i=0;i<3;i++) {

cout<<"Enter an integer: ";

cin>>num;

top=top->push(num);

} //3 numbers

//look at the list

IntNode *temp = top;

while( temp!=NULL ) {

cout<< temp->data<<" ";// show the data in the linked list

temp = temp->next; // transfer the address of 'temp->next' to 'temp'

} //loop until the end, that is when next is null

system("pause");

}//main

Third, I must use this stack template in my project:

Stack Class: pushing and popping nodes

A stack will be a list of nodes that are linked together.

The stack has methods push: to insert a node at the top, and pop: to remove the top node and return its

value.

ListNode.h there is no ListNode.cpp

//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 NODETYPE &); //constructor

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

NODETYPE data; //data

ListNode *nextPtr; //next node in list

};//end class ListNode

//constructor

template

ListNode ::ListNode(const NODETYPE &info )

: data( info ), nextPtr(NULL)

{

//empty body

}//end ListNode constructor

//return copy of data in node

template

NODETYPE ListNode::getData()const

{

return data;

}//end function getData

#endif

Stack Class: pushing and popping nodes Prof. J. Joy 2 of 4

Stack.h there is no Stack.cpp

//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 NODETYPE &);

NODETYPE pop();

bool isEmpty() const;

void print() const;

private:

ListNode*firstPtr;//pointer to first node

//utility function to allocate new node

ListNode*getNewNode(const NODETYPE &);

};//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 NODETYPE &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

Stack Class: pushing and popping nodes Prof. J. Joy 3 of 4

//delete node from front of list: pop

template

NODETYPE 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

NODETYPE 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 NODETYPE &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

Stack Class: pushing and popping nodes Prof. J. Joy 4 of 4

Main program to test the stack:

#include

#include

#include "Stack.h"

using namespace std;

//display program instructions to user

void instructions() {

cout<<"Enter one of the following: "

<<" 1 to push "

<<" 2 to pop "

<<" 3 to end processing ";

} //end function instructions

//function to test a Stack

template

void testList(Stack &listObject, const string &typeName )

{

cout<<"Testing a stack of "<

instructions();//display instructions

int choice;//store user choice

T value;//store input value

do//perform user-selected actions

{

cout<<"?";

cin>>choice;

switch(choice ) {

case 1:// push

cout<<"Enter "<

cin>>value;

listObject.push(value);

listObject.print();

break;

case 2:// pop

if(listObject.isEmpty()) cout<<"Can't pop, the list is empty ";

else cout<

listObject.print();

break;

}//end switch

} while(choice<3 );>

} //end testList

void main() {

cout<<"hello ";

//test Stack of string values

Stack myStack;

testList( myStack, "string" );

system("pause");

}

I can modify the code to add whatever I need in main to test the program with my own class.

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

More Books

Students also viewed these Databases questions