2012-04-22 115 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