| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Mike Wahler" |
| Date: |
31 Mar 2004 06:08:51 PM |
| Object: |
Re: Nested sort on a linked list |
"Mike Jeffers" <mikael888@lycos.com> wrote in message
news:7cdc09ec.0403301912.a7b744@posting.google.com...
Hi all,
I'm pretty new to programming and need help to do a nested sort on a
linked list.
Basically I'm sorting a list of structures with names, classroom
number and floor level. I can sort in scending order any of the 3
values but I need to do a nested sort first with floor, followed by
classroom number and finally name.
Please tell me where I have gone wrong with this.
This is my linked list structure:
typedef struct tagCLASSROOMDATA
{
//added to display addtional info on nozzle configuration
char name[128];
short floor;
short roomnumber;
} CLASSROOMDATA;
typedef struct CLASSROOMFILEITEM
{
struct tagCLASSROOMITEM* next;
CLASSROOMDATA dt;
} CLASSROOMITEM;
I am using the algorithm below for my sorting:
sortClass( )
{
BINFILEITEM *p, *q, *r, *s, *temp ;
p = r = firstBinfileItem ;
while ( p -> next != NULL )
{
s = q = p -> next ;
while ( q != NULL )
{
if ( p -> dt.floor > q -> dt.floor )
{
if ( p -> next == q ) /* Adjacent Nodes */
{
if ( p == firstBinfileItem )
{
p -> next = q -> next ;
q -> next = p ;
temp = p ;
p = q ;
q = temp ;
firstBinfileItem = p ;
r = p ;
s = q ;
q = q -> next ;
}
else
{
p -> next = q -> next ;
q -> next = p ;
r -> next = q ;
temp = p ;
p = q ;
q = temp ;
s = q ;
q = q -> next ;
}
}
else
{
if ( p == firstBinfileItem )
{
temp = q -> next ;
q -> next = p -> next ;
p -> next = temp ;
s -> next = p ;
temp = p ;
p = q ;
q = temp ;
s = q ;
q = q -> next ;
firstBinfileItem = p ;
}
else
{
temp = q -> next ;
q -> next = p -> next ;
p -> next = temp ;
r -> next = q ;
s -> next = p ;
temp = p ;
p = q ;
q = temp ;
s = q ;
q = q -> next ;
}
}
}
else
{
s = q ;
q = q -> next ;
}
}
r = p ;
p = p -> next ;
}
}
Any help will be most appreciated, thanks!
Given objects 'left' and 'right' being compared:
if(left.key1 == right.key1)
{
if(left.key2 == right.key2)
{
if(left.key3 < right.key3)
swap(left, right);
}
else
{
if(left.key2 < right.key2)
swap(left, right);
}
}
else
if(left.key1 < right.key1)
swap(left, right);
}
-Mike
.
|
|
| User: "Bill Weston" |
|
| Title: Re: Nested sort on a linked list |
01 Apr 2004 10:26:35 AM |
|
|
"Mike Wahler" <mkwahler@mkwahler.net> wrote in message news:<n4Jac.9633$Dv2.3819@newsread2.news.pas.earthlink.net>...
"Mike Jeffers" <mikael888@lycos.com> wrote in message
news:7cdc09ec.0403301912.a7b744@posting.google.com...
Hi all,
I'm pretty new to programming and need help to do a nested sort on a
linked list.
Basically I'm sorting a list of structures with names, classroom
number and floor level. I can sort in scending order any of the 3
values but I need to do a nested sort first with floor, followed by
classroom number and finally name.
Please tell me where I have gone wrong with this.
This is my linked list structure:
[snip]
Any help will be most appreciated, thanks!
Given objects 'left' and 'right' being compared:
if(left.key1 == right.key1)
{
if(left.key2 == right.key2)
{
if(left.key3 < right.key3)
swap(left, right);
}
else
{
if(left.key2 < right.key2)
swap(left, right);
}
}
else
if(left.key1 < right.key1)
swap(left, right);
}
-Mike
Or, for a shortish list and an easy life, and assuming that your basic
sort algorithm does not rearrange equal elements, just do the sort
three times...
first sort on key3,
then sort on key2,
then sort on key1.
Bill Weston
.
|
|
|
|

|
Related Articles |
|
|