Subversion Repositories spk

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
#ifndef LISTS_H
2
#define LISTS_H
3
 
4
#include <stdlib.h>
5
#include "spkdll.h"
6
 
7
template <class LINKCLASS>
8
class CListNode // Node class, containing data and links to next and previous items in list */
9
{
10
protected:
11
	LINKCLASS *m_data;
12
	CListNode<LINKCLASS> *cNext, *cPrev;
13
 
14
public:
15
	CListNode (LINKCLASS *e) { m_data = e; cNext = NULL; cPrev = NULL; }; // Setting the element in a new containter 
16
 
17
	CListNode<LINKCLASS> *next () { return ( cNext ); } // Return Next contanir in list
18
	CListNode<LINKCLASS> *prev () { return ( cPrev ); } // Return Next contanir in list
19
 
20
	LINKCLASS *Data () { return m_data; } // returning the data in the list
21
	void ChangeData ( LINKCLASS *data ) { m_data = data; }
22
	void DeleteData(void) { if ( m_data ) delete m_data; m_data = NULL; } // deletes the current data
23
 
24
	void setNext( CListNode<LINKCLASS> *node ) { cNext = node; } // Setting the Next item in list
25
	void setPrev( CListNode<LINKCLASS> *node ) { cPrev = node; } // Setting the previous item in list
26
	void delNext() { cNext = NULL; } // Removing next Node
27
	void delPrev() { cPrev = NULL; } // Removing prev Node
28
};
29
 
30
 
31
/*
32
	######################################################################################
33
	############################## Double Linked List Functions ##########################
34
	######################################################################################
35
*/
36
 
