我试图找出一种方法来添加堆栈中的每个值。
目标是使用该值来确定堆栈中的所有值是否都是偶数。我已经写了代码来做到这一点
template <class Object>
bool Stack<Object>::objectIsEven( Object value ) const {
bool answer = false;
if (value % 2 == 0)
answer = true;
return( answer );
}
但是,我对如何在单独的方法中添加所有堆栈值感到困惑
堆栈.cpp
#ifndef STACK_CPP
#define STACK_CPP
#include "Stack.h"
namespace cs20 {
template <class Object>
Stack<Object>::Stack() {
topNode = NULL;
}
template <class Object>
Stack<Object>::Stack( const Stack<Object>& rhs ) {
topNode = NULL;
*this = rhs;
}
template <class Object>
Stack<Object>::~Stack() {
makeEmpty();
delete topNode;
}
template <class Object>
bool Stack<Object>::isEmpty() const {
return( (topNode == NULL) );
}
template <class Object>
bool Stack<Object>::even() const
{
}
// template Object must support the % operator which ints do
template <class Object>
bool Stack<Object>::objectIsEven( Object value ) const {
bool answer = false;
if (value % 2 == 0)
answer = true;
return( answer );
}
template <class Object>
void Stack<Object>::makeEmpty() {
while (!isEmpty()) {
pop();
}
}
template <class Object>
void Stack<Object>::push( const Object& data ) {
StackNode<Object>* newNode = new StackNode<Object>( data, topNode );
topNode = newNode;
}
template <class Object>
void Stack<Object>::pop() {
if (isEmpty()) {
throw EmptyStack();
}
StackNode<Object> *oldTop = topNode;
topNode = topNode->getNext();
delete oldTop;
}
template <class Object>
const Object& Stack<Object>::top( ) const {
if (isEmpty()) {
throw EmptyStack();
}
StackNode<Object> node = *topNode;
return( node.getElement() );
}
template <class Object>
Object Stack<Object>::topAndPop( ) {
Object o = top();
pop();
return( o );
}
// Deep copy of linked Stack
template <class Object>
const Stack<Object>& Stack<Object>::operator =( const Stack<Object>& rhs ) {
if (this != &rhs) {
makeEmpty();
if (!(rhs.isEmpty())) {
StackNode<Object> * rhsTopNode = rhs.topNode;
StackNode<Object> * myTopNode = new StackNode<Object>( rhsTopNode->getElement() );
topNode = myTopNode;
rhsTopNode = rhsTopNode->getNext();
while (rhsTopNode != NULL) {
myTopNode->setNext( new StackNode<Object>( rhsTopNode->getElement() ) );
myTopNode = myTopNode->getNext();
rhsTopNode = rhsTopNode->getNext();
}
}
}
return( *this );
}
template <class Object>
std::ostream& Stack<Object>::printStack( std::ostream& outs ) const {
if (isEmpty()) {
outs << "Empty Stack";
}
else {
outs << "TOP: ";
StackNode<Object> * node = topNode;
while (node != NULL) {
outs << node->getElement();
outs << "\n "; /// for visual alignment
node = node->getNext();
}
}
return( outs );
}
}
#endif