# include # include # include "ppc_ll.h" struct point { double x; double y; }; int main ( ); int catch_the_odds ( const void *aa ); int cmp1 ( const void *aa, const void *bb, void *pp ); int cmp2 ( const void *aa, const void *bb, void *pp ); void ll_filter_test ( ); void ll_free_test ( ); void ll_length_test ( ); void ll_pop_test ( ); void ll_push_test ( ); void ll_reverse_test ( ); void ll_sort_cmp1_test ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: ppc_ll_test() tests ppc_ll(). Licensing: This code is distributed under the MIT license. Modified: 30 April 2024 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "ppc_ll_test():\n" ); printf ( " C version\n" ); printf ( " Test ppc_ll(), which demonstrates linked lists.\n" ); ll_push_test ( ); ll_pop_test ( ); ll_free_test ( ); ll_reverse_test ( ); ll_sort_cmp1_test ( ); ll_filter_test ( ); ll_length_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "ppc_ll_test():\n" ); printf ( " Normal end of execution.\n" ); timestamp ( ); return 0; } /******************************************************************************/ int catch_the_odds ( const void *aa ) /******************************************************************************/ /* Purpose: catch_the_odds() returns a 0 for items to keep, and nonzero otherwise. Discussion: Here, we "catch" the odd numbers in a list of integers. Licensing: This code is distributed under the MIT license. Modified: 30 April 2024 Author: John Burkardt Input: void *aa: a pointer to an item of data. Output: int value: 0 if the data is to be kept, nonzero otherwise. */ { int a = * ( int * ) aa; int value; value = a % 2; return value; } /******************************************************************************/ int cmp1 ( const void *aa, const void *bb, void *pp ) /******************************************************************************/ /* Purpose: cmp1() compares integer values for the sort test. Licensing: This code is distributed under the MIT license. Modified: 29 April 2024 Author: John Burkardt */ { int a = * ( int * ) aa; int b = * ( int * ) bb; int value; if ( a < b ) { value = -1; } else if ( b < a ) { value = +1; } else { value = 0; } return value; } /******************************************************************************/ int cmp2 ( const void *aa, const void *bb, void *pp ) /******************************************************************************/ /* Purpose: cmp2() compares real vectors of dimension 2 for the sort test. Licensing: This code is distributed under the MIT license. Modified: 29 April 2024 Author: John Burkardt */ { struct point *a = ( struct point * ) aa; struct point *b = ( struct point * ) bb; double d1; double d2; double dx; double dy; struct point *p = ( struct point * ) pp; int value; dx = a->x - p->x; dy = a->y - p->y; d1 = dx * dx + dy * dy; dx = b->x - p->x; dy = b->y - p->y; d2 = dx * dx + dy * dy; if ( d1 < d2 ) { value = -1; } else if ( d2 < d1 ) { value = +1; } else { value = 0; } return value; } /******************************************************************************/ void ll_filter_test ( ) /******************************************************************************/ /* Purpose: ll_filter_test() tests ll_filter(). Licensing: This code is distributed under the MIT license. Modified: 30 April 2024 Author: John Burkardt */ { conscell *kept = NULL; conscell *list = NULL; conscell *removed = NULL; int a = 101; int b = -45; int c = 1000; int d = 12; int e = 88; int f = 89; int g = 101; int h = 102; printf ( "\n" ); printf ( "ll_filter_test():\n" ); printf ( " Demonstrate ll_filter()\n" ); list = ll_push ( list, &a ); list = ll_push ( list, &b ); list = ll_push ( list, &c ); list = ll_push ( list, &d ); list = ll_push ( list, &e ); list = ll_push ( list, &f ); list = ll_push ( list, &g ); list = ll_push ( list, &h ); printf ( "\n" ); printf ( " The original linked list:\n" ); for ( conscell *p = list; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Apply the filter. */ kept = ll_filter ( list, catch_the_odds, &removed ); /* Print the kept items. */ printf ( "\n" ); printf ( " The kept items:\n" ); for ( conscell *p = kept; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Print the removed items. */ printf ( "\n" ); printf ( " The removed items:\n" ); for ( conscell *p = removed; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Free memory. */ ll_free ( list ); printf ( "\n" ); printf ( " linked list memory has been freed.\n" ); return; } /******************************************************************************/ void ll_free_test ( ) /******************************************************************************/ /* Purpose: ll_free_test() tests ll_free(). Licensing: This code is distributed under the MIT license. Modified: 25 April 2024 Author: John Burkardt */ { conscell *list = NULL; int a = 101; int b = -45; int c = 1000; int d = 12; printf ( "\n" ); printf ( "ll_free_test():\n" ); printf ( " Demonstrate ll_free()\n" ); list = ll_push ( list, &a ); list = ll_push ( list, &b ); list = ll_push ( list, &c ); list = ll_push ( list, &d ); printf ( "\n" ); printf ( " The original linked list:\n" ); for ( conscell *p = list; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Free memory. */ ll_free ( list ); printf ( "\n" ); printf ( " linked list memory has been freed.\n" ); return; } /******************************************************************************/ void ll_length_test ( ) /******************************************************************************/ /* Purpose: ll_length_test() tests ll_length(). Licensing: This code is distributed under the MIT license. Modified: 30 April 2024 Author: John Burkardt */ { conscell *list = NULL; int a = 101; int b = -45; int c = 1000; int d = 12; int l; printf ( "\n" ); printf ( "ll_length_test():\n" ); printf ( " Demonstrate ll_length()\n" ); list = ll_push ( list, &a ); list = ll_push ( list, &b ); list = ll_push ( list, &c ); list = ll_push ( list, &d ); printf ( "\n" ); printf ( " The linked list:\n" ); for ( conscell *p = list; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Get length. */ l = ll_length ( list ); printf ( "\n" ); printf ( " The list includes %d links.\n", l ); /* Free memory. */ ll_free ( list ); printf ( "\n" ); printf ( " linked list memory has been freed.\n" ); return; } /******************************************************************************/ void ll_pop_test ( ) /******************************************************************************/ /* Purpose: ll_pop_test() tests ll_pop(). Licensing: This code is distributed under the MIT license. Modified: 24 April 2024 Author: John Burkardt */ { conscell *list = NULL; int a = 101; int b = -45; int c = 1000; int d = 12; printf ( "\n" ); printf ( "ll_pop_test():\n" ); printf ( " Demonstrate ll_pop()\n" ); /* Create a quick linked list. */ list = ll_push ( list, &a ); list = ll_push ( list, &b ); list = ll_push ( list, &c ); list = ll_push ( list, &d ); /* Print the list. */ printf ( "\n" ); printf ( " The initial linked list:\n" ); for ( conscell *p = list; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Pop twice. */ list = ll_pop ( list ); list = ll_pop ( list ); printf ( "\n" ); printf ( " The linked list after two pops:\n" ); for ( conscell *p = list; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Pop thrice. */ list = ll_pop ( list ); list = ll_pop ( list ); list = ll_pop ( list ); printf ( "\n" ); printf ( " The linked list after three more pops:\n" ); for ( conscell *p = list; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Free memory. */ ll_free ( list ); return; } /******************************************************************************/ void ll_push_test ( ) /******************************************************************************/ /* Purpose: ll_push_test() tests ll_push(). Licensing: This code is distributed under the MIT license. Modified: 23 April 2024 Author: John Burkardt */ { conscell *list = NULL; int a = 101; int b = -45; int c = 1000; int d = 12; printf ( "\n" ); printf ( "ll_push_test():\n" ); printf ( " Demonstrate ll_push()\n" ); list = ll_push ( list, &a ); list = ll_push ( list, &b ); list = ll_push ( list, &c ); list = ll_push ( list, &d ); printf ( "\n" ); printf ( " The original linked list:\n" ); for ( conscell *p = list; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Free memory. */ ll_free ( list ); return; } /******************************************************************************/ void ll_reverse_test ( ) /******************************************************************************/ /* Purpose: ll_reverse_test() tests ll_reverse(). Licensing: This code is distributed under the MIT license. Modified: 25 April 2024 Author: John Burkardt */ { conscell *list = NULL; conscell *new = NULL; int a = 101; int b = -45; int c = 1000; int d = 12; printf ( "\n" ); printf ( "ll_reverse_test():\n" ); printf ( " Demonstrate ll_reverse()\n" ); list = ll_push ( list, &a ); list = ll_push ( list, &b ); list = ll_push ( list, &c ); list = ll_push ( list, &d ); printf ( "\n" ); printf ( " The original linked list:\n" ); for ( conscell *p = list; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Reverse the list. */ new = ll_reverse ( list ); printf ( "\n" ); printf ( " The reversed linked list:\n" ); for ( conscell *p = new; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Free memory. Error occurs if I free list. */ /* ll_free ( list ); */ ll_free ( new ); return; } /******************************************************************************/ void ll_sort_cmp1_test ( ) /******************************************************************************/ /* Purpose: ll_sort_cmp1_test() tests ll_sort() using cmp1. Licensing: This code is distributed under the MIT license. Modified: 25 April 2024 Author: John Burkardt */ { conscell *list = NULL; conscell *new = NULL; int a = 101; int b = -45; int c = 1000; int d = 12; printf ( "\n" ); printf ( "ll_sort_cmp1_test():\n" ); printf ( " Demonstrate ll_sort() using cmp1()\n" ); list = ll_push ( list, &a ); list = ll_push ( list, &b ); list = ll_push ( list, &c ); list = ll_push ( list, &d ); printf ( "\n" ); printf ( " The original linked list:\n" ); for ( conscell *p = list; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Sort the list. */ new = ll_sort ( list, cmp1, NULL ); printf ( "\n" ); printf ( " The sorted linked list:\n" ); for ( conscell *p = new; p != NULL; p = p->next ) { printf ( " %d", * ( int * )p->data ); } printf ( "\n" ); /* Free memory. Error occurs if I free list. */ /* ll_free ( list ); */ ll_free ( new ); return; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }