아래 코드가 있습니다. 이 코드는 누군가가 푸시/팝 스택을 활성화 할 수 있도록 템플릿으로 만든 기본적인 푸시/팝 스택 클래스입니다. 나는 숙제를 가지고 있고, 지금하려고하는 것은 여러 값을 가진 스택을 만드는 것입니다.C++ 여러 값을 가진 스택
그래서 기본적으로 3 개의 정수를 전송할 수있는 스택을 만들 수 있고 싶을뿐만 아니라이를 푸시/팝 할 수도 있습니다. 내가 찾고있는 것은 이것이 어떻게 작동해야하는지에 대한 이론이며 누군가 나를 위해 숙제를하도록하지는 않는다.
시나리오는 우리가 부품을 다루는 것입니다. 따라서 사용자는 일련 번호 (int), 제조 날짜 (int) 및 lotnum (int)을 입력합니다. 따라서 내 질문은 다음과 같습니다.
- 값을 "팝"할 때 팝업 중에 세 개의 값을 모두 보내야합니까?
클래스 또는 다른 것과 같은 구조체를 사용하여 새 클래스를 만들어야합니까?
/**************************************************************************** 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; }
이 라인에서 그 구조체 뭔가를해야만위한 템플릿 재고를 인스턴스화 할 수 오직 반환 (! 상단). – parallelgeek
푸시 (const T &)를 사용하여 푸시를 절약 할 수도 있습니다. 이 경우이 함수를 호출하는 동안 임시 값을 생성하지 않습니다. – parallelgeek