/* File: main06.c Uses ll.h and ll.c for generic linked list routines. Makes linked list of int and of double in same program. */ #include #include #include "ll.h" typedef struct int_node { struct int_node *next ; int data ; } int_node ; typedef struct double_node { struct double_node *next ; double data ; } double_node ; int_node *new_int_node (int data) { int_node *ptr ; ptr = malloc(sizeof(*ptr)) ; if (ptr == NULL) { fprintf(stderr, "Could not malloc() in new_int_node()!\n") ; exit(1) ; } ptr->data = data ; ptr->next = NULL ; return ptr ; } void print_int_node(int_node *ptr) { printf("%d ", ptr->data) ; return ; } int test_int() { base_node *list1 ; int_node *ptr ; printf("New list1: ") ; list1 = LL_New() ; LL_Insert(list1, new_int_node(72)) ; LL_Insert(list1, new_int_node(94)) ; LL_Insert(list1, new_int_node(31)) ; LL_Insert(list1, new_int_node(44)) ; LL_Insert(list1, new_int_node(57)) ; LL_Insert(list1, new_int_node(18)) ; LL_Insert(list1, new_int_node(2)) ; LL_Print(list1, (print_ftype) &print_int_node) ; printf("Remove 1 node: ") ; LL_Delete(list1) ; LL_Print(list1, (print_ftype) &print_int_node) ; printf("First item: ") ; ptr = (int_node *) LL_First(list1) ; printf("%d ", ptr->data) ; printf("\n") ; printf("Reverse list: ") ; base_node *temp_list = LL_New() ; while( ( ptr = (int_node *) LL_Extract(list1) )) { LL_Insert(temp_list, ptr) ; } LL_Destroy(list1) ; list1 = temp_list ; //shallow copy temp_list = NULL ; LL_Print(list1, (print_ftype) &print_int_node) ; LL_Destroy(list1) ; return 0 ; } double_node *new_double_node (double data) { double_node *ptr ; ptr = malloc(sizeof(*ptr)) ; if (ptr == NULL) { fprintf(stderr, "Could not malloc() in new_double_node()!\n") ; exit(1) ; } ptr->data = data ; ptr->next = NULL ; return ptr ; } void print_double_node(double_node *ptr) { printf("%f ", ptr->data) ; return ; } int test_double() { base_node *list1 ; double_node *ptr ; printf("New list1: ") ; list1 = LL_New() ; LL_Insert(list1, new_double_node(72.1)) ; LL_Insert(list1, new_double_node(94.2)) ; LL_Insert(list1, new_double_node(31.3)) ; LL_Insert(list1, new_double_node(44.4)) ; LL_Insert(list1, new_double_node(57.5)) ; LL_Insert(list1, new_double_node(18.6)) ; LL_Insert(list1, new_double_node(2.7)) ; LL_Print(list1, (print_ftype) &print_double_node) ; printf("Remove 1 node: ") ; LL_Delete(list1) ; LL_Print(list1, (print_ftype) &print_double_node) ; printf("First item: ") ; ptr = (double_node *) LL_First(list1) ; printf("%f ", ptr->data) ; printf("\n") ; printf("Reverse list: ") ; base_node *temp_list = LL_New() ; while( ( ptr = (double_node *) LL_Extract(list1) )) { LL_Insert(temp_list, ptr) ; } LL_Destroy(list1) ; list1 = temp_list ; //shallow copy temp_list = NULL ; LL_Print(list1, (print_ftype) &print_double_node) ; LL_Destroy(list1) ; return 0 ; } int main() { test_int() ; test_double() ; return 0 ; }