Exemple #1
0
/**
 * Copy a list
 *
 * Arguments:
 * head      A pointer pointing to the first element of the sparse array
 *
 * Returns:
 * A copy sparse array
 *
 * This function will copy the sparse array that is passed to it. The * copy will be made into new memory. 
 *
 * This is useful, for example, when we want to merge
 * two linked lists together. We can make a copy of one of the linked
 * lists, and then merge the second into the copy. In this way the
 * original copy of the list is not "mutated".
 */
Node * List_copy(Node * head)
{
  Node * p = head;   //maintains pointer to top of the list passed in
  int counter = 0;   //counter to see how many nodes are on the list
  int * value;       //array for the values of the list
  int * index;       //array for the indexes of the list
  int i = 0;         //variable to fill in array values from list to be copied

  while(head != NULL)
    {
      counter++;
      head = head->next;
    }
  value = malloc(sizeof(int) * counter);
  index = malloc(sizeof(int) * counter);
  
  while(p != NULL)
    {
      value[i] = p->value;
      index[i] = p->index;
      i++;
      p = p->next;
    }
  
  Node * q = List_build(value,index,counter);
  
  free(value);
  free(index);

  return q;
}
Exemple #2
0
int main (int argc, char * argv[])
{
  printf("make a node and print it");
  Node * test = List_create(5,1);
  List_print(stdout, test);

  printf("destroying it");
  List_destroy(test);
  List_print(stdout, test);
  
  printf("linkingit again");
  Node * newnode =NULL;
  Node * newnode2 = NULL;
  int values[4] = {2,3,1,2};
  int indexs[4] = {1,4,2,3};
  // int i;
  int length = 4;
  printf("this is newnode");
  newnode = List_build(values, indexs, length);
  //printf("this is the newnode");  
  //List_print(stdout, newnode);

  //printf("address:%p", newnode->next);
  //printf("address:%p", newnode->next->next);
  newnode2 = List_copy(newnode);
  printf("copy of new node");
  List_print(stdout, newnode2);  
  //List_print(stdout, newnode);
  Node * merged = NULL;
  merged = List_merge(newnode, newnode2);
  List_print(stdout, merged);  
// printf("copy linked list");
    
  /*
  while (newnode!= NULL)
    {
      List_print(stdout, newnode);
      printf("this is the address : %p", newnode->next);  
    }
  */   
//newnode = List_insert_ascend(newnode -> next,3,4);
  //List_print(stdout, newnode);
  //printf("this is the address : %p", newnode->next);

  //List_print(stdout, newnode -> next); 
  return 0;
}
Exemple #3
0
/**
 * Copy a list
 *
 * Arguments:
 * head      A pointer pointing to the first element of the sparse array
 *
 * Returns:
 * A copy sparse array
 *
 * This function will copy the sparse array that is passed to it. The
 * copy will be made into new memory. 
 *
 * This is useful, for example, when we want to merge
 * two linked lists together. We can make a copy of one of the linked
 * lists, and then merge the second into the copy. In this way the
 * original copy of the list is not "mutated".
 */
