C
chellappa
Hi Everybody,
This is i am trying for sorting linked using pointer..........
but i dont know wthere this logic is good or not...........
please correct eroor.......it is not working ,i t simple looping
thats it
please correct this error
By
CNS
---------------------->
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
struct list
{
int a;
struct list *next;
};
struct list *head ,*curr, *top,
*a1=NULL,*a2=NULL,*temp,*a1parent=NULL,*a2parent=NULL;
head=NULL;
int i;
int value;
for(i=0;i<=10;i++)
{
curr=(struct list *)malloc(sizeof(struct list *));
curr->a=rand()%100;
curr->next =NULL;
if (head==NULL)
{
head=curr;
}
else
{
top->next =curr;
}
top=curr;
}
a1parent=head;
for(a1=head;a1;a1=a1->next)
{
a2parent=head;
for(a2=head;a2->next!=NULL;a2=a2->next)
{
if(a1->a < a2->a)
{
/* value=a1->a;
a1->a=a2->a;
a2->a=value;*/
if (a1->next ==NULL)
{
printf("\na1 next null");
scanf("%d",i);
a1parent->next=a2;
temp=a2->next;
a2->next=NULL;
a1->next=temp;
a2parent->next=a1;
printf("\na1 next null end");
}
else if (a2->next ==NULL)
{
printf("\na2 next null");
a2parent->next=a1;
temp=a1->next;
a1->next=NULL;
a2->next=temp;
a1parent->next=a2;
printf("\na2 next null end");
}
else if(a1==head && a2->next==NULL)
{
a2->next=a1->next;
head=a2;
a2parent->next=a1;
a1->next=NULL;
}
else if (a2==head && a2->next==NULL)
{
a2->next=a2->next;
head=a1;
a1parent->next=a2;
a2->next=NULL;
}
else if(a1==head)
{
printf("\na1 head ");
scanf("%d",&i);
temp=a2->next;
a2->next=a1->next;
head=a2;
a2parent->next=a1;
a1->next=temp;
printf("\na1 head END ");
}
else if (a2==head)
{
printf("\na2 head ");
scanf("%d",&i);
temp=a1->next;
a1->next=a2->next;
head=a1;
a1parent->next=a2;
a2->next=temp;
printf("\na2 head end");
}
else
{
temp=a2->next;
a1parent->next=a2;
a2->next=a1->next;
a2parent->next=a1;
a1->next=temp;
}
}
a2parent=a2;
}
a1parent=a1;
printf("for 1 ok");
}
for (top = head; top; top = top->next)
printf("%d\n", top->a);
free(head);
}
This is i am trying for sorting linked using pointer..........
but i dont know wthere this logic is good or not...........
please correct eroor.......it is not working ,i t simple looping
thats it
please correct this error
By
CNS
---------------------->
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
struct list
{
int a;
struct list *next;
};
struct list *head ,*curr, *top,
*a1=NULL,*a2=NULL,*temp,*a1parent=NULL,*a2parent=NULL;
head=NULL;
int i;
int value;
for(i=0;i<=10;i++)
{
curr=(struct list *)malloc(sizeof(struct list *));
curr->a=rand()%100;
curr->next =NULL;
if (head==NULL)
{
head=curr;
}
else
{
top->next =curr;
}
top=curr;
}
a1parent=head;
for(a1=head;a1;a1=a1->next)
{
a2parent=head;
for(a2=head;a2->next!=NULL;a2=a2->next)
{
if(a1->a < a2->a)
{
/* value=a1->a;
a1->a=a2->a;
a2->a=value;*/
if (a1->next ==NULL)
{
printf("\na1 next null");
scanf("%d",i);
a1parent->next=a2;
temp=a2->next;
a2->next=NULL;
a1->next=temp;
a2parent->next=a1;
printf("\na1 next null end");
}
else if (a2->next ==NULL)
{
printf("\na2 next null");
a2parent->next=a1;
temp=a1->next;
a1->next=NULL;
a2->next=temp;
a1parent->next=a2;
printf("\na2 next null end");
}
else if(a1==head && a2->next==NULL)
{
a2->next=a1->next;
head=a2;
a2parent->next=a1;
a1->next=NULL;
}
else if (a2==head && a2->next==NULL)
{
a2->next=a2->next;
head=a1;
a1parent->next=a2;
a2->next=NULL;
}
else if(a1==head)
{
printf("\na1 head ");
scanf("%d",&i);
temp=a2->next;
a2->next=a1->next;
head=a2;
a2parent->next=a1;
a1->next=temp;
printf("\na1 head END ");
}
else if (a2==head)
{
printf("\na2 head ");
scanf("%d",&i);
temp=a1->next;
a1->next=a2->next;
head=a1;
a1parent->next=a2;
a2->next=temp;
printf("\na2 head end");
}
else
{
temp=a2->next;
a1parent->next=a2;
a2->next=a1->next;
a2parent->next=a1;
a1->next=temp;
}
}
a2parent=a2;
}
a1parent=a1;
printf("for 1 ok");
}
for (top = head; top; top = top->next)
printf("%d\n", top->a);
free(head);
}