Ejemplo n.º 1
0
void mergeSort(struct node **head_ref)
{
  struct node *head = *head_ref;
  if(head == NULL || head->next == NULL) return;
  struct node *a=NULL;
  struct node *b=NULL;
  frontBackSplit(head,&a,&b);
  mergeSort(&a);
  mergeSort(&b);
  *head_ref = sortedMerge(a,b);
}
Ejemplo n.º 2
0
//merge sort for linked list
void mergeSort(Node **headRef) {
  Node *head = *headRef;
  Node *a;
  Node *b;

  if((head ==NULL)||(head->next==NULL)) {
     return;
  }
  frontBackSplit (head, &a, &b); // split head into a, b sublists
  mergeSort(&a);
  mergeSort(&b);

  *headRef = sortedMerge(a,b); // merge to sorted list
}
Ejemplo n.º 3
0
void RecordList::mergeSort(RecordListNode **headRef) {
	RecordListNode *head = *headRef;
	RecordListNode *a;
	RecordListNode *b;

	/* Base case -- length 0 or 1 */
	if ((head == NULL) || (head->_next == 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);
}
Ejemplo n.º 4
0
int main(){
  int c, n, r;
  do {
    printf("0.  Show\n");
    printf("1.  Append\n");
    printf("2.  Push\n");
    printf("3.  pop\n");
    printf("4.  Count Nodes\n");
    printf("5.  Get nth element\n");
    printf("6.  Delete List\n");
    printf("7.  Insert at nth index\n");
    printf("8.  InsertSort the List\n");
    printf("9.  FrontBackSplit\n");
    printf("10. Remove Duplicates\n");
    printf("11. Reverse the List\n");
    printf("12. Alternating Split\n");
    printf("13. Make a Dummy List\n");
    printf("14. Shuffle Merge\n");
    printf("15. EXIT\n");
    printf("Your Choice : ");
    scanf("%d", &c);
    switch(c){
      case 0 : show();
               break;
      case 1 : printf("Enter a number : ");
               scanf("%d", &n);
               append(n);
               break;
      case 2 : printf("Enter a number : ");
               scanf("%d", &n);
               push(n);
               break;         
      case 3 : if((r = pop()) > 0)
                 printf("%d\n", r);
               else
                 printf("List Empty\n");  
               break;
      case 4 : printf("%d\n", countNodes());
               break;
      case 5 : printf("Enter the index : ");
               scanf("%d", &n);
               ((r = getNth(n)) >= 0) ? printf("%d\n", r) : printf("Index out of Range\n");
               break;
      case 6 : deleteList();
               break;
      case 7 : printf("Enter the index and the number : ");
               scanf("%d%d", &r, &n);
               if(insertNth(r, n) < 0)
                 printf("Index out of Range. Aborting...");
               break;
      case 8 : insertSort();
               break;
      case 9 : frontBackSplit();
               break;
      case 10: removeDuplicate();
               break;
      case 11: reverse();
               break;
      case 12: alternatingSplit();
               break;    
      case 13: makeDummyList();
               break; 
      case 14: shuffleMerge();
               break;                                                                                           
      case 15: exit(0);         
      default: printf("wrong Choice\n");
               exit(0);                  
    }
  }while(c >= 0 && c <= 14);
}