Exemple #1
0
struct word_t* SortedMerge(struct word_t* one, struct word_t* two)
{
	struct word_t* result = NULL;
	if (one == NULL)
	{
		return(two);
	}
	else if (two == NULL)
	{
		return(one);
	}
	
	if (strcmp(one->word, two->word) < 0 )
	{
		result = one;
		result->next = SortedMerge(one->next, two);
		return(result);
	}
	else
	{
		result = two;
		result->next = SortedMerge(one, two->next);
		return(result);
	}
	return(result);
}
Exemple #2
0
struct Link* SortedMerge(struct Link* a, struct Link* b)
{
  struct Link* result = NULL;
 
  /* Base cases */
  if (a == NULL)
     return(b);
  else if (b==NULL)
     return(a);
 
  /* Pick either a or b, and recur */
  if ( 
        int_less(         // returns true if alphabetical order
                        ((Node*)(a))->p, // e.g. jane
                        ((Node*)(b))->p  // e.g. mike
                      )
     )
  {
     result = a;
     result->next = SortedMerge(a->next, b);
  }
  else
  {
     result = b;
     result->next = SortedMerge(a, b->next);
  }
  return(result);
}
Exemple #3
0
List_PersonPtr_Iterator SortedMerge(List_PersonPtr *list, List_PersonPtr_Iterator a, List_PersonPtr_Iterator b)
{
  // A: begin, slow
  // B: slow, end
  if(a.node == NULL)
    return b;
  else if(b.node == NULL)
    return a;

  List_PersonPtr_Iterator result;
  if(list->compare(a.deref(&a), b.deref(&b)))
  {
    List_PersonPtr_Iterator temp = a;
    temp.inc(&temp);
    result = a;
    b = SortedMerge(list, temp, b);
    result.node->next = b.node;
    b.node->prev = result.node;
  }
  else
  {
    List_PersonPtr_Iterator temp = b;
    temp.inc(&temp);
    result = b;
    a = SortedMerge(list, a , temp);
    result.node->next = a.node;
    a.node->prev = result.node;
  }

  return result;
}
Exemple #4
0
//方法二: 递归
struct node *SortedMerge(struct node *ahead, struct node *bhead)
{
  if (NULL == ahead)
  {
    return bhead;
  }

  if (NULL == bhead)
  {
    return ahead;
  }

  struct node *result = NULL;
  if (ahead->data <= bhead->data)
  {
    result = ahead;
    ahead = ahead->next;
  }
  else
  {
    result = bhead;
    bhead = bhead->next;
  }

  result->next = SortedMerge(ahead, bhead);

  return result;
}
/* Drier program to test above functions*/
int main()
{
	/* Start with the empty list */
	struct node *res = NULL;
	struct node* a = NULL;
	struct node* b = NULL;

	/* Let us create two sorted linked lists to test
	the functions
	Created lists, a: 5->10->15, b: 2->3->20 */
	push(&a, 15);
	push(&a, 10);
	push(&a, 5);

	push(&b, 20);
	push(&b, 3);
	push(&b, 2);

	printList(a);
	printList(b);
	/* Remove duplicates from linked list */
	res = SortedMerge(a, b);

	printf("Merged Linked List is: \n");
	printList(res);

	return 0;
}
int main(){
	
	struct node *head1=NULL;
	struct node *head2=NULL;
	push(&head1,40);
	push(&head1,30);
	push(&head1,20);
	push(&head1,10);
	
	push(&head2,80);
	push(&head2,70);
	push(&head2,60);
	push(&head2,50);
	
	printList(head1);
	printf("\n");
	printList(head2);
	printf("\n");
	struct node *new_head = SortedMerge(head1,head2);
	printList(new_head);
	
	printf("\n");
	
	printList(head1);
	printf("\n");
	printList(head2);
	
}
Exemple #7
0
List_PersonPtr_Iterator node_sort(List_PersonPtr *list, List_PersonPtr_Iterator begin, List_PersonPtr_Iterator end)
{
  List_PersonPtr_Iterator res1;
  List_PersonPtr_Iterator res2;

  if(begin.node == end.node)
  {
    begin.node->next = NULL;
    return begin;
  }
  else
  {
    List_PersonPtr_Iterator slow = begin;
    List_PersonPtr_Iterator fast = begin;
    fast.inc(&fast);
    while(!List_PersonPtr_Iterator_equal(fast,end))
    {
      fast.inc(&fast);
      if(!List_PersonPtr_Iterator_equal(fast,end))
      {
        fast.inc(&fast);
        slow.inc(&slow);
      }
    }
    List_PersonPtr_Iterator slow_next = slow;
    slow_next.inc(&slow_next);
    slow.node->next = NULL;
    res1 = node_sort(list, begin, slow);
    res2 = node_sort(list, slow_next, end);
  }
  return SortedMerge(list, res1, res2);
}
void MergeSort(struct node** headRef)
{

  struct node *head = *headRef;
  struct node *head_front = NULL;
  struct node *head_back = NULL;

  if(head == NULL || head->next == NULL)
  {
    printf("return\n");
    return;
  }

  FrontBackSplit(head, &head_front, &head_back);


  print_linked_list("&head_front", &head_front);
  MergeSort(&head_front);
  print_linked_list("&head_back", &head_back);
  MergeSort(&head_back);

  *headRef = SortedMerge(head_front, head_back);

  print_linked_list("headRef", headRef);

}
/*-------------Test Driver-----------*/
main(){
	struct node* a = BuildOneTwo();
	struct node* b = BuildOneTwoThree();
	struct node* merged = SortedMerge(a,b);
	struct node* temp = merged;
	PrintList(temp);
}
Exemple #10
0
/*
 * @func: merge sort for link list
 */
