#include
#include
#define maxsize 100//默认堆栈的最大长度为100 using namespace std; template
class Stack { private: T arr[maxsize]; int currentSize;//保存栈顶的位置 public: Stack():currentSize(0) {}//初始化栈顶为0 void push(T temp)//新元素入栈,该元素为栈顶元素(压栈) { arr[currentSize++]=temp; } T pop()//栈顶元素出栈 { T temp=arr[currentSize-1]; arr[currentSize-1]=NULL; currentSize--; return temp; } bool empty()//判断该堆栈是否为空 { return (currentSize==0)?true:false; } bool full()//判断该堆栈是否为满 { return (currentSize==100)?true:false; } int size()//获得该堆栈的当前长度 { return currentSize; } int top()//获得该堆栈当前的栈顶元素 { return currentSize; } }; int main() { //在主函数中分别测试int、char和double型数据 string type=""; int length,tempInt,n; double tempDouble; char tempChar; while(cin>>type&&type!="STOP") { cin>>length; if(type=="Int") { Stack
Int; for(int i=0; i
>tempInt; if(Int.full()) cout<<"full!! "; else Int.push(tempInt); } cin>>n; for(int i=0; i
Char; for(int i=0; i
>tempChar; if(Char.full()) cout<<"full!! "; Char.push(tempChar); } cin>>n; for(int i=0; i
Sample input
Int
10 1 2 3 4 5 6 7 8 9 10
5
Double
5 0.8 4.5 6.2 5.4 12.9
7
Char
8 g h s a f o i p
6
STOP
Sample output
10 9 8 7 6
12.9 5.4 6.2 4.5 0.8 empty!! empty!!
p i o f a s