OpenMoHAA 0.82.1
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{
34public:
35 Type data;
36 StackNode *next;
37
38 StackNode(Type d);
39};
40
41template<class Type>
42inline StackNode<Type>::StackNode(Type d)
43 : data(d)
44{
45 next = NULL;
46}
47
48template<class Type>
49class Stack : public Class
50{
51private:
52 StackNode<Type> *head;
53
54public:
55 Stack();
56 ~Stack<Type>();
57 void Clear(void);
58 qboolean Empty(void);
59 void Push(Type data);
60 Type Pop(void);
61};
62
63template<class Type>
64inline Stack<Type>::Stack()
65{
66 head = NULL;
67}
68
69template<class Type>
70inline Stack<Type>::~Stack()
71{
72 Clear();
73}
74
75template<class Type>
76inline void Stack<Type>::Clear(void)
77{
78 while (!Empty()) {
79 Pop();
80 }
81}
82
83template<class Type>
84inline qboolean Stack<Type>::Empty(void)
85{
86 if (head == NULL) {
87 return true;
88 }
89 return false;
90}
91
92template<class Type>
93inline void Stack<Type>::Push(Type data)
94{
95 StackNode<Type> *tmp;
96
97 tmp = new StackNode<Type>(data);
98 if (!tmp) {
99 assert(NULL);
100 gi.Error(ERR_DROP, "Stack::Push : Out of memory");
101 }
102
103 tmp->next = head;
104 head = tmp;
105}
106
107template<class Type>
108inline Type Stack<Type>::Pop(void)
109{
110 Type ret;
111 StackNode<Type> *node;
112
113 if (!head) {
114 return NULL;
115 }
116
117 node = head;
118 ret = node->data;
119 head = node->next;
120
121 delete node;
122
123 return ret;
124}
Definition stack.h:33