struct node* MergeSortRecur(struct node* head){
    if (head == NULL)
            return NULL;
    struct node* pfront = NULL;
    struct node* pback = NULL;
    FrontBackSplit(head, &pfront, &pback);
    return SortedMerge(MergeSortRecur(pfront), MergeSortRecur(pback));
}
//SortedMerge() Using Recursion
//Merge() is one of those nice recursive problems where the recursive solution code is
//much cleaner than the iterative code. You probably wouldn't want to use the recursive
//version for production code however, because it will use stack space which is
//proportional to the length of the lists.
struct node* SortedMerge(struct node* a, struct node* b)
{
    struct node* result = NULL;
    // Base cases
    if (a==NULL) return(b);
    else if (b==NULL) return(a);
    // Pick either a or b, and recur
    if (a->data <= b->data) {
        result = a;
        a->next = SortedMerge(a->next, b);
    }
    else {
        result = b;
        b->next = SortedMerge(a, b->next);
    }
    return(result);
}
struct node* SortedMerge(struct node* a, struct node* b)
{
    struct node* result = NULL;
    if(a==NULL)
        return(b);
    else if(b==NULL)
        return(a);
    if(a->data <= b->data)
    {
        result = a;
        result->next = SortedMerge(a->next,b);
    }
    else
    {
        result = b;
        result->next = SortedMerge(a,b->next);
    }
    return(result);
}
main()
{
	struct node* head=NULL;
	struct node* a=BuildOneTwoThree();
	struct node* b=BuildOneTwo();
		head=SortedMerge(a,b);
	while(head!=NULL){
		printf("%d ",head->data);
		head=head->next;
	}
}
Exemple #14
0
struct node* SortedMerge(struct node* a, struct node* b, int columnNumber[], char order[], int numberOfColumnsSort)
{
    struct node* result = NULL;

    /* Base cases */
    if (a == NULL)
        return(b);
    else if (b==NULL)
        return(a);
    int i;
    /* Pick either a or b, and recur */
    for (i = 0; i<numberOfColumnsSort; ++i) {
        int cNumber = columnNumber[i], flag = 0;
        node *temp1, *temp2;
        temp1 = a;
        temp2 = b;
        while (cNumber--) {
            temp1 = temp1->row;
            temp2 = temp2->row;
        }
        /*if(i == 1)
            printf("**%d**\n", columnNumber[i]);*/
        if ((strcmp(temp1->value, temp2->value) < 0 && order[0] == 'a') ||
            (strcmp(temp1->value, temp2->value) > 0 && order[0] == 'd')) {
            flag = 1;
            result = a;
            result->column = SortedMerge(a->column, b, columnNumber, order, numberOfColumnsSort);
        }
        else if((strcmp(temp1->value, temp2->value) == 0)) {
            continue;
        }
        else {
            flag = 1;
            result = b;
            result->column = SortedMerge(a, b->column, columnNumber, order, numberOfColumnsSort);
        }
        if(flag == 1)
            break;
    }
    return(result);
}
Exemple #15
0
 ListNode* MergeSortRecur(ListNode* head){
     if (head == NULL)
         return NULL;
     else if (head->next == NULL)
         return head;
         
     ListNode* pfront = NULL;
     ListNode* pback = NULL;
     FrontBackSplit(head, &pfront, &pback);
     
     return SortedMerge(MergeSortRecur(pfront), MergeSortRecur(pback));
 }
