顺序栈
//
// 顺序栈
//
// Created by chenshang on 14-2-6.
// Copyright (c) 2014年 chenshang. All rights reserved.
//
#ifndef TestList_SeqStack_h
#define TestList_SeqStack_h
#include <iostream>
using namespace std;
typedef int T;
class SeqStack{
public:
SeqStack(int sz):top(-1),MaxSize(sz)
{
arr = new T[sz];
if (arr==NULL) {
cout<<"application error"<<endl;
exit(1);
}
}
~SeqStack(){
delete[] arr;
}
public:
void push(T item);//入栈
T pop();//出栈
T getTop();//得到栈顶元素
void print();//打印栈的元素
//清空栈的数据
void Empty(){
top=-1;
};
bool isEmpty()const{
return top==-1;
}
bool isFull()const{
return top==MaxSize-1;
}
private:
int top;
int *arr;
int MaxSize;
};
void SeqStack::push(T item){
if (isFull()) {
cout<<"The stack is full"<<endl;
return;
}
arr[++top]=item;
}
T SeqStack::pop(){
if (isEmpty()) {
cout<<"There is no element!"<<endl;
exit(1);
}
//先返回top的值,然后再减一
return arr[top--];
}
T SeqStack::getTop(){
if (isEmpty()) {
cout<<"There is no element!"<<endl;
exit(1);
}
return arr[top];
}
void SeqStack::print(){
cout<<"bottom";
for (int i=0; i<=top; i++) {
cout<<"-->"<<arr[i];
}
cout<<"-->top"<<endl<<endl<<endl;
}
#endif