2012-04-22 49 views
1

我在下面有一些代码。这段代码是一个基本的推/弹出栈类,我已经创建了它作为模板来使某人能够推送/弹出栈。我有一个家庭作业,我现在要做的是创建一个具有多个值的堆栈。具有多个值的C++堆栈

所以我希望能够创建一个基本上可以发送三个整数的堆栈,并且我可以在这里推送/弹出这些堆栈。我在寻找的是关于这应该如何工作的理论,我不想让某人为我做我的功课。

这种情况是我们正在处理零件。因此,用户将输入序列号(int),生产日期(int)和lotnum(int)。所以我的问题是:

  1. 当我“弹出”的值,我应该尝试发送所有三个值在流行或处理这种方式?
  2. 我应该尝试使用像类或其他类似的结构创建新类吗?

    /**************************************************************************** 
    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; 
    } 
    
+1

您可以通过使用简化的isEmpty()只返回(!顶部)。 – parallelgeek 2012-04-22 22:56:37

+0

您也可以使用push(const T&)节约推送。在这种情况下,您将不会在调用此函数时构造一个临时值。 – parallelgeek 2012-04-22 23:23:33

回答

4

你可以创建一个结构是3个整型值合计,然后实例模板库存为结构财产以后对这些行

#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; 
+1

这听起来像一个很好的方法,让我试试这个! – chadpeppers 2012-04-22 23:04:26