【问题标题】:Are all the values on the Stack smaller than a passed value?堆栈上的所有值是否都小于传递的值?
【发布时间】:2013-10-31 00:17:17
【问题描述】:

我正在完成一个堆栈程序,但不确定如何制定一种方法来确定堆栈上的所有值是否都小于传递的值。

因此,例如,对于包含 top- (1) (1) (2) (3) (5) (5) (2) 的堆栈,allSmallerThan(3) 应该返回 false,因为堆栈上的值更大大于 3。另一方面,对于包含 top- (1) (1) (2) (3) (5) (5) (2) 的堆栈,allSmallerThan(12) 应该返回 true,因为堆栈上的所有值小于 12。

我已经做了操作签名

int Stack<Object>::allSmallerThan( const Object & data ) const; 

但在那之后,我有点不知道该去哪里

以下是完整的实现文件。大部分文件都交给了我们,我们被要求完成某些操作。尤其是这一个是杀戮

#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>
void Stack<Object>::makeEmpty() {
    while (!isEmpty()) {
        pop();
    }
}
template <class Object>
    int Stack<Object>::allSmallerThan( const Object & data ) const{


    }


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

一切顺利 谢谢!

【问题讨论】:

  • 如果你实现了iterator,你可以使用泛型algorithm,在c++11中,std::all_of
  • [OT] 从守卫看来,您在.cpp 中编写了模板。我不确定这是否是您想要的,您可能更喜欢*.inl

标签: c++ templates c++11 stack


【解决方案1】:

由于您的元素未在堆栈中排序,您必须像在 printStack(...) 中那样手动遍历它们并自己检查值。

您可以在不小于给定值的第一次出现时中止。

【讨论】:

    【解决方案2】:

    遍历堆栈的所有值。如果其中一个值不小于参数,则返回 false。如果循环结束,则所有值都较小,因此返回 true。

    【讨论】:

    • 这个循环是什么样的。在这一点上做循环时,我总是踩到自己的脚。
    【解决方案3】:

    类似这样的:

     template <class Object>
            int Stack<Object>::allSmallerThan( const Object & data ) const{
            if (isEmpty()) {
                outs << "Empty Stack";
            }
            else {
                StackNode<Object> * node = topNode;
                while (node != NULL) {
                     if (node >= data) //You need to do the comparison by hand
                      return (false);
                    node = node->getNext();
                }
            }
            return( true );
         }
    

    【讨论】:

    • 您正在将对象与 StackNode 进行比较,这很可能会失败。可以通过node->getElement()从node访问对应的Object,但是不知道有没有实现比较运算符...
    • 您将 14 行 c++ 代码与一行伪代码混合在一起,这可能会导致 OP 走向错误的方向。您还使用了之前未定义的变量 outs...
    • 我试图实现这个伪代码。在驱动程序文件中,我将通过什么( const Object & data )?例如案例 ALLSMALLERTHAN: if (stack.allSmallerThan(XXX)) { cout
    • 你必须用这样的东西实例化你的 stask 对象: Stack stack;所以你需要传递一个相同类型的对象:stack.allSmallerThan(type);
    • if (node &gt;= data) 我收到错误Comparison between pointer and integer ('StackNode&lt;int&gt; *' and 'int')
    【解决方案4】:

    这最终奏效了

       template <class Object>
        int Stack<Object>::allSmallerThan( const Object & data ) const{
            if (isEmpty()) {
                throw EmptyStack();
            }
            else {
                StackNode<Object> * node = topNode;
                while (node != NULL) {
                    if (data <= node->getElement()) //You need to do the comparison by hand
                        return (false);
                    node = node->getNext();
                }
            }
            return( true );
        }
    

    【讨论】:

      猜你喜欢
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 2014-04-13
      • 1970-01-01
      • 2014-10-04
      相关资源
      最近更新 更多