By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,018 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,018 IT Pros & Developers. It's quick & easy.

Error of List Sorting

P: n/a
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=N ULL;
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);
}

Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

chellappa wrote:
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=N ULL;
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...

Nov 15 '05 #2

P: n/a
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.

Nov 15 '05 #3

P: n/a

chellappa wrote:
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=N ULL;
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.

Nov 15 '05 #4

P: n/a
jo*********@gmail.com writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.