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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
// LinkedList.cc for Blackbox - an X11 Window manager
// Copyright (c) 1997 - 1999 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// (See the included file COPYING / GPL-2.0)
//
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif // _GNU_SOURCE
#include "LinkedList.hh"
#include <stdio.h>
__llist_iterator::__llist_iterator(__llist *l) {
// initialize the iterator...
list = l;
reset();
}
void *__llist_iterator::current(void) {
// return the current node data... if any
return ((node) ? node->getData() : 0);
}
void __llist_iterator::reset(void) {
// update the iterator's current node to the first node in the linked list
if (list)
node = list->_first;
}
const int __llist_iterator::set(const int index) {
// set the current node to index
if (list) {
if (index < list->elements && index >= 0 && list->_first) {
node = list->_first;
for (register int i = 0; i < index; i++)
node = node->getNext();
return 1;
}
}
node = (__llist_node *) 0;
return 0;
}
void __llist_iterator::operator++(int) {
// iterate to the next node in the list...
node = ((node) ? node->getNext() : 0);
}
__llist::__llist(void *d) {
// initialize the linked list...
_first = (__llist_node *) 0;
_last = (__llist_node *) 0;
elements = 0;
if (d) insert(d);
}
__llist::~__llist(void) {
// remove all the items in the list...
for (register int i = 0, r = elements; i < r; i++)
remove(0);
}
const int __llist::insert(void *d, int index) {
// insert item into linked list at specified index...
if ((! _first) || (! _last)) {
// list is empty... insert the item as the first item, regardless of the
// index given
_first = new __llist_node;
_first->setData(d);
_first->setNext((__llist_node *) 0);
_last = _first;
} else {
if (index == 0) {
// if index is 0... prepend the data on the list
__llist_node *nnode = new __llist_node;
nnode->setData(d);
nnode->setNext(_first);
_first = nnode;
} else if ((index == -1) || (index == elements)) {
// if index is -1... append the data on the list
__llist_node *nnode = new __llist_node;
nnode->setData(d);
nnode->setNext((__llist_node *) 0);
_last->setNext(nnode);
_last = nnode;
} else if (index < elements) {
// otherwise... insert the item at the position specified by index
__llist_node *nnode = new __llist_node, *inode = _first->getNext();
if (! nnode)
return -1;
nnode->setData(d);
for (register int i = 1; i < index; i++)
if (inode)
inode = inode->getNext();
else {
delete nnode;
return -1;
}
if ((! inode) || inode == _last) {
nnode->setNext((__llist_node *) 0);
_last->setNext(nnode);
_last = nnode;
} else {
nnode->setNext(inode->getNext());
inode->setNext(nnode);
}
}
}
return ++elements;
}
const int __llist::remove(void *d) {
// remove list item whose data pointer address matches the pointer address
// given
if ((! _first) || (! _last))
return -1;
else if (_first->getData() == d) {
// remove the first item in the list...
__llist_node *node = _first;
_first = _first->getNext();
--elements;
delete node;
return 0;
} else {
// iterate through the list and remove the first occurance of the item
// NOTE: we don't validate _first in this assignment, because it is checked
// for validity above...
__llist_node *rnode = _first->getNext(), *prev = _first;
for (register int i = 1; i < elements; i++)
if (rnode)
if (rnode->getData() == d) {
// we found the item... update the previous node and delete the
// now useless rnode...
prev->setNext(rnode->getNext());
if (rnode == _last)
_last = prev;
--elements;
delete rnode;
return i;
} else {
prev = rnode;
rnode = rnode->getNext();
}
return -1;
}
}
void *__llist::remove(const int index) {
if (index >= elements || index < 0 || (! _first) || (! _last))
return (void *) 0;
// remove list item at specified index within the list
if (index == 0) {
// remove the first item in the list...
__llist_node *node = _first;
void *data_return = _first->getData();
_first = _first->getNext();
--elements;
delete node;
return data_return;
} else {
__llist_node *rnode = _first->getNext(), *prev = _first;
void *data_return = (void *) 0;
for (register int i = 1; i < index; i++)
if (rnode) {
prev = rnode;
rnode = rnode->getNext();
} else
return (void *) 0;
if (! rnode) return (void *) 0;
prev->setNext(rnode->getNext());
data_return = rnode->getData();
if (rnode == _last)
_last = prev;
--elements;
data_return = rnode->getData();
delete rnode;
return data_return;
}
return (void *) 0;
}
void *__llist::find(const int index) {
if (index >= elements || index < 0 || (! _first) || (! _last))
return (void *) 0;
if (index == 0) {
// return the first item
return first();
} else if (index == (elements - 1)) {
// return the last item
return last();
} else {
__llist_node *fnode = _first->getNext();
for (register int i = 1; i < index; i++)
if (fnode)
fnode = fnode->getNext();
else
return (void *) 0;
return fnode->getData();
}
return (void *) 0;
}
void *__llist::first(void) {
if (_first)
return _first->getData();
return (void *) 0;
}
void *__llist::last(void) {
if (_last)
return _last->getData();
return (void *) 0;
}
|