3 * kkconsui linkedlist class
4 * $Id: linkedlist.cc,v 1.2 2001/06/03 21:12:05 konst Exp $
6 * Copyright (C) 1999-2001 by Konstantin Klyagin <konst@konst.org.ua>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 #include "linkedlist.h"
27 linkedlist::linkedlist(): count(0), flist(0), freeitem(0) {
30 linkedlist::~linkedlist() {
32 // if(flist) delete flist;
35 void linkedlist::add(void *p
) {
36 flinkedlist
*l
= flist
, *k
= new flinkedlist
;
41 if(!count
) flist
= k
; else {
42 for(i
= 0; i
< count
-1; i
++, l
= l
->next
);
49 void linkedlist::insert(int n
, void *p
) {
51 flinkedlist
*l
= flist
, *k
= new flinkedlist
;
53 for(i
= 0; i
< n
-1; i
++, l
= l
->next
);
65 void linkedlist::remove(int n
) {
66 flinkedlist
*l
= flist
, *pr
;
68 if((n
< count
) && (n
>= 0)) {
71 if(freeitem
) freeitem(l
->data
); else free(l
->data
);
74 for(pr
= 0, i
= 0; i
< n
; i
++, l
= l
->next
) {
80 if(freeitem
) freeitem(l
->data
); else free(l
->data
);
89 void linkedlist::empty() {
92 for(i
= 0, l
= flist
; i
< count
&& l
; i
++) {
95 if(freeitem
) freeitem(p
->data
); else delete (p
->data
);
102 void* linkedlist::at(int n
) {
103 flinkedlist
*l
= flist
;
105 if((n
< count
) && (n
>= 0) && l
) {
106 for(i
= 0; i
< n
; i
++, l
= l
->next
);
113 void* linkedlist::find(void *p
, listcompare
*compare
) {
116 for(i
= 0; i
< count
; i
++) {
118 if(!compare(p
, l
)) return l
;
124 int linkedlist::findnum(void *p
, listcompare
*compare
) {
127 for(i
= 0; i
< count
; i
++) {
129 if(!compare(p
, l
)) return i
;
135 void linkedlist::sort(listcompare
*compare
) {
140 for(i
= 0; i
< count
; i
++)
141 for(f
= flist
; f
&& f
->next
; f
= f
->next
) {
142 if(compare(f
->next
->data
, f
->data
) > 0) {
144 f
->data
= f
->next
->data
;
145 f
->next
->data
= tempdata
;
150 void linkedlist::replace(int n
, void *p
) {
151 flinkedlist
*l
= flist
;
154 for(i
= 0; i
< n
; i
++, l
= l
->next
);
156 if(freeitem
) freeitem(l
->data
); else free(l
->data
);
162 void *linkedlist::foreach(listforeachfunc
*exec
, void *arg
) {
163 flinkedlist
*l
= flist
;
166 for(; !ret
&& l
; l
= l
->next
) {
167 ret
= exec(l
->data
, arg
);