c++ - 如何将栈的pop实现为数组程序?

标签 c++ arrays stack

您好,我无法将堆栈的简单弹出函数实现为数组程序。代码在下面,我不确定如何修复它。

我不确定所有可能的情况,所以如果您能给我建议,我将不胜感激!

#include "Exception.h"

template <typename Type>
class Drop_off_stack_as_array {
    private:
        int itop;
        int ibottom;
        int entry_count;
        int array_capacity;
        Type *array;

    public:
        Drop_off_stack_as_array( int = 10 );
        Drop_off_stack_as_array( Drop_off_stack_as_array const & );
        ~Drop_off_stack_as_array();

        int size() const;
        bool empty() const;
        Type top() const;
        bool full() const; 

        void swap( Drop_off_stack_as_array & );
        Drop_off_stack_as_array &operator = ( Drop_off_stack_as_array );
        void push( Type const & );
        Type pop();
        void clear();



    // Friends

    template <typename T>
    friend std::ostream &operator << ( std::ostream &, Drop_off_stack_as_array<T> const & );
};

template <typename Type>
Drop_off_stack_as_array<Type>::Drop_off_stack_as_array( int n ):
    itop(0),
    ibottom(0),
    entry_count(0),
    array_capacity(n),
    array(new Type[array_capacity]){
        //empty constructor
    }

template <typename Type>
Drop_off_stack_as_array<Type>::Drop_off_stack_as_array( Drop_off_stack_as_array<Type> const &stack ):
itop( stack.itop ),
ibottom( stack.ibottom ),
entry_count( stack.entry_count ),
array_capacity( array_capacity ),
array( new Type[array_capacity] ) {
    // The above initializations copy the values of the appropriate
    // member variables and allocate memory for the data structure; 
    // however, you must still copy the stored objects.


    for(int i = 0; i<array_capacity; i++){
        array[i] = stack.array[i];
    }
}

template <typename Type>
Drop_off_stack_as_array<Type>::~Drop_off_stack_as_array() {

    delete[] array;
}

template <typename Type>
int Drop_off_stack_as_array<Type>::size() const {

    return entry_count;
}

template <typename Type>
bool Drop_off_stack_as_array<Type>::full() const {
    return (entry_count == array_capacity);
}

template <typename Type>
bool Drop_off_stack_as_array<Type>::empty() const {

    return (entry_count == 0);
}

template <typename  Type>
Type Drop_off_stack_as_array<Type>::top() const {
    if(empty()){
        throw underflow();
    }
    return array[itop];
}

template <typename Type>
void Drop_off_stack_as_array<Type>::swap( Drop_off_stack_as_array<Type> &stack ) {
    std::swap( itop, stack.itop );
    std::swap( ibottom, stack.ibottom );
    std::swap( entry_count, stack.entry_count );
    std::swap( array_capacity, stack.array_capacity );
    std::swap( array, stack.array );
}

template <typename Type>
Drop_off_stack_as_array<Type> &Drop_off_stack_as_array<Type>::operator = ( Drop_off_stack_as_array<Type> rhs ) {
    swap( rhs );

    return *this;
} 



template <typename Type>
void Drop_off_stack_as_array<Type>::push( Type const &obj ) {
    if(full()){
        array[ibottom] = 0;
        itop = ibottom;
        ++ibottom;
    }
    else{
        array[itop+1] = obj;
        ++itop;
        ++entry_count;
    }
}

template <typename Type>
Type Drop_off_stack_as_array<Type>::pop() {

    if(empty()){
        throw underflow();
    }

    array[itop] = 0;
    --itop;
    --entry_count;

}

template <typename Type>
void Drop_off_stack_as_array<Type>::clear() {
    delete [] array;
    array = new Type(array_capacity);
}

最佳答案

也许是这样的:

template <typename Type>
Type Drop_off_stack_as_array<Type>::pop() {

    if(empty()){
        throw underflow();
    }

    Type result = array[itop]; // Safe a copy of the top element
    array[itop] = 0;
    --itop;
    --entry_count;
    return result; // return it (your return type is not void,
                   // so you need a return statment which returns a value
}

您的代码(不仅是这个地方)的奇怪之处在于 = 0以及你在用 itop 做什么和 ibottom计数器/指数/...?但我想这是另一个等待解决的问题,您的眼前问题有望通过上述解决。

(下次至少包括您在问题中得到的错误消息/警告,谢谢!)

关于c++ - 如何将栈的pop实现为数组程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19624623/

相关文章:

java - 使用 JNI 检索 jint 值时的随机值

c++ - 如何根据 std::remove_copy_if 实现 copy_if?

c++ - 将 Qt 线程与 COM 线程混合

c++ - 卡在一个 C++ 幸运七游戏程序上

python - 函数在数组的单个列中的每一行上迭代工作 - numpy

javascript - 在 JavaScript 中获取数组中对象的特定字段的总和

java - 在任何情况下,使用 ArrayList 实现的 Stack 或 Queue 是否比 Java 中实现的 LinkedList 提供更好、更快的性能?

php - 在 PHP 中处理其他程序员的复杂数组

stack - WinDBG 显示不完整的堆栈

堆栈跟踪 : stack scanning vs call frame vs given as instruction pointer in context