2

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

因此,例如,对于包含 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

万事如意,谢谢!

4

4 回答 4

3

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

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

于 2013-10-22T08:21:42.160 回答
2

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

于 2013-10-22T08:23:07.950 回答
1

像这样的东西:

 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 );
     }
于 2013-10-22T08:31:13.587 回答
0

这最终起作用了

   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 );
    }
于 2013-10-22T10:09:38.720 回答