![]()
![]()
목차
없음
본문내용
3. 스택 클래스 선언 및 정의
// **** file: stack.h
#include
using namespace std;
class Stack {
public:
enum { MaxStack = 5 };
void init() { top = -1; }
void push( int n ) {
if ( isFull() ) {
errMsg( “Full stack. Can’t push.” );
return;
}
arr[ ++top ] = n;
}
int pop() {
if ( isEmpty() ) {
errMsg( “Empty stack. Popping dummy value.” );
return dummy_val;
}
return arr[ top– ];
}
bool isEmpty() { return top < 0; }
bool isFull() { return top >= MaxStack – 1; }
void dump() {
cout << "Stack contents, top to bottom:\n";
for ( int i = top; i >= 0; i– )
cout << '\t' << arr[ i ] << '\n';
}
private:
void errMsg( const char* msg ) const {
cerr << "\n*** Stack operation failure: " << msg << '\n';
}
int top;
int arr[ MaxStack ];
int dummy_val;
};
4. 수행 프로그램
//**** file: stack.cpp
#include "stack.h" // header for Stack class
출처 : 해피캠퍼스
답글 남기기