gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:20:06 25 4
gpt4 key购买 nike

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

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

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com