blob: a9902bcced662feb05c892268cdc40dbb8eda183 (
plain)
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
|
#include "linkedlist.h"
LinkedList::LinkedList( )
{
pBase = NULL;
pTop = NULL;
pLast = NULL;
nSize = 0;
nLast = -1;
}
LinkedList::~LinkedList( )
{
/*
Link *pCur = pBase;
while( pCur )
{
Link *pLast = pCur;
pCur = pCur->pNext;
delete pLast;
}
*/
empty();
}
void *LinkedList::getAt( int index )
{
if( index < 0 || index >= nSize )
return NULL;
return getPtrTo( index )->pData;
}
void LinkedList::append( void *data )
{
if( pBase == NULL )
{
pBase = new Link( data );
pTop = pBase;
nSize++;
}
else
{
pTop->pNext = new Link( data );
pTop = pTop->pNext;
nSize++;
}
}
void LinkedList::insertBefore( void *data, int pos )
{
if( pos < 0 || pos > nSize )
return;
if( pos == 0 )
{
Link *pTmp = new Link( data, pBase );
if( pBase == NULL )
{
pTop = pTmp;
}
pBase = pTmp;
if( nLast >= 0 ) nLast++;
nSize++;
}
else
{
Link *pCur;
if( (pCur = getPtrTo( pos-1 )) == NULL )
{
return;
}
Link *pNew = new Link( data, pCur->pNext );
pCur->pNext = pNew;
if( pNew->pNext == NULL )
{
pTop = pNew;
}
if( nLast >= pos ) nLast++;
nSize++;
}
}
int LinkedList::getSize( )
{
return nSize;
}
bool LinkedList::isEmpty( )
{
if( nSize == 0 )
return true;
return false;
}
void LinkedList::deleteAt( int index )
{
if( index >= nSize ||
pBase == NULL )
return;
if( index == 0 )
{
Link *pTmp = pBase->pNext;
delete pBase;
pBase = pTmp;
if( nLast >= 0 ) nLast--;
nSize--;
if( pBase == NULL )
{
pTop = NULL;
}
else if( pBase->pNext == NULL )
{
pTop = pBase;
}
}
else
{
Link *pCur = getPtrTo( index-1 );
if( pCur->pNext == pTop )
{
pTop = pCur;
}
Link *pTmp;
if( pCur->pNext == NULL )
{
pTmp = NULL;
}
else
{
pTmp = pCur->pNext->pNext;
}
delete pCur->pNext;
pCur->pNext = pTmp;
if( nLast == index ) nLast = -1;
else if( index < nLast ) nLast--;
nSize--;
}
}
void LinkedList::empty()
{
while( nSize > 0 )
{
deleteAt( 0 );
}
}
void LinkedList::setSize( int newSize )
{
if( newSize < nSize )
{
// Delete items off of the end of the list.
while( nSize > newSize )
{
deleteAt( nSize-1 );
}
}
else
{
// Add null items to the end of the list.
while( nSize < newSize )
{
append( NULL );
}
}
}
void LinkedList::setAt( int index, void *data )
{
if( index >= nSize || index < 0 )
return;
getPtrTo( index )->pData = data;
}
LinkedList::Link *LinkedList::getPtrTo( int index )
{
if( index < 0 || index >= nSize )
return NULL;
if( index == nLast )
{
return pLast;
}
if( index == 0 )
{
pLast = pBase;
nLast = 0;
return pBase;
}
else
{
Link *pCur = pBase;
int nCur = 0;
if( nLast < index && nLast >= 0 )
{
pCur = pLast;
nCur = nLast;
}
while( nCur != index )
{
pCur = pCur->pNext;
nCur++;
}
nLast = index;
pLast = pCur;
return pCur;
}
}
|