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
parent
2c0aadbaae
commit
d3c1479ba1
96
Linked.c
96
Linked.c
|
@ -16,21 +16,27 @@ void MakeList ( List *mylist, short (*Compare) ( const Element Data1, const Elem
|
||||||
}
|
}
|
||||||
|
|
||||||
Node *Find ( List *mylist, Element Data ) {
|
Node *Find ( List *mylist, Element Data ) {
|
||||||
Node *n;
|
Node *n = mylist->RootNode;
|
||||||
n = mylist->RootNode->Next;
|
do {
|
||||||
while ( n != mylist->RootNode ) {
|
|
||||||
if ( mylist->Compare ( n->NodeElement, Data ) == 0 )
|
if ( mylist->Compare ( n->NodeElement, Data ) == 0 )
|
||||||
return n;
|
return n;
|
||||||
n = n->Next;
|
n = n->Next;
|
||||||
}
|
} while ( n != mylist->RootNode );
|
||||||
|
|
||||||
|
/* not found */
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node *Get ( List *mylist, unsigned short index ) {
|
Node *Get ( List *mylist, unsigned long index ) {
|
||||||
unsigned short i, halfsize;
|
unsigned long i, halfsize;
|
||||||
Node *GetPtr;
|
Node *GetPtr;
|
||||||
|
|
||||||
|
/* gotta check for special case get's */
|
||||||
|
if ( index == 0 )
|
||||||
|
return mylist->RootNode;
|
||||||
if ( index > mylist->size )
|
if ( index > mylist->size )
|
||||||
return (Node *)NULL;
|
return (Node *)NULL;
|
||||||
|
|
||||||
halfsize = mylist->size / 2;
|
halfsize = mylist->size / 2;
|
||||||
if ( index > halfsize ) {
|
if ( index > halfsize ) {
|
||||||
GetPtr = mylist->RootNode->Previous;
|
GetPtr = mylist->RootNode->Previous;
|
||||||
|
@ -42,28 +48,26 @@ Node *Get ( List *mylist, unsigned short index ) {
|
||||||
for ( i = 0; i < index; i++ )
|
for ( i = 0; i < index; i++ )
|
||||||
GetPtr = GetPtr->Next;
|
GetPtr = GetPtr->Next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return GetPtr;
|
return GetPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
short InsertAt ( List *mylist, Element Data, Node *Pos ) {
|
short InsertAt ( List *mylist, Element Data, Node *Pos ) {
|
||||||
Node *New;
|
Node *New = NULL;
|
||||||
New = malloc ( sizeof( Node ) );
|
|
||||||
if ( New == NULL )
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
New->NodeElement = Data;
|
|
||||||
|
|
||||||
if ( mylist->size == 0 ) {
|
if ( mylist->size == 0 ) {
|
||||||
New->Previous = mylist->RootNode;
|
New = mylist->RootNode;
|
||||||
New->Next = mylist->RootNode;
|
New->Next = NULL;
|
||||||
mylist->RootNode->Previous = New;
|
New->Previous = NULL;
|
||||||
mylist->RootNode->Next = New;
|
} else {
|
||||||
mylist->size++;
|
New = malloc ( sizeof( Node ) );
|
||||||
return 1;
|
if ( New == NULL )
|
||||||
}
|
return 0;
|
||||||
|
New->Next = Pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
New->NodeElement = Data;
|
||||||
mylist->size++;
|
mylist->size++;
|
||||||
New->Next = Pos;
|
|
||||||
|
|
||||||
if ( Pos != NULL ) {
|
if ( Pos != NULL ) {
|
||||||
New->Previous = Pos->Previous;
|
New->Previous = Pos->Previous;
|
||||||
|
@ -78,21 +82,29 @@ short IsEmpty ( List *mylist ) {
|
||||||
return mylist->size == 0;
|
return mylist->size == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned long ListSize ( List *mylist ) {
|
||||||
|
return mylist->size;
|
||||||
|
}
|
||||||
|
|
||||||
short Push ( List *mylist, Element Data ) {
|
short Push ( List *mylist, Element Data ) {
|
||||||
return InsertAt ( mylist, Data, mylist->RootNode->Next );
|
return InsertAt ( mylist, Data, mylist->RootNode );
|
||||||
}
|
}
|
||||||
|
|
||||||
Element Pop ( List *mylist ) {
|
Element Pop ( List *mylist ) {
|
||||||
Node *Return, *Next;
|
Node *Return = NULL, *Next = NULL;
|
||||||
Element data;
|
Element data = NULL;
|
||||||
Return = mylist->RootNode->Next;
|
Return = mylist->RootNode;
|
||||||
if ( Return == NULL )
|
|
||||||
return (Element)NULL;
|
if ( Return != NULL ) {
|
||||||
Next = Return->Next;
|
Next = Return->Next;
|
||||||
mylist->RootNode->Next = Next;
|
mylist->RootNode = Next;
|
||||||
data = Return->NodeElement;
|
data = Return->NodeElement;
|
||||||
free ( Return );
|
if ( Return->Previous != NULL )
|
||||||
mylist->size--;
|
Return->Previous->Next = Next;
|
||||||
|
Next->Previous = Return->Previous;
|
||||||
|
free ( Return );
|
||||||
|
mylist->size--;
|
||||||
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,9 +129,9 @@ void Delete ( List *mylist, Element Data ) {
|
||||||
free ( Deleted );
|
free ( Deleted );
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeletePosition ( List *mylist, unsigned short index ) {
|
void DeletePosition ( List *mylist, unsigned long index ) {
|
||||||
Node *Deleted, *Prev, *Next;
|
Node *Deleted, *Prev, *Next;
|
||||||
unsigned short i;
|
unsigned long i;
|
||||||
Deleted = mylist->RootNode;
|
Deleted = mylist->RootNode;
|
||||||
for ( i = 0; i < index; i++ ) {
|
for ( i = 0; i < index; i++ ) {
|
||||||
Deleted = Deleted->Next;
|
Deleted = Deleted->Next;
|
||||||
|
@ -139,16 +151,18 @@ void DeletePosition ( List *mylist, unsigned short index ) {
|
||||||
|
|
||||||
void MakeEmpty ( List *mylist ) {
|
void MakeEmpty ( List *mylist ) {
|
||||||
Node *node, *next;
|
Node *node, *next;
|
||||||
unsigned short i = 0;
|
|
||||||
next = NULL;
|
|
||||||
node = mylist->RootNode;
|
|
||||||
|
|
||||||
for ( i = 0; i <= mylist->size; i++ ) {
|
next = NULL;
|
||||||
next = node->Next;
|
node = mylist->RootNode;
|
||||||
|
mylist->size = 0;
|
||||||
|
if ( node->Previous != NULL )
|
||||||
|
node->Previous->Next = NULL;
|
||||||
|
|
||||||
|
do {
|
||||||
|
next = node->Next;
|
||||||
if ( node->NodeElement != NULL )
|
if ( node->NodeElement != NULL )
|
||||||
free ( node->NodeElement );
|
free ( node->NodeElement );
|
||||||
free ( node );
|
free ( node );
|
||||||
node = next;
|
node = next;
|
||||||
}
|
} while ( node != NULL );
|
||||||
mylist->size = 0;
|
|
||||||
}
|
}
|
||||||
|
|
8
List.h
8
List.h
|
@ -11,7 +11,7 @@ typedef struct NodeTag {
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Node *RootNode;
|
Node *RootNode;
|
||||||
short (*Compare) ( const Element Data1, const Element Data2 );
|
short (*Compare) ( const Element Data1, const Element Data2 );
|
||||||
unsigned short size;
|
unsigned long size;
|
||||||
} List;
|
} List;
|
||||||
|
|
||||||
/* Initializes the 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 */
|
/* Returns a pointer to a node containing Element Data */
|
||||||
Node *Find ( List *mylist, Element Data );
|
Node *Find ( List *mylist, Element Data );
|
||||||
/* Returns the index-th item in the List */
|
/* 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 */
|
/* Inserts Data in front of Node Pos into the List */
|
||||||
short InsertAt ( List *mylist, Element Data, Node *Pos );
|
short InsertAt ( List *mylist, Element Data, Node *Pos );
|
||||||
|
|
||||||
/* Returns true if list size is 0 */
|
/* Returns true if list size is 0 */
|
||||||
short IsEmpty ( List *mylist );
|
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 */
|
/* Pushes Data into mylist as if it were a stack */
|
||||||
short Push ( List *mylist, Element Data );
|
short Push ( List *mylist, Element Data );
|
||||||
|
@ -42,7 +44,7 @@ Element Dequeue ( List *mylist );
|
||||||
/* Deletes the Node containing Data */
|
/* Deletes the Node containing Data */
|
||||||
void Delete ( List *mylist, Element Data );
|
void Delete ( List *mylist, Element Data );
|
||||||
/* Deletes the index-th item in List */
|
/* 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 */
|
/* Completely empties a List. Must call MakeList to use again */
|
||||||
void MakeEmpty ( List *mylist );
|
void MakeEmpty ( List *mylist );
|
||||||
|
|
||||||
|
|
8
main.c
8
main.c
|
@ -22,8 +22,8 @@
|
||||||
|
|
||||||
void PrintList ( List *mylist ) {
|
void PrintList ( List *mylist ) {
|
||||||
Node *P;
|
Node *P;
|
||||||
unsigned short i = 0;
|
unsigned long i = 0;
|
||||||
P = mylist->RootNode->Next;
|
P = mylist->RootNode;
|
||||||
printf ( "[ " );
|
printf ( "[ " );
|
||||||
for ( i = 0; i < mylist->size; i++ ) {
|
for ( i = 0; i < mylist->size; i++ ) {
|
||||||
printf ( "[%s] ", (char *)P->NodeElement );
|
printf ( "[%s] ", (char *)P->NodeElement );
|
||||||
|
@ -76,11 +76,11 @@ int main ( void )
|
||||||
Push ( &mylist, tmp );
|
Push ( &mylist, tmp );
|
||||||
}
|
}
|
||||||
|
|
||||||
free ( Dequeue ( &mylist ) );
|
free ( Dequeue ( &mylist ) );
|
||||||
|
|
||||||
tmp = malloc ( sizeof( Element ) * 3 );
|
tmp = malloc ( sizeof( Element ) * 3 );
|
||||||
sprintf ( tmp, "%d", 20 );
|
sprintf ( tmp, "%d", 20 );
|
||||||
FindPtr = Find ( &mylist, (Element)"3" );
|
FindPtr = Find ( &mylist, (Element)"5" );
|
||||||
if ( !InsertAt ( &mylist, tmp, FindPtr ) ) {
|
if ( !InsertAt ( &mylist, tmp, FindPtr ) ) {
|
||||||
free ( tmp );
|
free ( tmp );
|
||||||
OutOfMemory ( &mylist );
|
OutOfMemory ( &mylist );
|
||||||
|
|
Reference in New Issue