Reputation: 2057
I have some code below. This code is a basic push/pop stack class that I have created as a template to enable someone to push/pop stacks. I have a homework assignment and what I am trying to do now is create a stack that has multiple values.
So I want to be able to create a stack that basically can be sent three integers and where I can push/pop these as well. What I am looking for is the theory on how this should work and I am not trying to get someone to do my homework for me.
The scenario is that we are dealing with parts. So the user will put in the serial number (int), manufacture date (int), and lotnum (int). So my questions are:
Should I try to create a new class using the struct like the class or something else?
/****************************************************************************
Inventory class.
Chad Peppers
This class creates a object for stacking nodes
In addition, there should be member functions to perform the following
operations:
- Push to the stack
- Pop to the stack
- Function to check if empty
****************************************************************************/
// Specification file for the DynIntStack class
template <class T>
class Inventory
{
private:
// Structure for stack nodes
struct StackNode
{
T value; // Value in the node
StackNode *next; // Pointer to the next node
};
StackNode *top; // Pointer to the stack top
public:
// Constructor
Inventory()
{ top = NULL; }
// Destructor
~Inventory();
// Stack operations
void push(T);
void pop(T &);
bool isEmpty();
};
/*************************************************************************
Basic class constructor.
Input Parameters: Information to build the stack
Return Type: void
*************************************************************************/
template<class T>
Inventory<T>::~Inventory()
{
StackNode *nodePtr, *nextNode;
// Position nodePtr at the top of the stack.
nodePtr = top;
// Traverse the list deleting each node.
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
/*************************************************************************
Function to push an item in the stack
Input Parameters: T
Return Type: void
*************************************************************************/
template<class T>
void Inventory<T>::push(T num)
{
StackNode *newNode; // Pointer to a new node
// Allocate a new node and store num there.
newNode = new StackNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node.
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else // Otherwise, insert NewNode before top.
{
newNode->next = top;
top = newNode;
}
}
/*************************************************************************
Function to pop an item in the stack
Input Parameters: T
Return Type: void
*************************************************************************/
template<class T>
void Inventory<T>::pop(T &num)
{
StackNode *temp; // Temporary pointer
// First make sure the stack isn't empty.
if (isEmpty())
{
cout << "The stack is empty.\n";
}
else // pop value off top of stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}
}
/*************************************************************************
Basic class deconstructor.
Input Parameters: None
Return Type: void
*************************************************************************/
template<class T>
bool Inventory<T>::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
Upvotes: 1
Views: 3352
Reputation: 17451
you could create a struct which is aggregate of the 3 int values and then instantiate template Inventory for that struct somthing on these lines
#include "Inventory.h"
//create an aggregate structure
struct ProductData {
int serial_num;
int manufacture_date;
int lot_num;
}
//instantiate Inventory for ProductData
Inventory<ProductData> stack;
Upvotes: 5