Error of List Sorting

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);
}
 
C

Cong Wang

chellappa said:
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);
}
It's hard to see your codes.Dizzy...
 
J

joey.graham

I agree...

Suggestions for future posts:
Use correct grammar.
Use correct spelling.
Attempt to compress your code as much as possible.
Don't use 8-space tabs.

As for your logic, compare what you've done to any decent C book. It's
recommended you don't deviate from the method shown there.
 
A

Andrew Poelstra

chellappa said:
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);
}

I agree...

Suggestions for future posts:
Use correct grammar.
Use correct spelling.
Attempt to compress your code as much as possible.
Don't use 8-space tabs.

As for your logic, compare what you've done to any decent C book. It's
recommended you don't deviate from the method shown there.
 
K

Keith Thompson

I agree...

You agree with what? Don't assume that readers can easily see the
article to which you're replying; provide some context. Search this
newsgroup for "context, dammit" for numerous explanations.
Suggestions for future posts:
Use correct grammar.
Yes.

Use correct spelling.
Yes.

Attempt to compress your code as much as possible.

Depends on what you mean by "compress". Trimming out whatever isn't
necessary to make the point (while keeping the program complete and
compilable) is good. Compressing the code by, for example,
eliminating all whitespace is bad.
Don't use 8-space tabs.

Don't use tabs at all in code posted to Usenet. A reasonable
indentation level is 4 spaces.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top