Code:
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
void insert( int *n )
{
}
void lowest( int *n )
{
if( n )
printf( "the lowest is: %d\n", *n );
}
void highest( int *n )
{
if( n )
printf( "the highest is: %d\n", *n );
return n == NULL;
}
void done( int *n )
{
printf( "done\n" );
return n == NULL;
}
int getint( int *i )
{
char buf[ BUFSIZ ] = {0};
printf( "enter a number, or just hit enter to quit: " );
fflush( stdout );
if( fgets( buf, BUFSIZ, stdin ) && sscanf( buf, "%d", i ) == 1 )
return 0;
return -1;
}
int overkill( void (*function)(int *), int *number )
{
static struct ll { struct ll *lower; struct ll *higher; int number; } *ln,*hn;
if( function == insert )
{
while( !getint( number ) )
{
struct ll *nn = malloc( sizeof *nn );
if( nn )
{
nn->lower = nn->higher = NULL;
nn->number = *number;
/* only node */
if( hn == NULL )
{
hn = ln = nn;
}
else if( hn->number < nn->number ) /* append as highest */
{
nn->lower = hn;
hn = nn;
}
else if( ln->number > nn->number ) /* prepent as lowest */
{
nn->higher = ln;
ln = nn;
}
else /* walk through it and insert */
{
struct ll *p = ln;
while( p->higher && p->higher->number < nn->number )
p = p->higher;
if( p )
{
nn->lower = p;
nn->higher = p->higher;
p->higher = nn;
}
else
{
printf( "you broke it\n" );
exit( EXIT_FAILURE );
}
}
}
else
exit( EXIT_FAILURE );
}
}
if( function == lowest )
{
*number = ln ? ln->number : INT_MAX;
function( number );
}
if( function == highest )
{
*number = hn ? hn->number : INT_MIN;
function( number );
}
if( function == done )
{
while( ln )
{
struct ll *lw = ln->higher;
free( ln );
ln = lw;
}
ln = hn = NULL;
}
return -1;
}
int main( void )
{
int n = 0;
overkill( insert, &n );
overkill( lowest, &n );
overkill( highest, &n );
overkill( done, &n );
return 0;
}
This program returns the minimum and maximum numbers entered. Or something close to it anyway.
Quzah.