Improved error handling for out of memory problems.
parent
a2efe57668
commit
2c0aadbaae
24
Linked.c
24
Linked.c
|
@ -4,15 +4,10 @@
|
||||||
|
|
||||||
#include "List.h"
|
#include "List.h"
|
||||||
|
|
||||||
void FatalError ( const char *ErrorMessage ) {
|
|
||||||
printf ( "%s\n", ErrorMessage );
|
|
||||||
exit ( 1 );
|
|
||||||
}
|
|
||||||
|
|
||||||
void MakeList ( List *mylist, short (*Compare) ( const Element Data1, const Element Data2 ) ) {
|
void MakeList ( List *mylist, short (*Compare) ( const Element Data1, const Element Data2 ) ) {
|
||||||
mylist->RootNode = malloc ( sizeof( Node ) );
|
mylist->RootNode = malloc ( sizeof( Node ) );
|
||||||
if ( mylist->RootNode == NULL )
|
if ( mylist->RootNode == NULL )
|
||||||
FatalError ( "MakeList: Could not allocate a Node" );
|
return; /* out of memory */
|
||||||
mylist->Compare = Compare;
|
mylist->Compare = Compare;
|
||||||
mylist->RootNode->Previous = NULL;
|
mylist->RootNode->Previous = NULL;
|
||||||
mylist->RootNode->Next = NULL;
|
mylist->RootNode->Next = NULL;
|
||||||
|
@ -50,12 +45,12 @@ Node *Get ( List *mylist, unsigned short index ) {
|
||||||
return GetPtr;
|
return GetPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InsertAt ( List *mylist, Element Data, Node *Pos ) {
|
short InsertAt ( List *mylist, Element Data, Node *Pos ) {
|
||||||
Node *New;
|
Node *New;
|
||||||
New = malloc ( sizeof( Node ) );
|
New = malloc ( sizeof( Node ) );
|
||||||
if ( New == NULL )
|
if ( New == NULL )
|
||||||
FatalError ( "InsertAt: Could not allocate new Node" );
|
return 0;
|
||||||
|
|
||||||
New->NodeElement = Data;
|
New->NodeElement = Data;
|
||||||
|
|
||||||
if ( mylist->size == 0 ) {
|
if ( mylist->size == 0 ) {
|
||||||
|
@ -64,7 +59,7 @@ void InsertAt ( List *mylist, Element Data, Node *Pos ) {
|
||||||
mylist->RootNode->Previous = New;
|
mylist->RootNode->Previous = New;
|
||||||
mylist->RootNode->Next = New;
|
mylist->RootNode->Next = New;
|
||||||
mylist->size++;
|
mylist->size++;
|
||||||
return;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
mylist->size++;
|
mylist->size++;
|
||||||
|
@ -76,14 +71,15 @@ void InsertAt ( List *mylist, Element Data, Node *Pos ) {
|
||||||
}
|
}
|
||||||
if ( New->Previous != NULL )
|
if ( New->Previous != NULL )
|
||||||
New->Previous->Next = New;
|
New->Previous->Next = New;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
short IsEmpty ( List *mylist ) {
|
short IsEmpty ( List *mylist ) {
|
||||||
return mylist->size == 0;
|
return mylist->size == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Push ( List *mylist, Element Data ) {
|
short Push ( List *mylist, Element Data ) {
|
||||||
InsertAt ( mylist, Data, mylist->RootNode->Next );
|
return InsertAt ( mylist, Data, mylist->RootNode->Next );
|
||||||
}
|
}
|
||||||
|
|
||||||
Element Pop ( List *mylist ) {
|
Element Pop ( List *mylist ) {
|
||||||
|
@ -100,8 +96,8 @@ Element Pop ( List *mylist ) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Enqueue ( List *mylist, Element Data ) {
|
short Enqueue ( List *mylist, Element Data ) {
|
||||||
InsertAt ( mylist, Data, mylist->RootNode );
|
return InsertAt ( mylist, Data, mylist->RootNode );
|
||||||
}
|
}
|
||||||
|
|
||||||
Element Dequeue ( List *mylist ) {
|
Element Dequeue ( List *mylist ) {
|
||||||
|
|
6
List.h
6
List.h
|
@ -25,17 +25,17 @@ 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 short index );
|
||||||
/* Inserts Data in front of Node Pos into the List */
|
/* Inserts Data in front of Node Pos into the List */
|
||||||
void 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 );
|
||||||
|
|
||||||
/* Pushes Data into mylist as if it were a stack */
|
/* Pushes Data into mylist as if it were a stack */
|
||||||
void Push ( List *mylist, Element Data );
|
short Push ( List *mylist, Element Data );
|
||||||
/* Pops the top Element off mylist, must free the returned Element when done */
|
/* Pops the top Element off mylist, must free the returned Element when done */
|
||||||
Element Pop ( List *mylist );
|
Element Pop ( List *mylist );
|
||||||
/* Enqueues Data as if List were a queue */
|
/* Enqueues Data as if List were a queue */
|
||||||
void Enqueue ( List *mylist, Element Data );
|
short Enqueue ( List *mylist, Element Data );
|
||||||
/* Dequeues the front Element off mylist, must free the returned Element when done */
|
/* Dequeues the front Element off mylist, must free the returned Element when done */
|
||||||
Element Dequeue ( List *mylist );
|
Element Dequeue ( List *mylist );
|
||||||
|
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -12,4 +12,4 @@ $(SRCS):
|
||||||
$(CC) $(CFLAGS) -o $@ $*.c
|
$(CC) $(CFLAGS) -o $@ $*.c
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@rm *.o
|
@rm *.o $(O_TARGET)
|
||||||
|
|
24
main.c
24
main.c
|
@ -39,6 +39,13 @@ short CompareString ( const Element Data1, const Element Data2 ) {
|
||||||
return strcmp ( Data1, Data2 );
|
return strcmp ( Data1, Data2 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OutOfMemory ( List *mylist ) {
|
||||||
|
printf ( "Ran out of memory!\n" );
|
||||||
|
if ( mylist != NULL && mylist->RootNode != NULL )
|
||||||
|
MakeEmpty ( mylist );
|
||||||
|
exit ( 1 );
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef USE_TI92PLUS
|
#ifdef USE_TI92PLUS
|
||||||
|
|
||||||
void _main ( void )
|
void _main ( void )
|
||||||
|
@ -59,6 +66,8 @@ int main ( void )
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
MakeList ( &mylist, CompareString );
|
MakeList ( &mylist, CompareString );
|
||||||
|
if ( mylist.RootNode == NULL )
|
||||||
|
OutOfMemory ( &mylist );
|
||||||
PrintList ( &mylist );
|
PrintList ( &mylist );
|
||||||
|
|
||||||
for ( i = 0; i < 10; i++ ) {
|
for ( i = 0; i < 10; i++ ) {
|
||||||
|
@ -72,15 +81,24 @@ int main ( void )
|
||||||
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)"3" );
|
||||||
InsertAt ( &mylist, tmp, FindPtr );
|
if ( !InsertAt ( &mylist, tmp, FindPtr ) ) {
|
||||||
|
free ( tmp );
|
||||||
|
OutOfMemory ( &mylist );
|
||||||
|
}
|
||||||
|
|
||||||
tmp = malloc ( sizeof( Element ) * 4 );
|
tmp = malloc ( sizeof( Element ) * 4 );
|
||||||
sprintf ( tmp, "%s", "FOO" );
|
sprintf ( tmp, "%s", "FOO" );
|
||||||
Enqueue ( &mylist, tmp );
|
if ( !Enqueue ( &mylist, tmp ) ) {
|
||||||
|
free ( tmp );
|
||||||
|
OutOfMemory ( &mylist );
|
||||||
|
}
|
||||||
|
|
||||||
tmp = malloc ( sizeof( Element ) * 4 );
|
tmp = malloc ( sizeof( Element ) * 4 );
|
||||||
sprintf ( tmp, "%s", "BAR" );
|
sprintf ( tmp, "%s", "BAR" );
|
||||||
Enqueue ( &mylist, tmp );
|
if ( !Enqueue ( &mylist, tmp ) ) {
|
||||||
|
free ( tmp );
|
||||||
|
OutOfMemory ( &mylist );
|
||||||
|
}
|
||||||
|
|
||||||
PrintList ( &mylist );
|
PrintList ( &mylist );
|
||||||
MakeEmpty ( &mylist );
|
MakeEmpty ( &mylist );
|
||||||
|
|
Reference in New Issue