2013-10-22 49 views
0

我想找出一个方法,将添加堆栈中的每个值。添加堆栈中的每个值

目标是使用该值来确定堆栈中的所有值是否均匀。 我已经写代码来做到这一点

template <class Object> 
bool Stack<Object>::objectIsEven(Object value) const { 
    bool answer = false; 
    if (value % 2 == 0) 
      answer = true; 
    return(answer); 
} 

不过,我难倒如何添加所有堆叠的价值观在一个单独的方法

Stack.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 
+1

你需要告诉我们堆栈的样子。 –

+2

我觉得命名一个模板参数'Object'是很奇怪的。 –

+0

@ZacWrangler编辑 – rezivor

回答

1

你不能在栈实现中定义你的getSum()函数吗?还是有任何限制?

溶胶 - 1:

template <class Object> 
Object Stack<Object>::getSum() { 
    Object sum = 0; // or memset or other stuffs 
    StackNode<Object> * node = topNode; 
    while (node != NULL) { 
     sum += node->getElement(); 
     node = node->getNext(); 
    } 
    return sum; 
} 

正如加里提到也可以通过缓存总和值每次推或弹出提高效率(I,E堆被改变) 溶胶2:

添加成员属性,这将表示之和(E,G - 总和)

template <class Object> 
Stack<Object>::Stack() { 
    topNode = NULL; 
    this->sum = 0; // initialize or new object 
} 

更新每当对象被推总和

template <class Object> 
void Stack<Object>::push(const Object& data) { 
    StackNode<Object>* newNode = new StackNode<Object>(data, topNode); 
    topNode = newNode; 
    this->sum = this->sum + data; 
} 

相同的更新也适用于只要对象被移除

template <class Object> 
void Stack<Object>::pop() { 
    if (isEmpty()) { 
     throw EmptyStack(); 
    } 
    StackNode<Object> *oldTop = topNode; 
    topNode = topNode->getNext(); 
    this->sum = this->sum - oldTop->getElement(); 
    delete oldTop; 
} 

现在流行()的最后一个函数getSum()

template <class Object> 
    Object Stack<Object>::getSum() { 
     return this->sum; 
    } 

溶胶 - 1具有O(N)= N成本虽然Sol-2的O(N)= 1

现在你应该可以使用pStack-> objectIsEven(pStack-> getSum());

我认为你不需要关心线程在你的栈实现,否则你应该使用某种同步机制,而插入/删除操作使栈一致。

希望这会有所帮助。 问候,

0

使用此例如:

只需将总和保留在本地,然后逐个弹出堆栈中的值:

void MathStack::addAll() 
{ 
    int num = 0, sum = 0; 

    while(!isEmpty()) 
    { 
     pop(num); 
     sum += num; 
    } 

    push(sum); 
}