コード例 #1
0
ファイル: answer07.c プロジェクト: rnobis/solutions
/**
 * Merged two linked list together
 * 
 * Arguments:
 * head1      A pointer pointing to linked list 1
 * head2      A pointer pointing to linked list 2
 *
 * Returns:
 * A merged sparse array
 *
 * This function will merge two linked lists. Before merging, you 
 * should make a copy of "head1" so that you will still have the 
 * original array while modifying (merging) the second linked list. 
 *
 * Please refer to the README file for detailed instructions on how to
 * merge two lists.
 *
 * This function should not modify either "head1" or "head2". You only
 * need to make a clone of "head1".
 */
Node * List_merge(Node * head1, Node * head2)
{
  Node * head3 = List_copy(head1); //copy of list1
   
  while (head2 != NULL)
    {
      head3 = List_insert_ascend(head3,head2->value,head2->index);
      head2 = head2->next;
    }
  Node * copy = head3; //maintains pointer to top of list while deleting zero values
  Node * copy2 = head3;
  
  while(head3 != NULL)
    {
      if(head3->value == 0)
	{
	  if(head3 == copy)
	    {
	      copy2 = head3->next;
	    }
	  head3 = List_delete(head3,head3->index);
	}
	  
      head3 = head3->next;
    }
  if(head3 == NULL)
    {
      return copy2;    
    }
  else
    {
      return head3;
    }
}
コード例 #2
0
//Function to build list from frequencies
Node * List_build(int * frequencies)
{
  Node * ln;
  int j = 0;

  while(frequencies[j] == 0)
    {
      j++;
    }

  printf("Inserting %c : %d\n", j, frequencies[j]);
  ln = List_create(j, frequencies[j]);
  j++;
  int i = 0;
  i = j;

  while(i < 127)
    {
      if (frequencies[i] > 0)
	{
	  printf("Inserting %c : %d\n", i, frequencies[i]);
	  ln = List_insert_ascend(ln, i, frequencies[i]);
	}
      i++;
    }
  return ln;
}
コード例 #3
0
ファイル: answer07.c プロジェクト: sheel7/ECE264
/**
 * 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;
	while(head != NULL)
	{
		copy = List_insert_ascend(copy, head->value, head->index);
		head = head->next;
	}
    return copy;
}
コード例 #4
0
ファイル: answer07.c プロジェクト: paulkrog/R_Solutions
/**
 * Build a sparse array from the given indices and values with 
 * specific length.
 *
 * Arguments:
 * value    Array of values
 * index    Array of indices
 * length   Length of the above arrays
 *
 * Returns:
 * A sparse array.
 *
 * If a sparse array node has { value = 1000, index = 2 }, then that means that
 * the index "2" caries the value "1000". This is meant to convey an array of 1000 
 * "2s", but instead of creating 1000 nodes in your linked list, you only create
 * 1 node, and that note conceptually has 1000 "copies" of it. Hence 
 * each node in a sparse array has a "value" in addition to its "index".
 *
 * Note that an index can never carry the value of "0", because this would not make
 * any sense; however, negative values are fine. A negative value may seem odd
 * at first blush; however, this is like substraction, and makes sense for certain
 * cases.
 *
 * You need to insert nodes in ascending order by index.
 * See the notes to "List_insert_ascend"
 */
Node * List_build(int * value, int * index, int length)
{
  Node * head = NULL;
  int i = 0;
  for (i = 0; i < length; i++)
    {
      head = List_insert_ascend(head, value[i], index[i]);
    }
  return head;
}
コード例 #5
0
ファイル: answer07.c プロジェクト: hkoris/ECE264
/**
 * Build a sparse array from the given indices and values with 
 * specific length.
 *
 * Arguments:
 * value    Array of values
 * index    Array of indices
 * length   Length of the above arrays
 *
 * Returns:
 * A sparse array.
 *
 * If a sparse array node has { value = 1000, index = 2 }, then that means that
 * the index "2" caries the value "1000". This is meant to convey an array of 1000 
 * "2s", but instead of creating 1000 nodes in your linked list, you only create
 * 1 node, and that note conceptually has 1000 "copies" of it. Hence 
 * each node in a sparse array has a "value" in addition to its "index".
 *
 * Note that an index can never carry the value of "0", because this would not make
 * any sense; however, negative values are fine. A negative value may seem odd
 * at first blush; however, this is like substraction, and makes sense for certain
 * cases.
 *
 * You need to insert nodes in ascending order by index.
 * See the notes to "List_insert_ascend"
 */
