1 #pragma once
2
3 typedef int Type;
4
5 template <class Type> class SStack{
6 private:
7 Type *data;
8 int sp;
9 int size;
10
11 bool IsEmpty();
12 bool IsFull();
13
14 public:
15 SStack(int len);
16 ~SStack();
17 bool Push(Type d);
18 bool Pop(Type *d);
19 };
1 #include <stdlib.h>
2 #include "SquenceStack.h"
3
4 template <class Type> SStack<Type> :: SStack(int len)
5 {
6 data = (Type *)malloc(sizeof(*data) * len);
7 if(NULL == data)
8 exit(-1);
9
10 size = len;
11 sp = 0;
12 }
13
14 template <class Type> SStack<Type> :: ~SStack()
15 {
16 free(data);
17 size = 0;
18 sp = 0;
19 }
20
21 template <class Type> bool SStack<Type> :: IsEmpty()
22 {
23 return 0 == sp ;
24 }
25
26 template <class Type> bool SStack<Type> :: IsFull()
27 {
28 return sp == size;
29 }
30
31 template <class Type> bool SStack<Type> :: Push(Type d)
32 {
33 if (IsFull())
34 return false;
35
36 data[sp++] = d;
37
38 return true;
39 }
40
41 template <class Type> bool SStack<Type> :: Pop(Type *d)
42 {
43 if (IsEmpty())
44 return false;
45
46 *d = data[--sp];
47
48 return true;
49 }
1 #include <iostream>
2 #include "SquenceStack.h"
3 #include "SquenceStack.cpp"
4
5 using namespace std;
6
7 int main(int argc, char **argv)
8 {
9 int i;
10 int a[] = {1, 2, 3, 4 ,5 ,6 ,7};
11 class SStack<Type> stk(20);
12 Type d;
13
14 for (i = 0; i < 7; i++)
15 stk.Push(a[i]);
16
17 while (stk.Pop(&d))
18 cout << d << " ";
19
20 cout << endl;
21
22 return 0;
23 }