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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
/// LinkedList.cc apdated for use in bbtools 23-10-1998 by John Kennis
//
//
// Original notice:
//
// LinkedList.cc for Blackbox - an X11 Window manager
// Copyright (c) 1997, 1998 by Brad Hughes, bhughes@tcac.net
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// (See the included file COPYING / GPL-2.0)
//
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "LinkedList.hh"
// *************************************************************************
// Linked List iterator class code
// *************************************************************************
__llist_iterator::__llist_iterator(__llist *l) {
list = l;
reset();
}
__llist_iterator::~__llist_iterator(void) {
}
void *__llist_iterator::current(void) {
return ((node) ? node->data : 0);
}
void __llist_iterator::reset(void) {
if (list)
node = list->_first;
}
void __llist_iterator::resetLast(void) {
if (list)
node = list->_last;
}
const int __llist_iterator::set(const int index) {
if (list) {
if (index < list->elements && index >= 0) {
node = list->_first;
int i;
for (i = 0; i < index; i++)
node = node->next;
return 1;
}
}
node = (__llist_node *) 0;
return 0;
}
void __llist_iterator::operator++(int) {
node = ((node) ? node->next : 0);
}
void __llist_iterator::operator--(int) {
node = ((node) ? node->prev : 0);
}
// *************************************************************************
// Linked List class code
// *************************************************************************
__llist::__llist(void *d) {
if (! d) {
_first = (__llist_node *) 0;
_last = (__llist_node *) 0;
elements = 0;
} else {
_first = new __llist_node;
_first->data = d;
_first->next = (__llist_node *) 0;
_first->prev = (__llist_node *) 0;
_last = _first;
elements = 1;
}
}
__llist::~__llist(void) {
int i, r = elements;
for (i = 0; i < r; i++)
remove(0);
}
const int __llist::insert(void *d, int index) {
if (! _first) {
_first = new __llist_node;
_first->data = d;
_first->next = (__llist_node *) 0;
_first->prev = (__llist_node *) 0;
_last = _first;
} else {
// if index is -1... append the data to the end of the list
if (index == -1) {
__llist_node *nnode = new __llist_node;
nnode->data = d;
nnode->next = (__llist_node *) 0;
nnode->prev = _last;
_last->next = nnode;
_last = nnode;
} else {
__llist_node *nnode = new __llist_node, *inode = _first;
// otherwise... insert the item at the position specified by index
if (index > elements) return -1;
int i;
for (i = 0; i < index; i++)
inode = inode->next;
if ((! inode) || inode == _last) {
nnode->data = d;
nnode->next = (__llist_node *) 0;
nnode->prev = _last;
_last->next = nnode;
_last = nnode;
} else {
nnode->data = d;
nnode->next = inode->next;
nnode->prev = inode;
inode->next->prev = nnode;
inode->next = nnode;
}
}
}
return ++elements;
}
const int __llist::remove(void *d) {
// remove list item whose data pointer address matches the pointer address
// given
__llist_node *rnode = _first;
int i;
for (i = 0; i < elements; i++) {
if (rnode->data == d) {
if (rnode->prev) {
rnode->prev->next = rnode->next;
if (rnode->next)
rnode->next->prev = rnode->prev;
} else {
// we removed the _first item in the list... reflect that removal in
// the list internals
_first = rnode->next;
if (_first)
_first->prev = (__llist_node *) 0;
}
if (rnode == _last) {
_last = rnode->prev;
if (_last)
_last->next = (__llist_node *) 0;
}
--elements;
delete rnode;
break;
}
if (rnode)
rnode = rnode->next;
else
return -1;
}
return i;
}
void *__llist::remove(const int index) {
if (index < elements && index >= 0) {
// remove list item at specified index within the list
__llist_node *rnode = _first;
void *data_return = 0;
int i;
for (i = 0; i < index; i++)
rnode = rnode->next;
if (rnode->prev) {
rnode->prev->next = rnode->next;
if (rnode->next)
rnode->next->prev = rnode->prev;
} else {
// we removed the _first item in the list... reflect that removal in the
// list internals
_first = rnode->next;
if (_first)
_first->prev = (__llist_node *) 0;
}
if (rnode == _last) {
_last = rnode->prev;
if (_last)
_last->next = (__llist_node *) 0;
}
--elements;
data_return = rnode->data;
delete rnode;
return data_return;
}
return (void *) 0;
}
void *__llist::find(const int index) {
if (index < elements && index >= 0) {
__llist_node *fnode = _first;
int i;
for (i = 0; i < index; i++)
fnode = fnode->next;
return fnode->data;
}
return (void *) 0;
}
|