This repository has been archived on 2020-05-27. You can view files and clone it, but cannot push or open issues/pull-requests.
clinkedlist/main.c

116 lines
2.1 KiB
C
Raw Permalink Normal View History

2004-02-11 02:58:36 -05:00
/* LinkList: a circular linked list implementation for the TI-92+ graphing calculator */
//#define USE_TI92PLUS
#ifdef USE_TI92PLUS
#define ENABLE_ERROR_RETURN
#define OPTIMIZE_ROM_CALLS
#define SAVE_SCREEN
#include <tigcclib.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif
#include "List.h"
void PrintList ( List *mylist ) {
Node *P;
unsigned long i = 0;
P = mylist->RootNode;
2004-02-11 02:58:36 -05:00
printf ( "[ " );
for ( i = 0; i < mylist->size; i++ ) {
printf ( "[%s] ", (char *)P->NodeElement );
if ( P->Next == NULL )
break;
else
P = P->Next;
}
printf ( "]\n" );
}
short CompareString ( const Element Data1, const Element 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 );
}
2004-02-11 02:58:36 -05:00
#ifdef USE_TI92PLUS
void _main ( void )
#else
int main ( void )
#endif
{
List mylist;
int i;
Element tmp;
Node *FindPtr;
#ifdef USE_TI92PLUS
ClrScr();
#endif
MakeList ( &mylist, CompareString );
if ( mylist.RootNode == NULL )
OutOfMemory ( &mylist );
2004-02-11 02:58:36 -05:00
PrintList ( &mylist );
for ( i = 0; i < 10; i++ ) {
tmp = malloc ( sizeof( Element ) * 3 );
sprintf ( tmp, "%d", i );
Push ( &mylist, tmp );
}
free ( Dequeue ( &mylist ) );
2004-02-11 02:58:36 -05:00
tmp = malloc ( sizeof( Element ) * 3 );
sprintf ( tmp, "%d", 20 );
FindPtr = Find ( &mylist, (Element)"5" );
if ( !InsertAt ( &mylist, tmp, FindPtr ) ) {
free ( tmp );
OutOfMemory ( &mylist );
}
2004-02-11 02:58:36 -05:00
tmp = malloc ( sizeof( Element ) * 4 );
sprintf ( tmp, "%s", "FOO" );
if ( !Enqueue ( &mylist, tmp ) ) {
free ( tmp );
OutOfMemory ( &mylist );
}
2004-02-11 02:58:36 -05:00
tmp = malloc ( sizeof( Element ) * 4 );
sprintf ( tmp, "%s", "BAR" );
if ( !Enqueue ( &mylist, tmp ) ) {
free ( tmp );
OutOfMemory ( &mylist );
}
2004-02-11 02:58:36 -05:00
PrintList ( &mylist );
MakeEmpty ( &mylist );
#ifdef USE_TI92PLUS
/* wait on keyboard input to continue */
ngetchx();
#endif
#ifndef USE_TI92PLUS
return 0;
#endif
}