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

Recommended Answers

All 6 Replies

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

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.

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.

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

#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");
   }

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.