summaryrefslogtreecommitdiff
path: root/public/mxtk/mxlinkedlist.h
blob: c87362bc76abbb0a05dc0d7eb0f5503fd3a97c53 (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
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
//
//                 mxToolKit (c) 1999 by Mete Ciragan
//
// file:           mxLinkedList.h
// implementation: all
// last modified:  Mar 19 1999, Mete Ciragan
// copyright:      The programs and associated files contained in this
//                 distribution were developed by Mete Ciragan. The programs
//                 are not in the public domain, but they are freely
//                 distributable without licensing fees. These programs are
//                 provided without guarantee or warrantee expressed or
//                 implied.
//
#ifndef INCLUDED_MXLINKEDLIST
#define INCLUDED_MXLINKEDLIST



typedef struct mxListNode_s
{
	void *d_data;
	struct mxListNode_s *d_next;
	struct mxListNode_s *d_prev;
} mxListNode;



class mxLinkedList
{
	mxListNode *d_head;
	mxListNode *d_tail;
	int d_nodeCount;

	// NOT IMPLEMENTED
	mxLinkedList (const mxLinkedList&);
	mxLinkedList& operator= (const mxLinkedList&);

public:
	//CREATORS
	mxLinkedList ()
	{
		d_head = new mxListNode;
		d_tail = new mxListNode;
		d_head->d_data = 0;
		d_head->d_next = d_tail;
		d_head->d_prev = 0;
		d_tail->d_data = 0;
		d_tail->d_next = 0;
		d_tail->d_prev = d_head;
		d_nodeCount = 0;
	}

	~mxLinkedList ()
	{
		removeAll ();
		delete d_tail;
		delete d_head;
	}

	// MANIPULATORS
	void add (void *data)
	{
		mxListNode *node = new mxListNode;
		node->d_data = data;
		d_tail->d_prev->d_next = node;
		node->d_prev = d_tail->d_prev;
		node->d_next = d_tail;
		d_tail->d_prev = node;
		++d_nodeCount;
	}

	void remove (void *data)
	{
		mxListNode *node = d_head->d_next;
		while (node != d_tail)
		{
			mxListNode *next = node->d_next;
			if (node->d_data == data)
			{
				node->d_prev->d_next = node->d_next;
				node->d_next->d_prev = node->d_prev;
				delete node;
			}

			node = next;
		}
		--d_nodeCount;
	}

	void removeAll ()
	{
		mxListNode *node = d_head->d_next;

		while (node != d_tail)
		{
			mxListNode *next = node->d_next;
			delete node;
			node = next;
		}

		d_head->d_next = d_tail;
		d_tail->d_prev = d_head;
		d_nodeCount = 0;
	}

	void setData (mxListNode *node, void *data)
	{
		if (node)
			node->d_data = data;
	}

	// ACCESSORS
	void *getData (mxListNode *node) const
	{
		if (node)
			return node->d_data;

		return 0;
	}

	mxListNode *getFirst () const
	{
		if (d_head->d_next != d_tail)
			return d_head->d_next;

		return 0;
	}

	mxListNode *getNext (mxListNode *node) const
	{
		if (node)
		{
			if (node->d_next != d_tail)
				return node->d_next;

			return 0;
		}

		return 0;
	}

	mxListNode *getLast () const
	{
		if (d_tail->d_prev != d_head)
			return d_tail->d_prev;

		return 0;
	}

	mxListNode *getPrev (mxListNode *node) const
	{
		if (node)
		{
			if (node->d_prev != d_head)
				return node->d_prev;

			return 0;
		}

		return 0;
	}

	mxListNode *at (int pos) const
	{
		mxListNode *node = d_head->d_next;
		while (pos > 0 && node != d_tail)
		{
			pos--;
			node = node->d_next;
		}

		if (node != d_tail)
			return node;

		return 0;
	}

	bool isEmpty () const
	{
		return (d_head->d_next == d_tail);
	}

	int getNodeCount () const
	{
		return d_nodeCount;
	}
};



#endif // INCLUDED_MXLINKEDLIST