将堆栈中的每个值相加
Add every value in stack
本文关键字:堆栈 更新时间:2023-10-16
我正在尝试找出一种将堆栈中的每个值相加的方法。
目标是使用该值来确定堆栈中的所有值是否都是偶数。我已经写了代码来做到这一点
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
你不能在堆栈实现中定义你的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;
}
正如Gary提到的,您还可以通过在每次推送或弹出时缓存总值来提高效率(即堆栈已更改)溶胶-2:
添加一个成员属性,该属性将表示总和(例如,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;
}
每当删除对象时,pop() 都会进行相同的更新
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;
}
Sol-1 的 O(N) = N 成本,而 Sol-2 的 O(N) = 1
现在你应该能够使用 pStack->objectIsEven(pStack->getSum());
我认为您不需要关心堆栈实现中的线程,否则您应该在插入/删除操作时使用某种同步机制以使堆栈保持一致。
希望这有帮助。问候
使用此示例:
只需将总和保存在局部,然后一次从堆栈中弹出一个值:
void MathStack::addAll()
{
int num = 0, sum = 0;
while(!isEmpty())
{
pop(num);
sum += num;
}
push(sum);
}
相关文章:
- 算法问题:查找从堆栈中弹出的所有序列
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 为什么调用堆栈数组会导致内存泄漏
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 我的 int main() 中出现堆栈溢出错误
- 堆栈和队列是否像C++中的数组一样传递?
- 拥有映射的现代方法,该映射可以指向或引用已在堆栈上分配的不同类型的数据
- 为什么 STL 容器适配器堆栈中的 top 返回常量引用?
- 从堆栈分配的原始指针构造智能指针
- 在函数范围内在堆栈上分配的数组在离开函数时是否总是被释放?
- 堆栈中大小变量输入错误 (C++)
- 堆栈问题(平衡表达式问题集)
- C++ 在堆栈中包含多态属性的类对象存储
- 用于解析 win64 堆栈跟踪的命令行客户端(可以访问符号服务器)
- 在 C++ 中使用链表进行堆栈
- 变量周围的堆栈'...'已损坏
- 在 gtest 中初始化堆栈上的引用变量的隔离错误
- C++线程的可用堆栈大小