Sorting A Linked List--- Please help

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

Join Date: Mar 2008
Posts: 3
Reputation: SEOT is an unknown quantity at this point 
Solved Threads: 0
SEOT SEOT is offline Offline
Newbie Poster

Sorting A Linked List--- Please help

 
0
  #1
Mar 7th, 2008
I need to sort a list with a bunch of numbers in separate columns. How would you sort a linked list with a bunch of numbers in separate columns. Anyone have any good examples to share or point me in the right direction. Please help - SEOT
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 3
Reputation: SEOT is an unknown quantity at this point 
Solved Threads: 0
SEOT SEOT is offline Offline
Newbie Poster

Re: Sorting A Linked List--- Please help

 
0
  #2
Mar 7th, 2008
Please.....Please.....Please.....I really need help with this. I can not find anything in all my C++ books about this, and there is hardly anything on the internet on it. I believe it can be done. Anybody.....somebody....helppppp - SEOT
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Sorting A Linked List--- Please help

 
0
  #3
Mar 7th, 2008
Originally Posted by SEOT View Post
Please.....Please.....Please.....I really need help with this. I can not find anything in all my C++ books about this, and there is hardly anything on the internet on it. I believe it can be done. Anybody.....somebody....helppppp - SEOT
Posts like this decrease your likelihood of being helped. Explain your situation better, post some code, even if you know it is incorrect, and try to explain what you think you need to do and where you're stuck.

Originally Posted by SEOT View Post
How would you sort a linked list with a bunch of numbers in separate columns.
The linked list has a bunch of numbers in separate columns? The input file does? What data does the linked list node hold? A single number? I'm guessing it's that and that there are numbers in an input file and you need to insert them into a sorted linked list. That's a guess though since you didn't actually say so. It certainly can be done.

So try to explain a little more clearly, show some code, and I'm sure someone will be happy to help.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Sorting A Linked List--- Please help

 
0
  #4
Mar 7th, 2008
The following code will sort a list of columns of numbers. The standard (STL) list class has a sort function, and you just have to make sure the list items have an operator<() assigned. Alternately you can write a compare function outside of the ItemRow class and feed it explicitly to sort(compareFunc).

Have a look at:
http://www.cplusplus.com/reference/stl/list/sort.html

  1. #include <iostream>
  2. #include <list>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. #define NUM_COLS 4
  8.  
  9. int sortByColumnN = 0;
  10.  
  11. class ItemRow{
  12. public:
  13. int columns[NUM_COLS];
  14.  
  15. // this lets us sort the items
  16. bool operator<(ItemRow &rhs){
  17. return columns[sortByColumnN] < rhs.columns[sortByColumnN];
  18. }
  19.  
  20. void display(){
  21. for(int i = 0; i < NUM_COLS; i++){
  22. cout << columns[i] << ", ";
  23. }
  24. cout << endl;
  25. }
  26. };
  27.  
  28. main(){
  29. list<ItemRow> myList;
  30.  
  31. for(int i = 0; i < 10; i++){
  32. ItemRow thisRow;
  33.  
  34. for(int j = 0; j < NUM_COLS; j++){
  35. thisRow.columns[j] = rand() % 30;
  36. }
  37.  
  38. myList.push_back(thisRow);
  39. }
  40.  
  41. // display unsorted
  42. for(list<ItemRow>::iterator it = myList.begin(); it != myList.end(); it++){
  43. it->display();
  44. }
  45.  
  46. myList.sort();
  47.  
  48. cout << endl << endl << endl;
  49.  
  50. // display sorted
  51. for(list<ItemRow>::iterator it = myList.begin(); it != myList.end(); it++){
  52. it->display();
  53. }
  54.  
  55. // let's sort by the second column
  56. sortByColumnN = 1;
  57. myList.sort();
  58.  
  59. cout << endl << endl << endl;
  60.  
  61. // display sorted
  62. for(list<ItemRow>::iterator it = myList.begin(); it != myList.end(); it++){
  63. it->display();
  64. }
  65.  
  66. system("pause");
  67. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 3
Reputation: SEOT is an unknown quantity at this point 
Solved Threads: 0
SEOT SEOT is offline Offline
Newbie Poster

Re: Sorting A Linked List--- Please help

 
0
  #5
Mar 7th, 2008
Will this work with a Linked List? I am trying to figure out how to sort a linked list. I have a bunch of numbers that I want to sort and find the highest and lowest numbers, so I want to sort the list in ascending or descending order. Then, I could grab the numbers off the top of the list. Can this be done with a Linked List?? Please help me- SEOT
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Sorting A Linked List--- Please help

 
0
  #6
Mar 7th, 2008
Originally Posted by SEOT View Post
Will this work with a Linked List? I am trying to figure out how to sort a linked list. I have a bunch of numbers that I want to sort and find the highest and lowest numbers, so I want to sort the list in ascending or descending order. Then, I could grab the numbers off the top of the list. Can this be done with a Linked List?? Please help me- SEOT

Well, is the linked already built and unsorted or are you building and sorting it as you go? You're going to have to get more specific about what you have already, what the goal is, and what you can't do. Where is the "bunch of numbers"? In a linked list already? In a file? In an array? Is this a linked list that you've designed yourself and that is in a struct? Put up some code.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Sorting A Linked List--- Please help

 
0
  #7
Mar 9th, 2008
Will this work with a Linked List?
The STL list I showed the example for is a linked list. So I guess the answer is: Yes.
Reply With Quote Quick reply to this message  
Reply

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




Views: 2346 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC