aboutsummaryrefslogtreecommitdiff
path: root/Src/nde/LinkedList.cpp
blob: 05f4d12275a6e82379e1c464428e7d59aa03a8af (plain) (blame)
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
/* ---------------------------------------------------------------------------
Nullsoft Database Engine
--------------------
codename: Near Death Experience
--------------------------------------------------------------------------- */

/* ---------------------------------------------------------------------------

Double-Linked List Class

Caution: The entire library relies on this base class. Modify with care!

--------------------------------------------------------------------------- */

#include "nde.h"
#include "LinkedList.h"
//---------------------------------------------------------------------------
LinkedListEntry *LinkedListEntry::GetNext(void)  const
{
	return Next;
}

//---------------------------------------------------------------------------
LinkedListEntry *LinkedListEntry::GetPrevious(void)  const
{
	return Previous;
}

//---------------------------------------------------------------------------
LinkedListEntry::LinkedListEntry()
{
	//Int=0;
	Next=NULL;
	Previous=NULL;
}

//---------------------------------------------------------------------------
LinkedListEntry::~LinkedListEntry()
{
}


//---------------------------------------------------------------------------
LinkedList::LinkedList()
{
	Head = NULL;
	Foot = NULL;
	NElements=0;
}

//---------------------------------------------------------------------------
LinkedList::~LinkedList()
{
	while (Head)
		RemoveEntry(Head);
}

//---------------------------------------------------------------------------
void LinkedList::AddEntry(LinkedListEntry *Entry, bool Cat)
{
#ifdef WIN32
	//EnterCriticalSection(&Critical);
#endif
	if (!Cat)
	{
		if (Head)
			Head->Previous = Entry;
		Entry->Next = Head;
		Entry->Previous = NULL;
		Head = Entry;
		if (!Foot)
			Foot = Entry;
	}
	else
	{
		if (Foot)
			Foot->Next = Entry;
		Entry->Previous = Foot;
		Entry->Next = NULL;
		Foot = Entry;
		if (!Head)
			Head = Entry;
	}
	NElements++;
#ifdef WIN32
	//LeaveCriticalSection(&Critical);
#endif
}

//---------------------------------------------------------------------------
void LinkedList::RemoveEntry(LinkedListEntry *Entry)
{
	if (!Entry) return;
#ifdef WIN32
	//EnterCriticalSection(&Critical);
#endif
	if (Entry->Next)
		Entry->Next->Previous = Entry->Previous;
	else
		Foot = Entry->Previous;
	if (Entry->Previous)
		Entry->Previous->Next = Entry->Next;
	else
		Head = Entry->Next;

	if (NElements > 0)
	{
		delete Entry;
		NElements--;
	}
#ifdef WIN32
	//LeaveCriticalSection(&Critical);
#endif
}

//---------------------------------------------------------------------------
void LinkedList::WalkList(WalkListProc WalkProc, int ID, void *Data1, void *Data2)
{
	if (!WalkProc) return;
	LinkedListEntry *Entry = Head;
	while (Entry)
	{
		if (!WalkProc(Entry, ID, Data1, Data2)) break;
		Entry = Entry->Next;
	}
}