Node * List_copy(Node * head)
{
	Node* copy = NULL;
	Node* p = head;
	int length = 0;
	while(p != NULL){
		length++;
		p = p->next;
	}
	int* values = malloc(sizeof(int) * length);
	int* indeces = malloc(sizeof(int) * length);
	p = head;
	int i;
	for(i = 0;i < length;i++){
		values[i] = p->value;
		indeces[i] = p->index;
		p = p->next;
	}
	copy = List_build(values, indeces, length);
	free(values);
	free(indeces);
	return copy;
}
Exemple #4
0
int readSparseArrayFile(const char* filename, Node * * sparse_array)
{
    // initialize input file
    *sparse_array = NULL;
    FILE * fptr = NULL;
    fptr = fopen(filename, "r");
    if (fptr == NULL)
	{
	    fprintf(stderr, "Error opening file: '%s', aborting.\n", filename);
	    return FALSE;
	}

    // initializing array
    int hasError = FALSE;
    int sparse_array_size = 0;
    int i;
    int lineno = 0;
    fscanf(fptr, "%d", &sparse_array_size);
    lineno++;

    int * array_index = malloc(sizeof(int) * sparse_array_size);
    int * array_value = malloc(sizeof(int) * sparse_array_size);
    if (sparse_array_size > 0 && (!array_index || !array_value))
	{
	    fprintf(stderr, "Failed to allocate memory (!), aborting.\n");
	    hasError = TRUE;
	}

    for(i = 0; i < sparse_array_size && !hasError; i++)
	{
	    if(fscanf(fptr, "%d", &array_index[i]) != 1)
		{
		    fprintf(stderr, "Input file error at line #%d\n", lineno);
		    hasError = TRUE;
		}
	    lineno++;
	}

    for(i = 0; i < sparse_array_size &&  !hasError; i++)
	{

	    if(fscanf(fptr, "%d", &array_value[i]) != 1)
		{
		    fprintf(stderr, "Input file error at line #%d\n", lineno);
		    hasError = TRUE;
		}
	    lineno++;
	}
    printf("\n");

    if(!hasError) 
	{
	    *sparse_array = List_build(array_value, array_index, sparse_array_size);
	}

    fclose(fptr);
    free(array_index);
    free(array_value);

    if(hasError)
	{
	    fprintf(stderr, "Aborting due to previous errors, goodbye.\n");
	    return FALSE;
	}
    
    return TRUE;
}
//Running everything and saving it correctly.
int main(int argc, char **argv)
{

  if(argc < 2)
    {
      printf("Error, no input given.\n");
      return(0);
    }

  int freq_array[127];

  char * filename = argv[1];
  int filename_size = strlen(filename);

  frequency_count(filename, freq_array);

  char * outfile = (char *)malloc(filename_size+5 * sizeof(char));

  strcpy(outfile, filename);
  outfile = strcat(outfile, ".huff");

  Node * list;

  list = List_build(freq_array);
  if (list == NULL)
    {
      return(0);
    }

  Node * temp;
  temp = list;
  while(temp != NULL)
    {
      printf("Frequency of %c = %d\n", temp -> char_val, temp -> freq);
      temp = temp -> next;
    }
  ListNode * tree_list;
  tree_list = ListNode_build(list);

  while(tree_list -> next != NULL)
    {
      ListNode * second = tree_list -> next;
      ListNode * third = second -> next;

      TreeNode * tn1 = tree_list -> tree_ptr;
      TreeNode * tn2 = second -> tree_ptr;

      free(tree_list);
      free(second);
      tree_list = third;

      TreeNode * fin = Tree_merge(tn1, tn2);
      ListNode * wn = ListNode_create(fin);

      tree_list = ListNode_insert(tree_list, wn);
    }
  Weight_print(tree_list); 
  List_destroy(list);
  TreeNode * root = tree_list -> tree_ptr; 
  free(tree_list);

  int numRow = Tree_leaf(root);
  int numCol = Tree_height(root);
  numCol++;
  int ** codebook = malloc(sizeof(int*) * numRow);
  int row;
  for(row = 0; row < numRow; row++)
    {
      codebook[row] = malloc(sizeof(int) * numCol);
      int col;
      for(col = 0; col < numCol; col++)
	{
	  codebook[row][col] = -1;
	}
    }
  buildCodeBook(root, codebook);
  printCodeBook(codebook, numRow);

  unsigned int numChar = 0;
  int z;
  for(z = 0; z < 127; z++)
    {
      numChar += freq_array[z];
    }
  printf("numChar: %d\n", numChar);
  int mapping[127];
  int ind;
  for(ind = 0; ind < 127; ind++)
    {
      mapping[ind] = -1;
      int ind2;
      for(ind2 = 0; ind2 < numRow; ind2 ++)
	{
	  if(codebook[ind2][0] == ind)
	    {
	      mapping[ind] = ind2;
	    }
	}
    }
  Tree_header(root, numChar,outfile);
  compress(filename, outfile, codebook, mapping);
  return(0);
}