forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 1
/
类模板之栈.cpp
63 lines (57 loc) · 1.14 KB
/
类模板之栈.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
using namespace std;
template<typename T,int MAXSIZE>
class Stack{
public:
Stack(){}
void init(){
top=-1;
}
bool isFull(){
if(top>=MAXSIZE-1)
return true;
else
return false;
}
bool isEmpty(){
if(top==-1)
return true;
else
return false;
}
void push(T e);
T pop();
private:
T elems[MAXSIZE];
int top;
};
template<typename T,int MAXSIZE> void Stack<T,MAXSIZE>::push(T e){
if(!isFull()){
elems[++top]=e;
}
else{
cout<<"栈已满,请不要再加入元素!";
return;
}
}
template<typename T,int MAXSIZE> T Stack<T,MAXSIZE>::pop(){
if(!isEmpty()){
return elems[top--];
}
else{
cout<<"栈已空,请不要再弹出元素!";
return 0;
}
}
int main(int argc, char const *argv[])
{
Stack<int,10> s1;
s1.init();
int i;
for(i=1;i<11;i++)
s1.push(i);
for(i=1;i<11;i++) cout<<s1.pop()<<"\t";
cout<<endl;
system("pause");
return 0;
}