gpt4 book ai didi

具有多个值的 C++ 堆栈

转载 作者:行者123 更新时间:2023-11-30 01:26:31 29 4
gpt4 key购买 nike

我在下面有一些代码。此代码是一个基本的压入/弹出堆栈类,我将其创建为模板以允许某人压入/弹出堆栈。我有一个家庭作业,我现在要做的是创建一个具有多个值的堆栈。

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

场景是我们在处理零件。因此用户将输入序列号 (int)、制造日期 (int) 和批号 (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;
    }

最佳答案

您可以创建一个结构,它是 3 个 int 值的集合,然后在这些行上为该结构实例化模板库存

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

关于具有多个值的 C++ 堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10272725/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com