2
// Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
4
// This file may be distributed under terms of the GPL
18
CVSID_DOT_H(LLIST_H_CVSID);
20
LList::LNode::LNode( void *data ){
30
for ( int i = 0 ; i < MAXCURR ; i++ )
35
LList::LList( int ( *cmp_fun )( void *data, void *key ) ){
39
for ( int i = 0 ; i < MAXCURR ; i++ )
44
LList::~LList( void ){
49
int LList::push( void *data ){
51
top_ = new LNode( data );
52
if ( top_ == NULL ) return ( 0 );
58
btm_->next_ = new LNode( data );
59
if ( btm_->next_ == NULL ) return ( 0 );
61
btm_->next_->prev_ = btm_;
66
void *LList::pop( void ){
70
if ( !n_ ) return ( NULL );
83
btm_->prev_->next_ = NULL;
89
void *LList::dequeue( void ){
93
if ( !n_ ) return ( NULL );
107
top_->next_->prev_ = NULL;
113
int LList::insert( void *data, void *key ){
114
LNode *current, *temp;
120
if ( ( top_ = new LNode( data ) ) == NULL ) return ( 0 );
126
while ( ( cmp_fun_( current->data_, key ) < 0 ) &&
127
( current->next_ != NULL ) )
128
current = current->next_;
130
if ( ( temp = new LNode( data ) ) == NULL ) return ( 0 );
134
/* Add To End of List */
135
if ( ( current->next_ == NULL ) &&
136
( cmp_fun_( current->data_, key ) < 0 ) ) {
138
current->next_ = temp;
143
/* Add To Top of List */
144
if ( ( current->prev_ == NULL ) &&
145
( cmp_fun_( current->data_, key ) > 0 ) ) {
146
temp->next_ = current;
147
current->prev_ = temp;
153
temp->next_ = current;
154
temp->prev_ = current->prev_;
155
current->prev_->next_ = temp;
156
current->prev_ = temp;
160
void *LList::find( void *key ){
163
temp = findnode( key );
164
if ( temp == NULL ) return ( NULL );
165
return ( temp->data_ );
168
void *LList::removematch( void *key ){
171
ptr = findnode( key );
173
if ( ptr == NULL ) return ( NULL );
175
return ( deletenode( ptr ) );
178
int LList::putontop( void *data ){
181
if ( ( buff = new LNode( data ) ) == NULL ) return ( 0 );
190
void LList::remove( void *data ){
195
while ( (!found) && (tmp != NULL) ) {
196
if ( tmp->data_ == data ) found = 1;
197
if ( !found ) tmp = tmp->next_;
200
if ( (tmp == NULL) || (!found) ) return;
205
void *LList::findn( int n ){
208
temp = findnnode( n );
209
if ( temp == NULL ) return ( NULL );
210
return ( temp->data_ );
213
int LList::index( void *data ){
218
while ( tmp != NULL ) {
219
if ( tmp->data_ == data ) return ( a );
226
void LList::setc( int n, int which ){
227
curr_[which] = findnnode( n );
230
void LList::incc( int which ){
231
if ( curr_[which] != NULL ) curr_[which] = curr_[which]->next_;
234
void LList::decc( int which ){
235
if ( curr_[which] != NULL ) curr_[which] = curr_[which]->prev_;
238
void *LList::findc( int which ){
239
if ( curr_[which] == NULL ) return ( NULL );
241
return ( curr_[which]->data_ );
244
void LList::save( int size, FILE *fp ){
248
fwrite( &n_, sizeof( int ), 1, fp ); /* save n */
251
for ( i = 1 ; i <= n_ ; i ++ ) {
253
fwrite ( buf, size, 1, fp );
258
int LList::restore( int size, FILE *fp ){
262
fread ( &i, sizeof ( int ), 1, fp );
264
for ( ; i > 0 ; i-- ) {
265
if ( ( buf = new char[size] ) == NULL ) return ( 0 );
266
if ( ! push( buf ) ) return ( 0 );
272
void LList::kill( void ){
282
LList::LNode *LList::findnode( void *key ){
287
if ( current == NULL ) return ( NULL );
289
while ( ( cmp_fun_( current->data_, key ) ) &&
290
( current != NULL ) )
291
current = current->next_;
293
if ( current == NULL ) return ( NULL );
298
void *LList::deletenode( LNode *ptr ){
301
if ( ( top_ == NULL ) || ( ptr == NULL ) ) return ( NULL );
313
if ( ptr->prev_ == NULL ) {
321
if ( ptr->next_ == NULL ) {
329
ptr->prev_->next_ = ptr->next_;
330
ptr->next_->prev_ = ptr->prev_;
336
LList::LNode *LList::findnnode( int i ){
340
if ( (i > n_) || (i < 1) ) return ( NULL );
344
for ( j = 1 ; j < i ; j++ ) current = current->next_;
349
for ( j = n_ ; j > i ; j-- ) current = current->prev_;