OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
stack.h
1/*
2===========================================================================
3Copyright (C) 2024 the OpenMoHAA team
4
5This file is part of OpenMoHAA source code.
6
7OpenMoHAA source code is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 2 of the License,
10or (at your option) any later version.
11
12OpenMoHAA source code is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OpenMoHAA source code; if not, write to the Free Software
19Foundation, 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
31template <class Type>
32class StackNode : public Class
33 {
34 public:
35 Type data;
36 StackNode *next;
37
38 StackNode( Type d );
39 };
40
41template <class Type>
42inline StackNode<Type>::StackNode( Type d ) : data( d )
43 {
44 next = NULL;
45 }
46
47template <class Type>
48class 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
62template <class Type>
63inline Stack<Type>::Stack()
64 {
65 head = NULL;
66 }
67
68template <class Type>
69inline Stack<Type>::~Stack()
70 {
71 Clear();
72 }
73
74template <class Type>
75inline void Stack<Type>::Clear
76 (
77 void
78 )
79
80 {
81 while( !Empty() )
82 {
83 Pop();
84 }
85 }
86
87template <class Type>
88inline qboolean Stack<Type>::Empty
89 (
90 void
91 )
92
93 {
94 if ( head == NULL )
95 {
96 return true;
97 }
98 return false;
99 }
100
101template <class Type>
102inline 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
121template <class Type>
122inline 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 }
Definition stack.h:33