Stack Class 实现
Stack.h
Stack.cpp
测试main.cpp
1
#ifndef STACK_H
2
#define STACK_H
3
4
5
#define SIZE_INIT 100
6
#define SIZE_INCREMENT 100
7
template <typename Type> class Stack
8
{
9
public:
10
Stack();
11
~Stack();
12
int getLength() const { return length; }
13
Type getTop();
14
bool empty(){ return (top == base); }
15
bool full(){ return (length >= size); }
16
void push(const Type &val);
17
Type pop();
18
void makeEmpty();
19
void visit();
20
21
22
private:
23
Type *base;
24
Type *top;
25
int length;
26
int size;
27
};
28
29
#include "Stack.cpp"
30
31
32
#endif

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

Stack.cpp
1
//for Stack class implement
2
3
template <typename Type> Stack<Type>::Stack() : length(0), size(SIZE_INIT)
4
{
5
base = new Type[SIZE_INIT];
6
top = base;
7
}
8
9
template <typename Type> Stack<Type>::~Stack()
10
{
11
delete [] base;
12
13
}
14
15
template <typename Type> Type Stack<Type>::getTop()
16
{
17
if(empty())
18
{
19
cerr << "Stack empty: getTop()\n";
20
exit(-1);
21
}
22
23
return *(top-1);
24
}
25
26
template <typename Type> void Stack<Type>::push(const Type &val)
27
{
28
if(full())
29
{
30
cerr << "Stack full: push()\n";
31
exit(-1);
32
}
33
34
*top = val;
35
top++;
36
length++;
37
}
38
39
template <typename Type> Type Stack<Type>::pop()
40
{
41
if(empty())
42
{
43
cerr << "Stack empty: pop()\n";
44
exit(-1);
45
}
46
47
Type val = *(top-1);
48
top--;
49
length--;
50
51
return val;
52
}
53
54
template <typename Type> void Stack<Type>::makeEmpty()
55
{
56
if(empty())
57
{
58
return;
59
}
60
61
while(top != base)
62
{
63
pop();
64
}
65
66
}
67
68
69
template <typename Type> void Stack<Type>::visit()
70
{
71
if(empty())
72
{
73
cout << "empty, no element visited\n";
74
return;
75
}
76
77
Type *ptr = base;
78
79
cout << "[bottom: ";
80
while(ptr != top)
81
{
82
cout << *ptr << " ";
83
ptr++;
84
}
85
cout << ":top]\n";
86
87
}

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

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

测试main.cpp
1
#include <iostream>
2
#include <string>
3
#include "Stack.h"
4
using namespace std;
5
6
int main()
7
{
8
Stack<int> iStack;
9
iStack.push(2);
10
iStack.push(5);
11
iStack.push(7);
12
iStack.push(78);
13
iStack.push(32);
14
iStack.push(93);
15
16
//cout << iStack.getLength() << " " << iStack.getTop() << ;
17
18
iStack.visit();
19
20
iStack.pop();
21
22
iStack.visit();
23
24
iStack.pop();
25
26
iStack.visit();
27
28
iStack.makeEmpty();
29
30
//cout << iStack.getLength() ;
31
//cout << " " << iStack.getTop();
32
33
Stack<string> strStack;
34
strStack.push("I");
35
strStack.push("am");
36
strStack.push("a");
37
strStack.push("graduate");
38
strStack.push("student");
39
strStack.push("in");
40
strStack.push("SJTU");
41
42
strStack.visit();
43
44
strStack.pop();
45
46
strStack.visit();
47
48
strStack.pop();
49
50
strStack.visit();
51
52
53
54
return 0;
55
56
}

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
