Fixed all manner of weirdness in the code. RootNode is no longer just a

placeholder. Used C niftiness to reduce code length and increase
efficiency.
master
penguinc 2005-09-04 23:50:09 +00:00
parent 2c0aadbaae
commit d3c1479ba1
3 changed files with 64 additions and 48 deletions

View File

@ -16,21 +16,27 @@ void MakeList ( List *mylist, short (*Compare) ( const Element Data1, const Elem
}
Node *Find ( List *mylist, Element Data ) {
Node *n;
n = mylist->RootNode->Next;
while ( n != mylist->RootNode ) {
Node *n = mylist->RootNode;
do {
if ( mylist->Compare ( n->NodeElement, Data ) == 0 )
return n;
n = n->Next;
}
} while ( n != mylist->RootNode );
/* not found */
return NULL;
}
Node *Get ( List *mylist, unsigned short index ) {
unsigned short i, halfsize;
Node *Get ( List *mylist, unsigned long index ) {
unsigned long i, halfsize;
Node *GetPtr;
/* gotta check for special case get's */
if ( index == 0 )
return mylist->RootNode;
if ( index > mylist->size )
return (Node *)NULL;
halfsize = mylist->size / 2;
if ( index > halfsize ) {
GetPtr = mylist->RootNode->Previous;
@ -42,28 +48,26 @@ Node *Get ( List *mylist, unsigned short index ) {
for ( i = 0; i < index; i++ )
GetPtr = GetPtr->Next;
}
return GetPtr;
}
short InsertAt ( List *mylist, Element Data, Node *Pos ) {
Node *New;
New = malloc ( sizeof( Node ) );
if ( New == NULL )
return 0;
New->NodeElement = Data;
Node *New = NULL;
if ( mylist->size == 0 ) {
New->Previous = mylist->RootNode;
New->Next = mylist->RootNode;
mylist->RootNode->Previous = New;
mylist->RootNode->Next = New;
mylist->size++;
return 1;
}
New = mylist->RootNode;
New->Next = NULL;
New->Previous = NULL;
} else {
New = malloc ( sizeof( Node ) );
if ( New == NULL )
return 0;
New->Next = Pos;
}
New->NodeElement = Data;
mylist->size++;
New->Next = Pos;
if ( Pos != NULL ) {
New->Previous = Pos->Previous;
@ -78,21 +82,29 @@ short IsEmpty ( List *mylist ) {
return mylist->size == 0;
}
unsigned long ListSize ( List *mylist ) {
return mylist->size;
}
short Push ( List *mylist, Element Data ) {
return InsertAt ( mylist, Data, mylist->RootNode->Next );
return InsertAt ( mylist, Data, mylist->RootNode );
}
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--;
Node *Return = NULL, *Next = NULL;
Element data = NULL;
Return = mylist->RootNode;
if ( Return != NULL ) {
Next = Return->Next;
mylist->RootNode = Next;
data = Return->NodeElement;
if ( Return->Previous != NULL )
Return->Previous->Next = Next;
Next->Previous = Return->Previous;
free ( Return );
mylist->size--;
}
return data;
}
@ -117,9 +129,9 @@ void Delete ( List *mylist, Element Data ) {
free ( Deleted );
}
void DeletePosition ( List *mylist, unsigned short index ) {
void DeletePosition ( List *mylist, unsigned long index ) {
Node *Deleted, *Prev, *Next;
unsigned short i;
unsigned long i;
Deleted = mylist->RootNode;
for ( i = 0; i < index; i++ ) {
Deleted = Deleted->Next;
@ -139,16 +151,18 @@ void DeletePosition ( List *mylist, unsigned short index ) {
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;
next = NULL;
node = mylist->RootNode;
mylist->size = 0;
if ( node->Previous != NULL )
node->Previous->Next = NULL;
do {
next = node->Next;
if ( node->NodeElement != NULL )
free ( node->NodeElement );
free ( node );
node = next;
}
mylist->size = 0;
node = next;
} while ( node != NULL );
}

8
List.h
View File

@ -11,7 +11,7 @@ typedef struct NodeTag {
typedef struct {
Node *RootNode;
short (*Compare) ( const Element Data1, const Element Data2 );
unsigned short size;
unsigned long size;
} List;
/* Initializes the list.
@ -23,12 +23,14 @@ void MakeList ( List *mylist, short (*Compare) ( const Element Data1, const Elem
/* Returns a pointer to a node containing Element Data */
Node *Find ( List *mylist, Element Data );
/* Returns the index-th item in the List */
Node *Get ( List *mylist, unsigned short index );
Node *Get ( List *mylist, unsigned long index );
/* Inserts Data in front of Node Pos into the List */
short InsertAt ( List *mylist, Element Data, Node *Pos );
/* Returns true if list size is 0 */
short IsEmpty ( List *mylist );
/* Returns the size of the list */
unsigned long ListSize ( List *mylist );
/* Pushes Data into mylist as if it were a stack */
short Push ( List *mylist, Element Data );
@ -42,7 +44,7 @@ Element Dequeue ( List *mylist );
/* Deletes the Node containing Data */
void Delete ( List *mylist, Element Data );
/* Deletes the index-th item in List */
void DeletePosition ( List *mylist, unsigned short index );
void DeletePosition ( List *mylist, unsigned long index );
/* Completely empties a List. Must call MakeList to use again */
void MakeEmpty ( List *mylist );

8
main.c
View File

@ -22,8 +22,8 @@
void PrintList ( List *mylist ) {
Node *P;
unsigned short i = 0;
P = mylist->RootNode->Next;
unsigned long i = 0;
P = mylist->RootNode;
printf ( "[ " );
for ( i = 0; i < mylist->size; i++ ) {
printf ( "[%s] ", (char *)P->NodeElement );
@ -76,11 +76,11 @@ int main ( void )
Push ( &mylist, tmp );
}
free ( Dequeue ( &mylist ) );
free ( Dequeue ( &mylist ) );
tmp = malloc ( sizeof( Element ) * 3 );
sprintf ( tmp, "%d", 20 );
FindPtr = Find ( &mylist, (Element)"3" );
FindPtr = Find ( &mylist, (Element)"5" );
if ( !InsertAt ( &mylist, tmp, FindPtr ) ) {
free ( tmp );
OutOfMemory ( &mylist );