Exemple #16
0
//better code...
void MergeSort(struct node** headRef) {
	struct node* head = *headRef;
	struct node* a;
	struct node* b;
	// Base case -- length 0 or 1
	if ((head == NULL) || (head->next == NULL)) {
		return;
	}
	FrontBackSplit(head, &a, &b); // 
	
	MergeSort(&a); // 
	MergeSort(&b);
	*headRef = SortedMerge(a, b); //
}
// 15 — MergeSort()
void MergeSort(struct node** headRef) {
	struct node* head = *headRef;
	struct node* a;
	struct node* b;
	// Base case -- length 0 or 1
	if ((head == NULL) || (head->next == NULL)) {
		return;
	}
	FrontBackSplit(head, &a, &b); // Split head into 'a' and 'b' sublists
	// We could just as well use AlternatingSplit()
	MergeSort(&a); // Recursively sort the sublists
	MergeSort(&b);
	*headRef = SortedMerge(a, b); // answer = merge the two sorted lists together
}
Exemple #18
0
ALinkList::AListNode *SortedMerge(ALinkList::AListNode *node1, ALinkList::AListNode *node2)
{
	ALinkList::AListNode *result = NULL;

	if(!node1)
	return node2;
	if(!node2)
	return node1;
	if(node1->data < node2->data)
	{
		result = node1;
		result->Next = SortedMerge(node1->Next, node2);
	}

	else
	{
		result = node2;
		result->Next = SortedMerge(node1, node2->Next);
	}

	return result;

}
Exemple #19
0
ALinkList::AListNode * ALinkList::ZipMerge(AListNode *node1, AListNode *node2, bool bSwitch)
{
	ALinkList::AListNode *result = NULL;

	if(!node1)
	return node2;
	if(!node2)
	return node1;
	if(bSwitch)
	{
		result = node1;
		result->Next = SortedMerge(node1->Next, node2);
		bSwitch = false;
	}
	else
	{
		result = node2;
		result->Next = SortedMerge(node1, node2->Next);
		bSwitch = true;
	}

	return result;
}
struct node* SortedMerge(struct node *first, struct node *second)
{
    struct node *result;
    if(!first)
        return second;

    if(!second)
        return first;

    if(first->data < second->data)
    {
        result = first;
        result->next = SortedMerge(first->next, second);
	}

    if(second->data < first->data)
    {
        result = second;
        result->next = SortedMerge(first, second->next);
    }

    return result;
}
Exemple #21
0
/* See http://geeksforgeeks.org/?p=3622 for details of this
   function */
struct node* SortedMerge(struct node* a, struct node* b)
{
    struct node* result = NULL;
 
    /* Base cases */
    if (a == NULL)
        return (b);
    else if (b==NULL)
        return (a);
 
    /* Pick either a or b, and recur */
    if (a->data <= b->data)
    {
        result = a;
        result->arbit = SortedMerge(a->arbit, b);
    }
    else
    {
        result = b;
        result->arbit = SortedMerge(a, b->arbit);
    }
 
    return (result);
}
Exemple #22
0
void MergeSort(struct word_t** headref)
{
	struct word_t* one;
	struct word_t* two;
	struct word_t* head= *headref;
	if (head == NULL || head->next == NULL)
	{
		return;
	}

	Split(head, &one, &two);
	MergeSort(&one);
	MergeSort(&two);
	*headref = SortedMerge(one, two);
}
int main()
{
	struct node *Node=NULL, *ptr=NULL;
    struct node *start=NULL,*start1=NULL,*start2=NULL;
	char ch;
	do
	{
		ptr=Node;
		Node=NewNode();

		printf("\nEnter data :");
		scanf("%d",&Node->data);

		if(start1==NULL)
			start1=Node;
		else
			ptr->next=Node;
		printf("Do you want to continue(Y/N)");

	}while(toupper((ch=getch()))!='N');

	printf("\nComplete Linked list:\n");
	PrintList(start1);

    do
	{
		ptr=Node;
		Node=NewNode();

		printf("\nEnter data :");
		scanf("%d",&Node->data);

		if(start2==NULL)
			start2=Node;
		else
			ptr->next=Node;
		printf("Do you want to continue(Y/N)");

	}while(toupper((ch=getch()))!='N');

	printf("\nComplete Linked list:\n");
	PrintList(start2);
    start=SortedMerge(start1,start2);
    printf("\n\nMerged Linked list:\n");
	PrintList(start);

	return 0;
}
void FileIconCollection::MergeSort(FileIcon *&headRef)
// sorts the linked list by changing next pointers (not data)
{
	// Base case -- length 0 or 1
	if (!headRef || !headRef->pNext) return;

	// Split head into 'a' and 'b' sublists
	FileIcon *a, *b;
	FrontBackSplit(headRef, a, b);

	// Recursively sort the sublists
	MergeSort(a);
	MergeSort(b);

	// Merge the two sorted lists together
	headRef = SortedMerge(a, b);
}
void MergeSort(struct node** headRef)
{
    struct node* head = *headRef;
    struct node* a;
    struct node* b;

    if((head == NULL) || (head->next == NULL))
    {
        return;
    }

    FrontBackSplit(head, &a, &b);
    MergeSort(&a);
    MergeSort(&b);

    *headRef = SortedMerge(a,b);
}
int main() {
  struct node *a = NULL;
  SortedInsert2(&a, MakeNode(1));
  SortedInsert2(&a, MakeNode(4));
  SortedInsert2(&a, MakeNode(10));
  struct node *b = NULL;
  SortedInsert2(&b, MakeNode(3));
  SortedInsert2(&b, MakeNode(9));
  SortedInsert2(&b, MakeNode(21));
  
  printf("a: "); PrintList(a);
  printf("b: "); PrintList(b);
  struct node *merged_and_sorted = SortedMerge(a, b);
  printf("merged_and_sorted: "); PrintList(merged_and_sorted);
  printf("a: "); PrintList(a);
  printf("b: "); PrintList(b);
  
  return 0;
}
Exemple #27
0
/* sorts the linked list formed by arbit pointers
  (does not change next pointer or data) */
void MergeSort(struct node** headRef)
{
    struct node* head = *headRef;
    struct node* a, *b;
 
    /* Base case -- length 0 or 1 */
    if ((head == NULL) || (head->arbit == NULL))
        return;
 
    /* Split head into 'a' and 'b' sublists */
    FrontBackSplit(head, &a, &b);
 
    /* Recursively sort the sublists */
    MergeSort(&a);
    MergeSort(&b);
 
    /* answer = merge the two sorted lists together */
    *headRef = SortedMerge(a, b);
}
Exemple #28
0
/* sorts the linked list by changing next pointers (not data) */
void MergeSort(struct node** headRef, int columnNumber[], char order[], int numberOfColumnsSort)
{
    struct node* head = *headRef;
    struct node* a;
    struct node* b;

    /* Base case -- length 0 or 1 */
    if ((head == NULL) || (head->column == NULL))
    {
        return;
    }

    /* Split head into 'a' and 'b' sublists */
    FrontBackSplit(head, &a, &b);

    /* Recursively sort the sublists */
    MergeSort(&a, columnNumber, order, numberOfColumnsSort);
    MergeSort(&b, columnNumber, order, numberOfColumnsSort);

    /* answer = merge the two sorted lists together */
    *headRef = SortedMerge(a, b, columnNumber, order, numberOfColumnsSort);
}
Exemple #29
0
/* sorts the linked list by changing next pointers (not data) */
void MergeSort(struct Link** headRef)
{
  struct Link* head = *headRef; // deref that pointer to get the address of sentinel link
  struct Link* a;
  struct Link* b;
 
  //printf("%s is the head node's name\n", ((Node*)(head))->p->name);
  //printf("%s is the head.next node's name\n", ((Node*)(head->next))->p->name);
  //printf("%s is the head.next node's name\n", ((Node*)(head))->p->name);
  // the above works b.c. address of head is address of head->next
  //printf("%s is the head.prev node's name field====================\n", ((Node*)(head->prev))->p->name);
  // this last stmt designed to fail: it should point to sentinel, which has no data

  //1) If head is NULL or there is only one element 
  //in the Linked List, then return.
  // Base case -- length 0 or 1 
  if ((head == NULL) || (head->next == NULL))
  {
    return;
  }
 
  //2) Else divide the linked list into two halves.
  // a and b are two halves 
  /* Split head into 'a' and 'b' sublists 
     --a and b are empty when passed to FrontBackSplit, 
     and get their addresses populated when in that function.*/
  printf("calling FrontBackSplit(head, &a, &b); \n");
  FrontBackSplit(head, &a, &b); 

                        //printf("calling   MergeSort(&a); \n");
  // Recursively sort the sublists
  MergeSort(&a);
                        //printf("calling   MergeSort(&b); \n");
  MergeSort(&b);
 
  // merge the two sorted lists together 
  *headRef = SortedMerge(a, b);
}