Example #1
0
/* Destroy an entire sparse array. 
 * traversing the binary tree in postorder. Use the
 * SparseNode_destroy () function to destroy each node by itself.
 */
void SparseArray_destroy ( SparseNode * array )
{
  if (array == NULL) return;
  if (array->left != NULL) { SparseArray_destroy(array->left); }
  if (array->right != NULL) { SparseArray_destroy(array->right); }
  free(array);
}
Example #2
0
/* Destroy an entire sparse array tree. 
 *
 * Arguments:
 * *array         the root node of a sparse array tree
 * 
 * returns:
 * void
 *
 * traversing the binary tree in postorder. Use the
 * SparseNode_destroy () function to destroy each node by itself.
 */
void SparseArray_destroy(SparseNode * array) {
  if (array == NULL)
    return; //nothing to free
  if (array -> left != NULL) 
    SparseArray_destroy(array -> left);
  if (array -> right != NULL)
    SparseArray_destroy(array -> right);
  free(array);
}
Example #3
0
/* Destroy an entire sparse array tree.
*
* Arguments:
* *array the root node of a sparse array tree
*
* returns:
* void
*
* traversing the binary tree in postorder. Use the
* SparseNode_destroy () function to destroy each node by itself.
*/
void SparseArray_destroy ( SparseNode * array )
{
 if (array == NULL)
 {
  return;
 }
 SparseArray_destroy (array -> left);
 SparseArray_destroy (array -> right);
 free (array);
}
Example #4
0
void SparseArray_destroy (SparseNode * array)
{
  if(array != NULL)
    {

      SparseArray_destroy(array -> left);
      SparseArray_destroy(array -> right);
      free(array);

    }
}
int tests_SparseArray_destroy(int test_number) {
    int n_tests = 8;

    // If test_number is out of range, then...
    if(test_number < 0 || test_number >= n_tests) {
        //return how many distinct test-cases we have.
        return n_tests; 
    }

    printf("Testing: SparseNode_destroy(a%d)\n", test_number);
    SparseNode * sol_node = NULL;

    //build the array to be destroyed
    sol_node = make_array(test_number);

    printf("Built array a%d:\n", test_number);
    print_array(sol_node, 0);

    // Cleanup
    printf("Destroy array...\n");
    if(sol_node) {
        SparseArray_destroy(sol_node);
    }
    return TRUE;
}
Example #6
0
// gcc -g -Wall -Wshadow -DMYTEST -o answer07 answer07.c && ./answer07
int main(int argc, char * * argv)
{
  int val[8] = {0,1,2,4,1,5,8,3};
  int ind[8] = {5,2,7,1,9,6,4,3};
  printf("\nAbout to run my custom test-cases\n");

  // Test sparsenode create
  printf("\nSparseNode Create\n");
  SparseNode * head = SparseNode_create(7, 5);
  dump(head);
  //Insert
  printf("\nInsert\n");
  head = SparseArray_insert(head,10,3);
  head = SparseArray_insert(head,4,3);
  head = SparseArray_insert(head,1,3);
  head = SparseArray_insert(head,10,5);
  head = SparseArray_insert(head,8,3);
  head = SparseArray_insert(head,12,3);
  head = SparseArray_insert(head,5,3);
  dump(head);
  //Destroy
  printf("\nDestroy\n");
  SparseArray_destroy(head);
  dump(head);
  //Build
  printf("\nBuild\n");
  SparseNode * node;
  node = SparseArray_build(ind,val,8);
  dump(node);
  //Max and Min
  printf("\nMax\n");
  int max = SparseArray_getMax(node);
  int min = SparseArray_getMin(node);
  printf("\nMax = %d Min = %d\n",max,min);
  //Get Node
  printf("\nGet Node\n");
  SparseNode * getNode = SparseArray_getNode(node,7);
  dump(getNode);
  //Remove
  node = SparseArray_remove(node,2);
  printf("\nAfter Remove\n");
  dump(node);
  //Copy
  printf("\nCopy\n");
  dump(node);
  SparseNode * copy = SparseArray_copy(node);
  printf("\nAfter Copy\n");
  dump(copy);
  //Merge
  printf("\nMerge\n");

  return 0;
}
Example #7
0
int main ( int argc , char ** argv )
{
  int i; //index

  //check arguments
  if (argc != 3)
    {
      printf("usage: ./pa04 <input file> <output file>\n");
      return EXIT_FAILURE;
    }
  
  //initializing input file
  FILE * fptr = NULL;
  fptr = fopen(argv[1], "r");
  if (fptr == NULL)
    {
      printf("File error!\n");
      return EXIT_FAILURE;
    }
  /**********************************
   //initialize array 1
   **********************************/
  int number_of_value_1 = 0;
  fscanf(fptr, "%d", &number_of_value_1);
  printf("\nlength: %d\n", number_of_value_1);

  int* indices_1 = malloc(sizeof(int)* number_of_value_1);
  if (indices_1 == NULL)
    {
      printf("Array_1 indices malloc error!\n");
      return EXIT_FAILURE;
    }

  int* values_1 = malloc(sizeof(int)* number_of_value_1);
  if (values_1 == NULL)
    {
      printf("Array_1 values malloc error!\n");
      return EXIT_FAILURE;
    }

  printf("indices: ");

  for (i = 0; i < number_of_value_1; i++)
    {
      fscanf(fptr, "%d", &indices_1[i]);
      printf("%d ", indices_1[i]);
    }

  printf("\nvalues: ");
  for (i = 0; i < number_of_value_1; i++)
    {
      fscanf(fptr, "%d", &values_1[i]);
      printf("%d ", values_1[i]);
    }
  printf("\n");
  
  SparseNode * array_1 = NULL ;
  array_1 = SparseArray_build(indices_1, values_1, number_of_value_1);


  /************************************
   //initialize array 2
   ************************************/
  int number_of_value_2 = 0;
  fscanf(fptr, "%d", &number_of_value_2);
  printf("\nlength: %d\n", number_of_value_2);

  int* indices_2 = malloc(sizeof(int)* number_of_value_2);
  if (indices_2 == NULL)
    {
      printf("Array_2 indices malloc error!\n");
      return EXIT_FAILURE;
    }

  int* values_2 = malloc(sizeof(int)* number_of_value_2);
  if (values_2 == NULL)
    {
      printf("Array_2 values malloc error!\n");
      return EXIT_FAILURE;
    }

  printf("indices: ");
  for (i = 0; i < number_of_value_2; i++)
    {
      fscanf(fptr, "%d", &indices_2[i]);
      printf("%d ", indices_2[i]);
    }

  printf("\nvalues: ");
  for (i = 0; i < number_of_value_2; i++)
    {
      fscanf(fptr, "%d", &values_2[i]);
      printf("%d ", values_2[i]);
    }
   printf("\n");

  SparseNode * array_2 = NULL ;
  array_2 = SparseArray_build(indices_2, values_2, number_of_value_2);
  
  //finilizing the initialization step
  fclose(fptr);
  free(indices_1);
  free(values_1);
  free(indices_2);
  free(values_2);

  /***********************************
   ***********************************
   * Printing results
   ***********************************
   **********************************/
  SparseNode * onenode = NULL;
  
  fptr = fopen(argv[2], "w");
  if (fptr == NULL){
    printf("output file error\n");
    return EXIT_FAILURE;
  }

  /********************************************************
   * array 1 and array 2
   ********************************************************/

  printf("*******************************\n");
  printf("Array 1\n");
  printf("*******************************\n");
  if (array_1 != NULL)
    {
      for (i = SparseArray_getMin(array_1); 
	   i <= SparseArray_getMax(array_1); i++) 
	{
	  onenode = SparseArray_getNode(array_1,i);
	  if (onenode != NULL)
	    {
	      printf("%5d: %6d\n" , i , onenode->value);
	    }
	}
    }

  printf("\n*******************************\n");
  printf("Array 2\n");
  printf("*******************************\n");
  if (array_2 != NULL){
    for (i = SparseArray_getMin(array_2); 
	 i <= SparseArray_getMax(array_2); i++) 
      {
	onenode = SparseArray_getNode(array_2,i);
	if (onenode != NULL)
	  {
	    printf("%5d: %6d\n" , i , onenode->value);
	  }
      }
  }
  printf("\n*******************************\n");

  
  /********************************************************
   * copy array
   ********************************************************/
  
  SparseNode * copy = SparseArray_copy(array_1);

  printf("Array copy\n");
  printf("*******************************\n");
  if (copy != NULL){
    for (i = SparseArray_getMin(copy);
	 i <= SparseArray_getMax(copy); i++) 
      {
	onenode = SparseArray_getNode(copy,i);
	if (onenode != NULL)
	  {
	    printf("%5d: %6d\n" , i , onenode->value);
	  }
      }
  }
  printf("\n*******************************\n");
  
  /********************************************************
   * doing addition
   ********************************************************/

  SparseNode * array_new = NULL;
  array_new = SparseArray_merge(array_1, array_2);
  printf("Array result\n");
  printf("*******************************\n");
  if (array_new != NULL){
    for (i = SparseArray_getMin(array_new); 
	 i <= SparseArray_getMax(array_new); i++) {
      onenode = SparseArray_getNode(array_new,i);
      if (onenode != NULL)
	{
	  printf("%5d: %6d\n" , i , onenode->value);
	  fprintf(fptr, "%5d: %6d\n" , i , onenode->value);
	}
    }
  }
  printf("\n*******************************\n");

  

  SparseArray_destroy ( array_1 );
  SparseArray_destroy ( array_2 );
  SparseArray_destroy ( array_new );
  SparseArray_destroy ( copy );
  fclose(fptr);
  return EXIT_SUCCESS ;
}