#include <iostream>/File: int-stack.h/
#include <cstdlib>
using namespace std;
const int BUFFER SIZE = 100;
class int stack
{
private:
int data[BUFFER SIZE]; // Use an array to store data
int top_index; // Start from 0; -1 when empty
public:
int stack(void); // Default constructor
bool empty(void) const; // Check if the stack is empty
bool full(void) const; // Check if the stack is full
int size(void) const; // Give the number of items currently stored
int top(void) const; // Retrieve the value of the top item
void push(int); // Add a new item to the top of the stack
void pop(void); // Remove the top item from the stack
void insert_at_bottom(int_stack& stack, int item); //adds a new item to the bottom of the stack if the stack is not full.
};
与えられた関数reverse_stack(int_stack &スタック)を実装する必要があります。 これは実装でスタックを使用しても問題はありませんか?
void reverse_stack(int_stack& stack)
{
if (stack.empty())
return;
else
{
int top_item = stack.top();
stack.pop();
reverse_stack(stack);
insert_at_bottom(stack, top_item);
}
}
私のソリューションはこれです、再帰を使用してソリューションを提案している、私はそれが正しいことを知りたいと思いました。
void reverse_stack(int_stack& stack){
while(stack.empty() == false){
insert_at_bottom(stack,stack.top());
stack.pop();
}
'pop'pingの前に常に' insert_at_bottom'を呼び出すので、これは永遠に実行されるので、スタックの内容は常に回転します。また、関数に構文エラーがあります。 – ForceBru
適切にテストできるように、コード用のテストハーネスを記述する必要があります。 –