| | |
Sorting A Linked List--- Please help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 3,842
Reputation:
Solved Threads: 503
•
•
•
•
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
•
•
•
•
How would you sort a linked list with a bunch of numbers in separate columns.
So try to explain a little more clearly, show some code, and I'm sure someone will be happy to help.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
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
Have a look at:
http://www.cplusplus.com/reference/stl/list/sort.html
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <list> #include <cmath> using namespace std; #define NUM_COLS 4 int sortByColumnN = 0; class ItemRow{ public: int columns[NUM_COLS]; // this lets us sort the items bool operator<(ItemRow &rhs){ return columns[sortByColumnN] < rhs.columns[sortByColumnN]; } void display(){ for(int i = 0; i < NUM_COLS; i++){ cout << columns[i] << ", "; } cout << endl; } }; main(){ list<ItemRow> myList; for(int i = 0; i < 10; i++){ ItemRow thisRow; for(int j = 0; j < NUM_COLS; j++){ thisRow.columns[j] = rand() % 30; } myList.push_back(thisRow); } // display unsorted for(list<ItemRow>::iterator it = myList.begin(); it != myList.end(); it++){ it->display(); } myList.sort(); cout << endl << endl << endl; // display sorted for(list<ItemRow>::iterator it = myList.begin(); it != myList.end(); it++){ it->display(); } // let's sort by the second column sortByColumnN = 1; myList.sort(); cout << endl << endl << endl; // display sorted for(list<ItemRow>::iterator it = myList.begin(); it != myList.end(); it++){ it->display(); } system("pause"); }
•
•
Join Date: Mar 2008
Posts: 3
Reputation:
Solved Threads: 0
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
•
•
Join Date: Jan 2008
Posts: 3,842
Reputation:
Solved Threads: 503
•
•
•
•
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.
![]() |
Similar Threads
- Problem sorting a simple linked list (C)
- Quicksorting linked list - simple algorithm (C)
- Inserting in a sorted linked list(sorted alphabetically) (C++)
- linked list library (C)
- Link List Sorting Problem (C++)
- help by sorting a simply linked list (C)
Other Threads in the C++ Forum
- Previous Thread: ChildForm
- Next Thread: Finding previously entered data from a text file
Views: 2346 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






