void testInsert() {
	Node *head = LL_Create( 4 );

	LL_Insert( &head, 2, compare_ints );
	LL_Insert( &head, 8, compare_ints );
	assert( head -> data == 2 );
	
	LL_Insert_Front( &head, 1 );
	print_list( head );
	assert( head -> data == 1 );
}
Example #2
0
int main() {
   base_node *list1 ;
   str_node *ptr ;

   printf("New list1: ") ;
   list1 = LL_New() ;
   LL_Insert(list1, new_str_node("Hello")) ;
   LL_Insert(list1, new_str_node("World. ")) ;
   LL_Insert(list1, new_str_node("My name is")) ;
   LL_Insert(list1, new_str_node("Zaphod Beeblebrox. ")) ;
   LL_Insert(list1, new_str_node("I stole the")) ;
   LL_Insert(list1, new_str_node("Heart of Gold. ")) ;
   LL_Insert(list1, new_str_node("Good-bye!")) ;

   LL_Print(list1, (print_ftype) &print_str_node) ;

   printf("Remove 1 node: ") ;
   LL_Delete(list1) ;
   LL_Print(list1, (print_ftype) &print_str_node) ;


   printf("First item: ") ;
   ptr = (str_node *) LL_First(list1) ;
   printf("%s ", ptr->data) ;
   printf("\n") ;
   // ptr->data[1] = 'a' ;  // would cause compilation error
   
   printf("Reverse list: ") ;
   base_node *temp_list = LL_New() ;
   while( ( ptr = (str_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_str_node) ;

   LL_Destroy(list1) ;
   return 0 ;
}
Example #3
0
int main() {
   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) ;

   printf("Remove 1 node: ") ;
   LL_Delete(list1) ;
   LL_Print(list1) ;


   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) ;

   LL_Destroy(list1) ;
   return 0 ;
}
Example #4
0
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 ;
}
Example #5
0
int main() {
   base_node *list1 ;
   base_node *ptr ;

   printf("New list1: ") ;
   list1 = LL_New() ;
   LL_Insert(list1, new_int_node(72)) ;
   LL_Insert(list1, new_double_node(94.1)) ;
   LL_Insert(list1, new_int_node(31)) ;
   LL_Insert(list1, new_int_node(44)) ;
   LL_Insert(list1, new_double_node(57.2)) ;
   LL_Insert(list1, new_double_node(18.7)) ;
   LL_Insert(list1, new_int_node(2)) ;

   LL_Print(list1) ;

   printf("Remove 1 node: ") ;
   LL_Delete(list1) ;
   LL_Print(list1) ;


   printf("First item: ") ;
   ptr = (base_node *) LL_First(list1) ;
   (* (ptr->vfunc[PRINT_VF])) (ptr) ;
   printf("\n") ;
   
   printf("Reverse list: ") ;
   base_node *temp_list = LL_New() ;
   while( ( ptr = (base_node *) LL_Extract(list1) )) {
      LL_Insert(temp_list, ptr) ;
   }
   LL_Destroy(list1) ;
   list1 = temp_list ;   //shallow copy
   temp_list = NULL ;
   LL_Print(list1) ;

   LL_Destroy(list1) ;
   return 0 ;
}
Example #6
0
int main() {
    node *list1 ;
    node *ptr ;

    printf("New list1: ") ;
    list1 = LL_New() ;
    LL_Insert(list1, 72) ;
    LL_Insert(list1, 94) ;
    LL_Insert(list1, 31) ;
    LL_Insert(list1, 44) ;
    LL_Insert(list1, 57) ;
    LL_Insert(list1, 18) ;
    LL_Insert(list1, 2) ;
    LL_Print(list1) ;

    printf("Remove 1 node: ") ;
    LL_Delete(list1) ;
    LL_Print(list1) ;


    printf("First item: ") ;
    ptr = LL_First(list1) ;
    printf(DATA_TYPE_FORMAT, ptr->data) ;
    printf("\n") ;

    printf("Reverse list: ") ;
    node *temp_list = LL_New() ;
    while( ( ptr = LL_Extract(list1) )) {
        LL_Insert(temp_list, ptr->data) ;
    }
    LL_Destroy(list1) ;
    list1 = temp_list ;   //shallow copy
    temp_list = NULL ;
    LL_Print(list1) ;

    LL_Destroy(list1) ;
    return 0 ;
}