我试图弄清楚如何计算堆栈中的重复值。
我到目前为止的代码如下。我不知道如何存储已记录一次、两次或 100 次的值。假设我有一堆 (1, 1, 1, 4, 5, 3) 并且我想计算 1 出现的次数。每次在堆栈中计数 1 时,我如何存储?
template <class Object>
int Stack<Object>::count( const Object & data ) const{
StackNode<Object> * node = topNode;
int n = 0;
while (node != NULL) {
if (data == node->getElement())
n++;
node = node->getNext();
}
return n;
}
编辑: 我更新了一个修改过的代码,仍然不太正确,但更接近