37
#ifdef _SPKDLL
38
template <class LINKCLASS>
39
class __declspec(dllexport) CLinkList  /* Main list Class */
40
#else
41
template <class LINKCLASS>
42
class CLinkList  /* Main list Class */
43
#endif
44
{
45
private:
46
	int elements;
47
	CListNode<LINKCLASS> *m_pBack, *m_pFront, *m_pCurrent, *m_pItr;
48
	bool m_bCurrentStart;
49
 
50
public:
51
	CLinkList() { // default constructor, creats an empty list
52
		elements = 0; 
53
		m_pFront = NULL;
54
		m_pBack = NULL;
55
		m_pCurrent = NULL;
56
		m_pItr = NULL;
57
		m_bCurrentStart = false;
58
	}
59
 
60
	// Create n copies each with element of e
61
	CLinkList(int n, LINKCLASS *e)
62
	{ 
63
		int i;
64
		CListNode<LINKCLASS> *newNode;
65
		CListNode<LINKCLASS> *tmpNode;
66
 
67
		for ( i = 0; i < n; i++ )
68
		{
69
			newNode = new CListNode<LINKCLASS>( e ); // create new node
70
			tmpNode = m_pBack;  // assign last node added to tmp value
71
			m_pBack = newNode; // assign new node to back of list
72
			if ( i == 0 )  // if first node, add as front node
73
				m_pFront = newNode; 
74
			else
75
			{
76
				newNode->setPrev ( tmpNode );  // set new nodes previous pointer to last created node
77
				tmpNode->setNext ( newNode );  // set last created nodes next pointer to new node
78
			}
79
		}
80
 
81
		elements = n; // set elements to number of elements added
82
	}
83
 
84
 
85
	~CLinkList() { this->destroy(); } // Deconstructer, delete current list
86
 
9 cycrow 87
	virtual void clear(bool deletedata = false) { this->destroy(deletedata); }  // clears the list
1 cycrow 88
	void destroy(bool deletedata = false)
89
	{
90
		CListNode<LINKCLASS> *curNode, *tmpNode;
91
 
92
		curNode = m_pFront; // set current node as the first node in list
93
		while( curNode != NULL ) // loop until no nodes remain
94
		{
95
			tmpNode = curNode->next(); // set temp node as the next node in list
96
			if ( deletedata && curNode->Data() )
97
				delete curNode->Data();
98
			delete curNode; // delete current Node
99
 
100
			curNode = tmpNode; // change current node to next in list
101
		} 
102
 
103
		m_pFront = NULL;
104
		m_pBack = NULL;
105
		elements = 0;
106
	}
107
 
108
	void ResetIterate () { m_pItr = NULL; }
109
	CListNode<LINKCLASS> *Iterate () 
110
	{ 
111
		if ( !m_pItr ) 
112
			m_pItr = m_pFront; 
113
		else 
114
			m_pItr = m_pItr->next(); 
115
		return m_pItr; 
116
	}
117
	LINKCLASS *FoundIterate () { if ( !m_pItr ) return NULL; LINKCLASS *data = m_pItr->Data(); m_pItr = NULL; return data; }
118
	LINKCLASS *IterateData () 
119
	{ 
120
		if ( !m_pItr ) 
121
			return NULL; 
122
		return m_pItr->Data(); 
123
	}
10 cycrow 124
	CListNode<LINKCLASS> *CurrentNode () const { if ( m_bCurrentStart ) return m_pFront; return m_pCurrent; }
125
	LINKCLASS *CurrentData () const { if ( (m_bCurrentStart) && (m_pFront) ) return m_pFront->Data(); if ( (m_pCurrent) && (!m_bCurrentStart) ) return m_pCurrent->Data(); return NULL; }
1 cycrow 126
	CListNode<LINKCLASS> *SetCurrentFront () { m_pCurrent = m_pFront; m_bCurrentStart = false; return m_pCurrent; }
127
	CListNode<LINKCLASS> *SetCurrentBack () { m_pCurrent = m_pBack; m_bCurrentStart = false; return m_pCurrent; }
128
	CListNode<LINKCLASS> *MoveCurrentNext () { if ( m_bCurrentStart ) { m_pCurrent = m_pFront; m_bCurrentStart = false; } else if ( m_pCurrent ) m_pCurrent = m_pCurrent->next(); return m_pCurrent; }
129
	CListNode<LINKCLASS> *MoveCurrentPrev () { if ( m_pCurrent ) m_pCurrent = m_pCurrent->prev(); return m_pCurrent; }
130
	void RemoveCurrent () 
131
	{ 
132
		m_bCurrentStart = false;
133
 
134
		CListNode<LINKCLASS> *tmp = m_pCurrent->prev(); 
135
		this->remove ( m_pCurrent ); 
136
		m_pCurrent = tmp; 
137
		if ( !m_pCurrent ) 
138
		{
139
			m_pCurrent = m_pFront; 
140
			m_bCurrentStart = true; 
141
		}
142
	}
143
	void RemoveIterate ( bool back = false ) 
144
	{ 
145
		CListNode<LINKCLASS> *node = m_pItr->prev();
146
		if ( m_pItr ) 
147
			this->remove ( m_pItr ); 
148
 
149
		if ( back )
150
		{
151
			m_pItr = node;
152
			if ( !node )
153
				m_pItr = m_pFront;
154
		}
155
		else
156
			m_pItr = NULL; 
157
	}
158
 
9 cycrow 159
	int size () const { return elements; } // return the num of elements in list
1 cycrow 160
	void incElement () { elements++; } // Increment elements, used when new node is added
161
	void decElement () { elements--; } // Decrement elements, used when new node is deleted
162
 
10 cycrow 163
	CListNode<LINKCLASS> *Front () const { return ( m_pFront ); } // return the first node in the list
164
	CListNode<LINKCLASS> *Back () const { return ( m_pBack ); } // return the last node in the list
1 cycrow 165
	LINKCLASS *First () { SetCurrentFront(); if ( m_pCurrent ) return ( m_pCurrent->Data() ); return ( NULL ); } // return the first node in the list
166
	LINKCLASS *Last () { SetCurrentBack(); if ( m_pCurrent ) return ( m_pCurrent->Data() ); return (NULL ); } // return the last node in the list
167
	LINKCLASS *Next () { if ( MoveCurrentNext() ) return m_pCurrent->Data(); return NULL; }
168
	LINKCLASS *Prev () { if ( MoveCurrentPrev() ) return m_pCurrent->Data(); return NULL; }
169
 
170
	void RemoveEmpty() // removed empty data
171
	{
172
		CListNode<LINKCLASS> *curNode, *nextNode;
173
 
174
		curNode = m_pFront; // set node to first node in list
175
		while ( curNode != NULL ) // loop until past last node
176
		{
177
			nextNode = curNode->next();
178
 
179
			// check for data and remove
180
			if ( !curNode->Data() )
181
			{
182
				// set current to previous
183
				if ( curNode->prev() )
184
				{
185
					if ( nextNode )
186
						nextNode->setPrev(curNode->prev());
187
					curNode->prev()->setNext(nextNode);
188
				}
189
				// otherwise we are removing the head node
190
				else
191
				{
192
					m_pFront = nextNode;
193
					if ( nextNode )
194
						nextNode->delPrev();
195
				}
196
 
197
				// no next node, set the back to the prevois
198
				if ( !nextNode )
199
				{
200
					m_pBack = curNode->prev();
201
					if ( m_pBack )
202
						m_pBack->delNext();
203
				}
204
 
205
				delete curNode;
206
				--elements;
207
			}
208
 
209
			curNode = nextNode;
210
		}
211
	}
212
 
10 cycrow 213
	bool empty() const { // return true is the list is empty, otherwise return false
1 cycrow 214
		if ( (elements <= 0) || (!m_pFront) ) 
215
			return true;
216
		else
217
			return false;
218
	}
219
 
220
	void ChangeCurrent ( LINKCLASS *e ) 
221
	{ 
222
		if ( m_bCurrentStart )
223
		{
224
			if ( m_pFront )
225
				m_pFront->ChangeData ( e );
226
			return;
227
		}
228
		if ( !m_pCurrent ) return; 
229
		m_pCurrent->ChangeData ( e ); 
230
	}
231
 
232
	// Add to front of the list
233
	LINKCLASS *push_front (LINKCLASS *e)
234
	{
235
		CListNode<LINKCLASS> *newNode;
236
		CListNode<LINKCLASS> *tmpNode;
237
 
238
		newNode = new CListNode<LINKCLASS>( e );
239
 
240
		// place the node in the list
241
		if( m_pFront == NULL ) // list is empty
242
		{
243
			m_pFront = newNode; 
244
			m_pBack = newNode;
245
		} 
246
		else // list already has something in
247
		{
248
			tmpNode = m_pFront;
249
			m_pFront->setPrev( newNode ); // set old first node to point to new node
250
			m_pFront = newNode; // set new node as first item
251
			newNode->setNext ( tmpNode ); // Set new nodes next as original front node
252
		}
253
		this->incElement(); // Increment element numbers
254
 
255
		return e;
256
	}
257
 
258
	// Add to end of the list
259
	LINKCLASS *push_back (LINKCLASS *e)
260
	{
261
		CListNode<LINKCLASS> *newNode;
262
		CListNode<LINKCLASS> *tmpNode;
263
 
264
		newNode = new CListNode<LINKCLASS> ( e );
265
 
266
		// place the node in the list
267
		if( m_pFront == NULL ) // list is empty
268
		{
269
			m_pFront = newNode; 
270
			m_pBack = newNode;
271
		} 
272
		else // list already has something in
273
		{
274
			tmpNode = m_pBack;
275
			m_pBack->setNext( newNode ); // set old last node to point to new node
276
			m_pBack = newNode; // set new node as last item
277
			newNode->setPrev ( tmpNode ); // Set new nodes previous as original last node
278
		}
279
		this->incElement(); // Increment element numbers
280
 
281
		return e;
282
	}
283
 
284
	// remove node from the beginning of the list
285
	void pop_front ()
286
	{
287
		CListNode<LINKCLASS> *tmpNode;
288
 
289
		m_pFront->next()->delPrev(); 
290
		tmpNode = m_pFront->next();
291
		delete m_pFront; // remove the first node from memory
292
		m_pFront = tmpNode; // set new m_pFront Node
293
		this->decElement(); // decrement number of elements
294
	}
295
 
296
	// remove node from the end of the list
297
	void pop_back ()
298
	{
299
		CListNode<LINKCLASS> *tmpNode;
300
 
301
		if ( m_pBack->prev() )
302
			m_pBack->prev()->delNext(); 
303
		tmpNode = m_pBack->prev();
304
		if ( m_pBack == m_pFront )
305
			m_pFront = NULL;
306
		delete m_pBack; // remove the last node from memory
307
		m_pBack = tmpNode; // set new m_pBack Node
308
		this->decElement(); // decrement number of elements
309
	}
310
 
311
	// Creating new list and put elements in to start with
312
	void assign (int n, LINKCLASS *e)
313
	{
314
		int i;
315
		CListNode<LINKCLASS> *newNode, *tmpNode, *curNode;
316
 
317
		// Delete existiing nodes from the list
318
		curNode = m_pFront; // set current node as the first node in list
319
		while( curNode != NULL ) // loop until no nodes remain
320
		{
321
			tmpNode = curNode->next(); // set temp node as the next node in list
322
			delete curNode; // delete current Node
323
 
324
			curNode = tmpNode; // change current node to next in list
325
		} 
326
		// Add new nodes into the now empty list
327
		for ( i = 0; i < n; i++ )
328
		{
329
			newNode = new CListNode<LINKCLASS>( e ); // create new node
330
			tmpNode = m_pBack;  // assign last node added to tmp value
331
			m_pBack = newNode; // assign new node to back of list
332
			if ( i == 0 )  // if first node, add as front node
333
				m_pFront = newNode; 
334
			else
335
			{
336
				newNode->setPrev ( tmpNode );  // set new nodes previous pointer to last created node
337
				tmpNode->setNext ( newNode );  // set last created nodes next pointer to new node
338
			}
339
		}
340
 
341
		elements = n; // set elements to number of elements added
342
	}
343
 
344
	CListNode<LINKCLASS> *insert ( LINKCLASS *find, LINKCLASS *e )
345
	{
346
		int pos = FindPos ( find );
347
		if ( pos >= 0 )
348
			return insert ( pos + 1, e );
349
		return NULL;
350
	}
351
 
352
	// inserts new node of element "e" into the list in position of "pos"
353
	CListNode<LINKCLASS> *insert (int pos, LINKCLASS *e)
354
	{ // cant add outside of the list,
355
		int i, max;
356
		CListNode<LINKCLASS> *tmpNode, *newNode;
357
 
358
		if ( pos > (elements + 1) ) // element is out side of the list
359
			return NULL;
360
 
361
		newNode = new CListNode<LINKCLASS>( e ); // create new node
362
 
363
		tmpNode = m_pFront;
364
 
365
		max = pos;
366
		if ( max > elements ) // make sure it doesn't loop past the number of elements, so it doesn't try to access a point that doesn't exist
367
			max = elements ;
368
 
369
		for ( i = 1; i < max; i++ ) // get to position to insert
370
			tmpNode = tmpNode->next();
371
 
372
		if ( (pos != 1) && (pos < (elements + 1)) ) // beginning pos doesn't have previous, end pos has different values
373
		{ 
374
			newNode->setPrev( tmpNode->prev() );
375
			tmpNode->prev()->setNext( newNode );
376
		}
377
		if ( pos < (elements + 1) ) // end node doesn't have next
378
		{
379
			newNode->setNext( tmpNode );
380
			tmpNode->setPrev( newNode );
381
		}
382
		if ( pos == (elements + 1) ) // inserting at the end
383
		{
384
			tmpNode->setNext ( newNode );
385
			newNode->setPrev ( tmpNode );
386
			m_pBack = newNode;
387
		}
388
		if ( pos == 1 ) // inserting at the beginning
389
			m_pFront = newNode;
390
 
391
		elements++;
392
 
393
		return newNode;
394
	}
395
 
396
	// removes the node at element "pos"
397
	CListNode<LINKCLASS> *erase (int pos, bool del = false )
398
	{ // cant erase node outside of list
399
		int i;
400
		CListNode<LINKCLASS> *tmpNode, *returnNode;
401
 
402
		if ( pos > elements ) // element is out side of the list
403
			return NULL;
404
 
405
		tmpNode = m_pFront;
406
 
407
		for ( i = 1; i < pos; i++ ) // get to position to insert
408
			tmpNode = tmpNode->next();
409
 
410
		if ( tmpNode->prev() )
411
			tmpNode->prev()->setNext( tmpNode->next() );
412
		if ( tmpNode->next() )
413
			tmpNode->next()->setPrev( tmpNode->prev() );
414
 
415
		if ( pos == 1 )
416
		{
417
			tmpNode->delPrev();
418
			m_pFront = tmpNode->next();
419
		}
420
		if ( pos == elements )
421
		{
422
			if ( tmpNode->prev() )
423
				tmpNode->prev()->delNext();
424
			m_pBack = tmpNode->prev();
425
			returnNode = tmpNode->prev();
426
		}
427
		else
428
			returnNode = tmpNode->next();
429
		elements--;
430
		if ( del )
431
			tmpNode->DeleteData();
432
		delete tmpNode;
433
 
434
		return returnNode;
435
	}
436
 
437
	// remove the node
438
	void remove ( CListNode<LINKCLASS> *node, bool del = true )
439
	{
440
		if ( !node )
441
			return;
442
 
443
		// is there a next node ?
444
		if ( node->next() )
445
			node->next()->setPrev ( node->prev() );
446
		else if ( m_pBack == node )
447
			m_pBack = node->prev();
448
 
449
		if ( node->prev() )
450
			node->prev()->setNext ( node->next() );
451
		else if ( m_pFront == node )
452
			m_pFront = node->next();
453
 
454
		if ( del )
455
			delete node;
456
	}
457
 
458
	// removes all elements that has the element of "e"
459
	void remove (LINKCLASS *e, bool single = false, bool del = true)
460
	{
461
		CListNode<LINKCLASS> *tmpNode, *tmpNode2, *nextNode;
462
 
463
		tmpNode = m_pFront; // set node to first node in list
464
		while ( tmpNode != NULL ) // loop until psat last node
465
		{
466
			nextNode = tmpNode->next(); // set the next noe to look at
467
			if ( tmpNode->Data() == e ) // remove node
468
			{
469
				if ( (tmpNode == m_pFront) && (tmpNode == m_pBack) ) // only node in list, list will be empty
470
				{
471
					m_pFront = NULL; 
472
					m_pBack = NULL;
473
				}
474
				else if ( tmpNode == m_pFront ) // if front node, delete next nodes previous value
475
				{
476
					tmpNode->next()->delPrev();
477
					m_pFront = tmpNode->next();
478
				}
479
				else if ( tmpNode == m_pBack ) // if back node, delete previous nodes next value, and set back value
480
				{
481
					tmpNode->prev()->delNext();
482
					m_pBack = tmpNode->prev();
483
				}
484
				else // node is somewhere in the middle
485
				{
486
					tmpNode2 = tmpNode->next();
487
					tmpNode->next()->setPrev(tmpNode->prev());
488
					tmpNode->prev()->setNext(tmpNode2);
489
				}
490
				if ( del ) delete tmpNode; // delete node form memory
491
				elements--; // decrement elements
492
				if ( single ) break;
493
			}
494
			tmpNode = nextNode; // assign new tmpnode for loop
495
		}
496
	}
497
 
498
	CLinkList<LINKCLASS> operator=(CLinkList& list)
499
	{
500
		int i;
501
		CListNode<LINKCLASS> *oldNode, *newNode;
502
 
503
		this->destroy(true); // removes the original list
504
 
505
		oldNode = list.Front();
506
		for ( i = 1; i <= list.size(); i++ ) // loop for each node in the list
507
		{
508
			newNode = new CListNode<LINKCLASS> ( oldNode->Data() ); // creating new node with old nodes element
509
			if ( i == 1 )
510
				m_pFront = newNode;
511
			else
512
			{
513
				m_pBack->setNext( newNode );
514
				newNode->setPrev( m_pBack );
515
			}
516
			m_pBack = newNode; // set newly created node as the last one in list
517
			oldNode = oldNode->next();
518
		}
519
		elements = list.size();
520
 
521
		return (*this);
522
	}
523
 
524
	LINKCLASS *operator[](int pos) { return Get(pos); }
525
 
10 cycrow 526
	LINKCLASS *Get ( int id ) const
1 cycrow 527
	{
528
		int i = 0;
529
		if ( !m_pFront )
530
			return NULL;
531
 
532
		CListNode<LINKCLASS> *tmpNode = m_pFront;
533
		while ( tmpNode )
534
		{
535
			if ( id == i )
536
				return tmpNode->Data();
537
			tmpNode = tmpNode->next();
538
			++i;
539
		}
540
		return NULL;
541
	}
542
 
10 cycrow 543
	CListNode<LINKCLASS> *GetNode ( int id ) const
1 cycrow 544
	{
545
		int i = 0;
546
		if ( !m_pFront )
547
			return NULL;
548
 
549
		CListNode<LINKCLASS> *tmpNode = m_pFront;
550
		while ( tmpNode )
551
		{
552
			if ( id == i )
553
				return tmpNode;
554
			tmpNode = tmpNode->next();
555
			++i;
556
		}
557
		return NULL;
558
	}
559
 
560
	void Switch ( LINKCLASS *e, LINKCLASS *n )
561
	{
562
		CListNode<LINKCLASS> *node = m_pFront;
563
		while ( node )
564
		{
565
			if ( node->Data() == e )
566
				node->ChangeData ( n );
567
			node = node->next();
568
		}
569
	}
570
 
10 cycrow 571
	bool FindData ( LINKCLASS *e ) const
1 cycrow 572
	{
573
		CListNode<LINKCLASS> *node = m_pFront;
574
		while ( node )
575
		{
576
			if ( node->Data() == e )
577
				return true;
578
			node = node->next();
579
		}
580
		return false;
581
	}
582
 
10 cycrow 583
	int FindPos ( LINKCLASS *e ) const
1 cycrow 584
	{
585
		int pos = 0;
586
		CListNode<LINKCLASS> *node = m_pFront;
587
		while ( node )
588
		{
589
			if ( node->Data() == e )
590
				return pos;
591
			node = node->next();
592
			++pos;
593
		}
594
		return -1;
595
	}
596
 
597
	void MemoryClear()
598
	{
599
		CListNode<LINKCLASS> *node = m_pFront;
600
		while ( node )
601
		{
602
			if ( node->Data() )
603
				delete node->Data();
604
			node->ChangeData(NULL);
605
			node = node->next();
606
		}
607
 
608
		this->destroy();
609
	}
610
};
611
 
612
/*
613
    #####################################################################################################
614
	###################################### End Linked List Class ########################################
615
	#####################################################################################################
616
*/
617
 
618
#endif