OpenMoHAA
0.82.0
Loading...
Searching...
No Matches
stack.h
1
/*
2
===========================================================================
3
Copyright (C) 2024 the OpenMoHAA team
4
5
This file is part of OpenMoHAA source code.
6
7
OpenMoHAA source code is free software; you can redistribute it
8
and/or modify it under the terms of the GNU General Public License as
9
published by the Free Software Foundation; either version 2 of the License,
10
or (at your option) any later version.
11
12
OpenMoHAA source code is distributed in the hope that it will be
13
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with OpenMoHAA source code; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
===========================================================================
21
*/
22
23
// stack.h: Generic Stack object.
24
//
25
26
#pragma once
27
28
#include "g_local.h"
29
#include "class.h"
30
31
template
<
class
Type>
32
class
StackNode :
public
Class
33
{
34
public
:
35
Type data;
36
StackNode *next;
37
38
StackNode( Type d );
39
};
40
41
template
<
class
Type>
42
inline
StackNode<Type>::StackNode( Type d ) : data( d )
43
{
44
next = NULL;
45
}
46
47
template
<
class
Type>
48
class
Stack :
public
Class
49
{
50
private
:
51
StackNode<Type>
*head;
52
53
public
:
54
Stack();
55
~Stack<Type>();
56
void
Clear(
void
);
57
qboolean Empty(
void
);
58
void
Push( Type data );
59
Type Pop(
void
);
60
};
61
62
template
<
class
Type>
63
inline
Stack<Type>::Stack()
64
{
65
head = NULL;
66
}
67
68
template
<
class
Type>
69
inline
Stack<Type>::~Stack()
70
{
71
Clear();
72
}
73
74
template
<
class
Type>
75
inline
void
Stack<Type>::Clear
76
(
77
void
78
)
79
80
{
81
while
( !Empty() )
82
{
83
Pop();
84
}
85
}
86
87
template
<
class
Type>
88
inline
qboolean Stack<Type>::Empty
89
(
90
void
91
)
92
93
{
94
if
( head == NULL )
95
{
96
return
true
;
97
}
98
return
false
;
99
}
100
101
template
<
class
Type>
102
inline
void
Stack<Type>::Push
103
(
104
Type data
105
)
106
107
{
108
StackNode<Type>
*tmp;
109
110
tmp =
new
StackNode<Type>
( data );
111
if
( !tmp )
112
{
113
assert( NULL );
114
gi.Error( ERR_DROP,
"Stack::Push : Out of memory"
);
115
}
116
117
tmp->next = head;
118
head = tmp;
119
}
120
121
template
<
class
Type>
122
inline
Type Stack<Type>::Pop
123
(
124
void
125
)
126
127
{
128
Type ret;
129
StackNode<Type>
*node;
130
131
if
( !head )
132
{
133
return
NULL;
134
}
135
136
node = head;
137
ret = node->data;
138
head = node->next;
139
140
delete
node;
141
142
return
ret;
143
}
StackNode
Definition
stack.h:33
code
fgame
stack.h
Generated by
1.13.2