#define N 100 //100 個堆疊空間
int stack[N], sp=0;
//堆疊與堆疊指位器
void push(int data){//將資料 data 推入堆疊
if(sp>=N) //堆疊滿了
std::cout << "Stack full\n";
else
stack[sp++] = data;
}
int pop(){ //從堆疊取出料傳回
if(sp<=0) { //空堆疊
std::cout << "Stack empty\n";
return(-1);
}
else
return(stack[--sp]);
} |
const int N = 10;
class Stack{
private:
int data[N];int sp;
public:
Stack(){
sp=0;
}
void push(int x){
if(sp<N)
data[sp++]=x;
else
std::cout << "Stack full.\n";
}
int pop(){
if(N>0)
return(data[--sp]);
else
std::cout << "Stack empty.\n";
return(-1);
}
}; |