Sorting alphabetically a linked list

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 31
Reputation: group256 is an unknown quantity at this point 
Solved Threads: 1
group256 group256 is offline Offline
Light Poster

Sorting alphabetically a linked list

 
0
  #1
Aug 31st, 2009
Hi everyone,

I'm dealing with a linked list that is supposed to be sorted. It's consisted of a series of strings which each of these strings are associated with 4 more strings. So, I'm looking for a way to sort this linked list with optimum way possible. I have some ideas myself:

1- Make 5 big array and copy everything into it.
Then sort the first string and make any change that is done to first to the rest of arrays.

2- Do the same algorithm using vectors.

3- Or do the same thing using linked list.

Let's say it's something like this, please bare in mind the numbers are in string format:

3 Tom Hanks 52 male actor
1 Sean Penn 50 male actor
2 Clive Owen 44 male actor

after sorting

1 Sean Penn 50 male actor
2 Clive Owen 44 male actor
3 Tom Hanks 52 male actor

Please let me know what could be the easiest and best way to do it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 147
Reputation: GDICommander is an unknown quantity at this point 
Solved Threads: 19
GDICommander's Avatar
GDICommander GDICommander is offline Offline
Junior Poster

Re: Sorting alphabetically a linked list

 
0
  #2
Aug 31st, 2009
http://www.codeguru.com/forum/showthread.php?t=366064

You can use std::sort with a STL container, like a vector or a list. You will need to write a function that determines if a element is greater that an other one.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Sorting alphabetically a linked list

 
0
  #3
Aug 31st, 2009
You can use std::sort with a STL container, like a vector or a list.
sort() uses random access iterators, so it will not work on a list. STL lists use bidirectional iterators, but they also support their own sort method.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 31
Reputation: group256 is an unknown quantity at this point 
Solved Threads: 1
group256 group256 is offline Offline
Light Poster

Re: Sorting alphabetically a linked list

 
0
  #4
Aug 31st, 2009
No other answer???
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 22
Reputation: Topi Ojala is an unknown quantity at this point 
Solved Threads: 5
Topi Ojala's Avatar
Topi Ojala Topi Ojala is online now Online
Newbie Poster

Re: Sorting alphabetically a linked list

 
0
  #5
Aug 31st, 2009
One way springs to mind, might not be as effective as a linked list, but still...

First, create a struct of a data you want e.g. struct Actor, containing the info given to you. Then push these Actors in to a vector, deque, table, whatever you want, sorted by the first number ( string2int ).
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 147
Reputation: GDICommander is an unknown quantity at this point 
Solved Threads: 19
GDICommander's Avatar
GDICommander GDICommander is offline Offline
Junior Poster

Re: Sorting alphabetically a linked list

 
0
  #6
Aug 31st, 2009
You can implement a sort strategy on your linked list, like bubble sort, quick sort, merge sort. Some strategies do not require an additionnal container for the answer.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC