Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Nested sort on a linked list

Reply
Thread Tools

Re: Nested sort on a linked list

 
 
Mike Wahler
Guest
Posts: n/a
 
      04-01-2004

"Mike Jeffers" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
> 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


 
Reply With Quote
 
 
 
 
Bill Weston
Guest
Posts: n/a
 
      04-01-2004
"Mike Wahler" <(E-Mail Removed)> wrote in message news:<n4Jac.9633$(E-Mail Removed) link.net>...
> "Mike Jeffers" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ...
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Re: Nested sort on a linked list Richard Herring C++ 0 04-01-2004 10:30 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



Advertisments