-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacktp.h
66 lines (59 loc) · 903 Bytes
/
stacktp.h
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
64
65
66
// stacktp.h -- a stack template
#ifndef STACKTP_H_
#define STACKTP_H_
template <class Type>
class Stack
{
private:
enum {MAX = 10};
Type items[MAX];
int top;
public:
Stack();
bool isempty();
bool isfull();
bool push (const Type & item);
bool pop (Type & item);
Type operator[](int i) const;
};
template <class Type>
Stack<Type>::Stack()
{
top = 0;
}
template <class Type>
bool Stack<Type>::isempty()
{
return top == 0;
}
template <class Type>
bool Stack<Type>::isfull()
{
return top == MAX;
}
template <class Type>
bool Stack<Type>::push (const Type & item)
{
if (top < MAX) {
items[top++] = item;
return true;
} else {
return false;
}
}
template <class Type>
bool Stack<Type>::pop(Type & item)
{
if (top > 0) {
item = items[--top];
return true;
} else {
return false;
}
}
template <class Type>
Type Stack<Type>::operator[](int i) const
{
return items[i];
}
#endif