Node * List_build(int * value, int * index, int length)
{
	int i;
	Node* n = NULL;
	sortIndeces(value, index, length);
	for(i = 0;i < length;i++){
		n = List_insert_ascend(n, value[i], index[i]);
	}

  return n;
}
コード例 #6
0
ファイル: answer07.c プロジェクト: sheel7/ECE264
/**
 * Merged two linked list together
 * 
 * Arguments:
 * head1      A pointer pointing to linked list 1
 * head2      A pointer pointing to linked list 2
 *
 * Returns:
 * A merged sparse array
 *
 * This function will merge two linked lists. Before merging, you 
 * should make a copy of "head1" so that you will still have the 
 * original array while modifying (merging) the second linked list. 
 *
 * Please refer to the README file for detailed instructions on how to
 * merge two lists.
 *
 * This function should not modify either "head1" or "head2". You only
 * need to make a clone of "head1".
 */
Node * List_merge(Node * head1, Node * head2)
{
	Node * newList = List_copy(head1);
	while(head2 != NULL)
	{
		newList = List_insert_ascend(newList, head2->value, head2->index);
		head2 = head2->next;
	}

    return newList;
}
コード例 #7
0
ファイル: answer07.c プロジェクト: CindyCKL/ece264-fall2013
/**
 * Merged two linked list together
 * 
 * Arguments:
 * head1      A pointer pointing to linked list 1
 * head2      A pointer pointing to linked list 2
 *
 * Returns:
 * A merged sparse array
 *
 * This function will merge two linked lists. Before merging, you 
 * should make a copy of "head1" so that you will still have the 
 * original array while modifying (merging) the second linked list. 
 *
 * Please refer to the README file for detailed instructions on how to
 * merge two lists.
 *
 * This function should not modify either "head1" or "head2". You only
 * need to make a clone of "head1".
 */
Node * List_merge(Node * head1, Node * head2)
{
  Node * headcopy = NULL;
  headcopy = List_copy(head1);
  //Node * head1copy = List_copy(head1);//why is it unused?
  while(head2 != NULL)
    {
      if(head2->value != 0)
	{
      /*
      if((headcopy -> index) == (head2 -> index))
	{
	  //Node * headmerged =, should i make a new variable headmerged? 
	  headcopy->value = (headcopy->value) + (head2->value);
	  if ((headcopy -> value) == 0)
	    {
	      List_delete(headcopy, (headcopy->index));
	    }
	}
      else if (headcopy->index != head2->index)
	{
	  if(head2->value != 0)
	    {
	      headcopy = List_insert_ascend(headcopy, head2->value, head2->index);
	    }
	  //headcopy->next = List_create(head2->index, head2->
	  //if((headcopy->value) == 0)
	  //  {
	  //    List_delete(headcopy, headcopy->index);
	  //  }
	}
      
      else if(head2 == NULL)
	{
	  headcopy = List_copy(head1);
	  if(headcopy->value == 0)
	    {
	      List_delete(headcopy, headcopy->index);
	    }
	}
      */
	  headcopy = List_insert_ascend(headcopy, head2->value, head2->index);
	  if(headcopy->value == 0)
	    {
	      headcopy = List_delete(headcopy, headcopy->index);
	    }
	}      
      //head1 = head1 ->next;
      head2 = head2 ->next;
    }

    return headcopy;
}
コード例 #8
0
ファイル: answer07.c プロジェクト: sheel7/ECE264
/**
 * Build a sparse array from the given indices and values with 
 * specific length.
 *
 * Arguments:
 * value    Array of values
 * index    Array of indices
 * length   Length of the above arrays
 *
 * Returns:
 * A sparse array.
 *
 * If a sparse array node has { value = 1000, index = 2 }, then that means that
 * the index "2" caries the value "1000". This is meant to convey an array of 1000 
 * "2s", but instead of creating 1000 nodes in your linked list, you only create
 * 1 node, and that note conceptually has 1000 "copies" of it. Hence 
 * each node in a sparse array has a "value" in addition to its "index".
 *
 * Note that an index can never carry the value of "0", because this would not make
 * any sense; however, negative values are fine. A negative value may seem odd
 * at first blush; however, this is like substraction, and makes sense for certain
 * cases.
 *
 * You need to insert nodes in ascending order by index.
 * See the notes to "List_insert_ascend"
 */
