943,828 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1032
  • C++ RSS
Aug 31st, 2009
0

Sorting alphabetically a linked list

Expand Post »
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.
Reputation Points: 7
Solved Threads: 22
Junior Poster
group256 is offline Offline
182 posts
since Apr 2009
Aug 31st, 2009
0

Re: Sorting alphabetically a linked list

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.
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
Aug 31st, 2009
0

Re: Sorting alphabetically a linked list

Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Aug 31st, 2009
0

Re: Sorting alphabetically a linked list

No other answer???
Reputation Points: 7
Solved Threads: 22
Junior Poster
group256 is offline Offline
182 posts
since Apr 2009
Aug 31st, 2009
0

Re: Sorting alphabetically a linked list

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 ).
Reputation Points: 13
Solved Threads: 18
Junior Poster in Training
Topi Ojala is offline Offline
60 posts
since May 2009
Aug 31st, 2009
0

Re: Sorting alphabetically a linked list

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.
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reading input from file
Next Thread in C++ Forum Timeline: c++ question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC