-
Notifications
You must be signed in to change notification settings - Fork 2
/
stl_list.h
212 lines (169 loc) · 3.93 KB
/
stl_list.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#ifndef STL_LIST_H
#define STL_LIST_H
#ifdef _WIN32
#pragma once
#endif
#include <stdlib.h>
//#include "stl_exception.h"
#include "stl_node.h"
namespace rtx_stl
{
// Doug 9/21/2010
// Not tested very heavily. I don't suggest trusting this.
// Unlike the previously defined class (node), you
// are intended to use this outside of this file.
template <typename T> class list
{
private:
node<T> * head;
node<T> * tail;
node<T> * seek( int );
int num;
public:
// Constructors.
list();
~list();
node<T> * get( int );
void insert( int, node<T> * );
node<T> * remove( int );
void push_front( node<T> * );
node<T> * pop_front();
void push_back( node<T> * );
node<T> * pop_back();
void clear();
bool empty();
int size();
};
}
#ifdef _WIN32
;
#endif
// Doug 9/21/2010
// MSVC is retarded and won't let you separate template declarations
// and definitions, so we have to put the implementation in this header...
// I separated it in case it's actually me who's retarded and we can
// make this less horrible later.
#define RTX_STL_INIT_LIST() head = NULL; \
tail = NULL; \
num = 0;
namespace rtx_stl
{
//////////////////////////////////
// Queue functions //
//////////////////////////////////
template <typename T> list<T>::list()
{
RTX_STL_INIT_LIST();
}
template <typename T> list<T>::~list()
{
clear();
}
template <typename T> void list<T>::clear()
{
node<T> * current = head;
while ( current != NULL )
{
// Compiler will optimize this out.
node<T> * successive = current;
current = current->next;
delete successive;
}
RTX_STL_INIT_LIST();
}
template <typename T> node<T> * list<T>::seek( int position )
{
node<T> * current = head;
for ( int i = 0; i < position && current != NULL; i++ )
{
if ( current->next == NULL )
return current;
current = current->next;
}
return current;
}
template <typename T> node<T> * list<T>::get( int position )
{
if ( empty() )
throw "List is empty";
if ( position < 0 || position >= num )
throw "Out of bounds";
return seek( position );
}
template <typename T> void list<T>::insert( int position, node<T> * x )
{
if ( position < 0 || position > num )
throw "Out of bounds";
if ( x == NULL )
throw "Invalid data";
x->next = NULL;
x->prev = NULL;
node<T> * current = seek( position );
if ( num && position == num )
{
current->next = x;
x->prev = current;
}
else if ( current != NULL )
{
x->next = current;
if ( current->prev != NULL )
{
current->prev->next = x;
x->prev = current->prev;
}
current->prev = x;
}
if ( head == NULL || position == 0 )
head = x;
if ( tail == NULL || position == num )
tail = x;
num++;
}
template <typename T> node<T> * list<T>::remove( int position )
{
if ( empty() )
throw "List is empty";
if ( position < 0 || position >= num )
throw "Out of bounds";
node<T> * current = seek( position );
if ( current->next != NULL )
current->next->prev = current->prev;
if ( current->prev != NULL )
current->prev->next = current->next;
if ( current == head )
head = current->next;
if ( position == num - 1 )
tail = current->prev;
num--;
return current;
}
template <typename T> void list<T>::push_front( node<T> * x )
{
insert( 0, x );
}
template <typename T> node<T> * list<T>::pop_front()
{
return remove( 0 );
}
template <typename T> void list<T>::push_back( node<T> * x )
{
insert( num, x );
}
template <typename T> node<T> * list<T>::pop_back()
{
return remove( num - 1 );
}
template <typename T> bool list<T>::empty()
{
return head == NULL;
}
template <typename T> int list<T>::size()
{
return num;
}
}
#ifdef _WIN32
;
#endif
#endif