Node * List_build(int * value, int * index, int length)
{
	Node * sprsArr = NULL;
	int i = 0;

	for(i = 0; i < length; i++)
	{
		if(value[i] != 0)
		{
			sprsArr = List_insert_ascend(sprsArr,value[i],index[i]);
		}
	}
    return sprsArr;
}
コード例 #9
0
ファイル: answer07.c プロジェクト: CindyCKL/ece264-fall2013
/**
 * Build a sparse array from the given indices and values with 
 * specific length.
 *
c * Arguments:
 * value    Array of values
 * index    Array of indices
 * length   Length of the above arrays
 *
 * Returns:
= * A sparse array.
 *
 * If a sparse array node has { value = 1000, index = 2 }, then that means that
 * the index "2" caries the value "1000". This is meant to convey an array of 1000 
 * "2s", but instead of creating 1000 nodes in your linked list, you only create
 * 1 node, and that note conceptually has 1000 "copies" of it. Hence 
 * each node in a sparse array has a "value" in addition to its "index".
 *
 * Note that an index can never carry the value of "0", because this would not make
 * any sense; however, negative values are fine. A negative value may seem odd
 * at first blush; however, this is like substraction, and makes sense for certain
 * cases.
 *
 * You need to insert nodes in ascending order by index.
 * See the notes to "List_insert_ascend"
 */
Node * List_build(int * value, int * index, int length)
{
  Node * head =  NULL;
  int i;
  //int j;
  for(i= 0; i < length; i++)
    {
      if(value[i] != 0)
	{
	  head = List_insert_ascend(head, value[i], index[i]);
	  //  List_print(stdout, head);
	  //printf("this is the address: %p", head->next);
	}
    }
  return head;
}
コード例 #10
0
ファイル: answer07.c プロジェクト: hkoris/ECE264
/**
 * Merged two linked list together
 * 
 * Arguments:
 * head1      A pointer pointing to linked list 1
 * head2      A pointer pointing to linked list 2
 *
 * Returns:
 * A merged sparse array
 *
 * This function will merge two linked lists. Before merging, you 
 * should make a copy of "head1" so that you will still have the 
 * original array while modifying (merging) the second linked list. 
 *
 * Please refer to the README file for detailed instructions on how to
 * merge two lists.
 *
 * This function should not modify either "head1" or "head2". You only
 * need to make a clone of "head1".
 */
Node * List_merge(Node * head1, Node * head2)
{
	Node* p = List_copy(head1);
	Node* a1 = head2;
	Node* a2 = p;
	while(a1 != NULL){
		if(a1->index == a2->index){
			al->value += a2->value;
		}
		else{
			List_insert_ascend(a1, a2->value, a2->index);
		}
		a1 = a1->next;
	}
	
	return p;
}
コード例 #11
0
ファイル: answer07.c プロジェクト: rnobis/solutions
/**
 * Inserting "value" and "index" into the correct location in the 
 * sparse array "head"
 * 
 * Arguments: 
 * head      A pointer pointing to the first element of the linked list.
 * value     The "value" of the value
 * index     The "value" of the index
 *node
 * Returns:
 * A sparse array
 *
 * This function inserts the node ["value", "index"] into the sparse
 * array "head", and ensures that the nodes remain in ascending order
 * by their "index".
 *
 * Before and after the call to this function, "head" must be in
 * ASCENDING order by the "index" of each node.
 */
Node * List_insert_ascend(Node * head, int value, int index)
{
  if(head == NULL)
    {
      return List_create(value,index);
    }
  if((head->index) == index)
    {
      head->value += value;
      return head;
    }
  if ((head->index) > index)
    {
      Node * p = List_create(value,index);
      p->next = head;
      return p;
    }
  head->next = List_insert_ascend(head->next,value,index);
  return head;
}
コード例 #12
0
ファイル: answer07.c プロジェクト: sheel7/ECE264
/**
 * Inserting "value" and "index" into the correct location in the 
 * sparse array "head"
 * 
 * Arguments: 
 * head      A pointer pointing to the first element of the linked list.
 * value     The "value" of the value
 * index     The "value" of the index
 *
 * Returns:
 * A sparse array
 *
 * This function inserts the node ["value", "index"] into the sparse
 * array "head", and ensures that the nodes remain in ascending order
 * by their "index".
 *
 * Before and after the call to this function, "head" must be in
 * ASCENDING order by the "index" of each node.
 */
Node * List_insert_ascend(Node * head, int value, int index)
{
	if(head == NULL)
	{
		return(head = List_create(value,index));
	}
	if(head->index > index)
	{
		Node * p = List_create(value, index);
		p->next = head;
		return p;
	}
	if(head->index == index)
	{
		head->value += value;
		if(head->value == 0)
		{
			head = List_delete(head, head->index);
		}
		return(head);
	}
	head->next = List_insert_ascend(head->next, value, index);
	return(head);
}