Hey,
I think the problem is with function merge().
You are not passing the linked list as an argument for merge function.
I found out that we need to pass two linked lists to the merge() function.
Otherwise, the merge will occur locally in the merge() and won't be
reflected when the function returns.
Thanks
J.
On Jan 23, 2008 10:56 AM, <[EMAIL PROTECTED]> wrote:
> hey friends can u please debug i am sending
> there is a probelem in the code its not sorting i guess properly
> !!!!!!!!!!!!!
>
> #include<stdio.h>
> #include<conio.h>
> typedef struct node
> {
> int index;
> int data;
> struct node*next;
> }node;
> node *head,*aux;
> void merge(int low,int mid,int high)
> {
> node *temp1,*temp2,*l1;
> aux=NULL;
> l1=aux;
> temp1=head;
> while(temp1->index!=low)
> temp1=temp1->next;
> temp2=head;
> while(temp2->index!=mid+1)
> temp2=temp2->next;
> while(temp1->index<=mid && (temp2->index<=high && temp2!=NULL))
> {
> if(temp1->data>temp2->data)
> {
> if(l1==NULL)
> {
> // l1=(node*)malloc(sizeof(node));
> l1=temp2;
> temp2=temp2->next;
> }
> else
> {
> // l1->next=(node*)malloc(sizeof(node));
> l1->next=temp2;
> l1=l1->next;
> temp2=temp2->next;
> }
> l1->next=NULL;
> }
> else
> {
> if(l1==NULL)
> {
> //l1=(node*)malloc(sizeof(node));
> l1=temp1;
> temp1=temp1->next;
> }
> else
> {
> //l1->next=(node*)malloc(sizeof(node));
> l1->next=temp1;
> l1=l1->next;
> temp1=temp1->next;
> }
> l1->next=NULL;
> }
> }
> while(temp1->index<=mid)
> {
> if(l1==NULL)
> {
> //l1=(node*)malloc(sizeof(node));
> l1=temp1;
> temp1=temp1->next;
> }
> else
> {
> //l1->next=(node*)malloc(sizeof(node));
> l1->next=temp1;
> l1=l1->next;
> temp1=temp1->next;
> }
> l1->next=NULL;
> }
> while(temp2->index<=high)
> {
> if(l1==NULL)
> {
> //l1=(node*)malloc(sizeof(node));
> l1=temp2;
> temp2=temp2->next;
> }
> else
> {
> //l1->next=(node*)malloc(sizeof(node));
> l1->next=temp2;
> l1=l1->next;
> temp2=temp2->next;
> }
> l1->next=NULL;
> }
> }
> void merge_sort(int low,int high)
> {
> int mid;
> if(low<high)
> {
> mid=(low+high)/2;
> merge_sort(low,mid);
> merge_sort(mid+1,high);
> merge(low,mid,high);
> }
> }
>
> void main()
> {
> int n,x,i;
> node *p,*end;
> clrscr();
> head=NULL;
> printf("\nEnter the no of elements in the list\n");
> scanf("%d",&n);
> for(i=0;i<n;i++)
> {
> printf("\nEnter the data you want to enter\n");
> scanf("%d",&x);
> if(head==NULL)
> {
> head=(node*)malloc(sizeof(node));
> head->index=i;
> head->data=x;
> head->next=NULL;
> p=head;
> }
> else
> {
> p->next=(node*)malloc(sizeof(node));
> p->next->index=i;
> p->next->data=x;
> p=p->next;
> }
> p->next=NULL;
> }
> printf("\nThe given list before sorting\n");
> p=head;
> while(p!=NULL)
> {
> printf("%d\t",p->data);
> p=p->next;
> }
> end=head;
> while(end->next!=NULL)
> end=end->next;
> merge_sort(head->index,end->index);
> printf("\nThe linked list after sorting is\n");
> p=aux;
> while(p!=NULL)
> {
> printf("%d\t",p->data);
> p=p->next;
> }
> getch();
> }
>
> ---------------------------------
> Get the freedom to save as many mails as you wish. Click here to know how.
>
> [Non-text portions of this message have been removed]
>
>
>
[Non-text portions of this message have been removed]