2004-02-11 02:58:36 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "List.h"
|
|
|
|
|
|
|
|
void MakeList ( List *mylist, short (*Compare) ( const Element Data1, const Element Data2 ) ) {
|
|
|
|
mylist->RootNode = malloc ( sizeof( Node ) );
|
|
|
|
if ( mylist->RootNode == NULL )
|
2005-09-01 18:01:20 -04:00
|
|
|
return; /* out of memory */
|
2004-02-11 02:58:36 -05:00
|
|
|
mylist->Compare = Compare;
|
|
|
|
mylist->RootNode->Previous = NULL;
|
|
|
|
mylist->RootNode->Next = NULL;
|
|
|
|
mylist->RootNode->NodeElement = NULL;
|
|
|
|
mylist->size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *Find ( List *mylist, Element Data ) {
|
|
|
|
Node *n;
|
|
|
|
n = mylist->RootNode->Next;
|
|
|
|
while ( n != mylist->RootNode ) {
|
|
|
|
if ( mylist->Compare ( n->NodeElement, Data ) == 0 )
|
|
|
|
return n;
|
|
|
|
n = n->Next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *Get ( List *mylist, unsigned short index ) {
|
|
|
|
unsigned short i, halfsize;
|
|
|
|
Node *GetPtr;
|
|
|
|
if ( index > mylist->size )
|
|
|
|
return (Node *)NULL;
|
|
|
|
halfsize = mylist->size / 2;
|
|
|
|
if ( index > halfsize ) {
|
|
|
|
GetPtr = mylist->RootNode->Previous;
|
|
|
|
for ( i = 0; i < index; i++ )
|
|
|
|
GetPtr = GetPtr->Previous;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
GetPtr = mylist->RootNode->Next;
|
|
|
|
for ( i = 0; i < index; i++ )
|
|
|
|
GetPtr = GetPtr->Next;
|
|
|
|
}
|
|
|
|
return GetPtr;
|
|
|
|
}
|
|
|
|
|
2005-09-01 18:01:20 -04:00
|
|
|
short InsertAt ( List *mylist, Element Data, Node *Pos ) {
|
2004-02-11 02:58:36 -05:00
|
|
|
Node *New;
|
|
|
|
New = malloc ( sizeof( Node ) );
|
|
|
|
if ( New == NULL )
|
2005-09-01 18:01:20 -04:00
|
|
|
return 0;
|
|
|
|
|
2004-02-11 02:58:36 -05:00
|
|
|
New->NodeElement = Data;
|
|
|
|
|
|
|
|
if ( mylist->size == 0 ) {
|
|
|
|
New->Previous = mylist->RootNode;
|
|
|
|
New->Next = mylist->RootNode;
|
|
|
|
mylist->RootNode->Previous = New;
|
|
|
|
mylist->RootNode->Next = New;
|
|
|
|
mylist->size++;
|
2005-09-01 18:01:20 -04:00
|
|
|
return 1;
|
2004-02-11 02:58:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mylist->size++;
|
|
|
|
New->Next = Pos;
|
|
|
|
|
|
|
|
if ( Pos != NULL ) {
|
|
|
|
New->Previous = Pos->Previous;
|
|
|
|
Pos->Previous = New;
|
|
|
|
}
|
|
|
|
if ( New->Previous != NULL )
|
|
|
|
New->Previous->Next = New;
|
2005-09-01 18:01:20 -04:00
|
|
|
return 1;
|
2004-02-11 02:58:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
short IsEmpty ( List *mylist ) {
|
|
|
|
return mylist->size == 0;
|
|
|
|
}
|
|
|
|
|
2005-09-01 18:01:20 -04:00
|
|
|
short Push ( List *mylist, Element Data ) {
|
|
|
|
return InsertAt ( mylist, Data, mylist->RootNode->Next );
|
2004-02-11 02:58:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Element Pop ( List *mylist ) {
|
|
|
|
Node *Return, *Next;
|
|
|
|
Element data;
|
|
|
|
Return = mylist->RootNode->Next;
|
|
|
|
if ( Return == NULL )
|
|
|
|
return (Element)NULL;
|
|
|
|
Next = Return->Next;
|
|
|
|
mylist->RootNode->Next = Next;
|
|
|
|
data = Return->NodeElement;
|
|
|
|
free ( Return );
|
|
|
|
mylist->size--;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2005-09-01 18:01:20 -04:00
|
|
|
short Enqueue ( List *mylist, Element Data ) {
|
|
|
|
return InsertAt ( mylist, Data, mylist->RootNode );
|
2004-02-11 02:58:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Element Dequeue ( List *mylist ) {
|
|
|
|
return Pop ( mylist );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Delete ( List *mylist, Element Data ) {
|
|
|
|
Node *Deleted;
|
|
|
|
Deleted = Find ( mylist, Data );
|
|
|
|
if ( Deleted == NULL )
|
|
|
|
return;
|
|
|
|
if ( Deleted->Next != NULL )
|
|
|
|
Deleted->Next->Previous = Deleted->Previous;
|
|
|
|
if ( Deleted->Previous != NULL )
|
|
|
|
Deleted->Previous->Next = Deleted->Next;
|
|
|
|
free ( Deleted->NodeElement );
|
|
|
|
free ( Deleted );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeletePosition ( List *mylist, unsigned short index ) {
|
|
|
|
Node *Deleted, *Prev, *Next;
|
|
|
|
unsigned short i;
|
|
|
|
Deleted = mylist->RootNode;
|
|
|
|
for ( i = 0; i < index; i++ ) {
|
|
|
|
Deleted = Deleted->Next;
|
|
|
|
if ( Deleted == NULL )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Prev = Deleted->Previous;
|
|
|
|
Next = Deleted->Next;
|
|
|
|
free ( Deleted->NodeElement );
|
|
|
|
free ( Deleted );
|
|
|
|
if ( Prev != NULL )
|
|
|
|
Prev->Next = Next;
|
|
|
|
if ( Next != NULL )
|
|
|
|
Next->Previous = Prev;
|
|
|
|
mylist->size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MakeEmpty ( List *mylist ) {
|
|
|
|
Node *node, *next;
|
|
|
|
unsigned short i = 0;
|
|
|
|
next = NULL;
|
|
|
|
node = mylist->RootNode;
|
|
|
|
|
|
|
|
for ( i = 0; i <= mylist->size; i++ ) {
|
|
|
|
next = node->Next;
|
|
|
|
if ( node->NodeElement != NULL )
|
|
|
|
free ( node->NodeElement );
|
|
|
|
free ( node );
|
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
mylist->size = 0;
|
|
|
|
}
|