Solved

SortbyNumber NODEs

Posted on 1999-01-25
3
207 Views
Last Modified: 2010-04-02
In a linked list representation of a data stucture that represents a student and their grade; how can a sort be implemented to link the list in descending order according to their score? struct STUD { char *name; int score; STUD* next}; class ROSTER{ STUD *claslist; void sortbyNumber();};
0
Comment
Question by:184197
3 Comments
 
LVL 3

Expert Comment

by:_Scotch_
ID: 1185132
Sorting a linked list is incredibly slow.  You would do better
do collect an array of pointers and then use qsort() - then do
whatever sorted processing you want on your array of pointers.


0
 
LVL 1

Accepted Solution

by:
MCoroli earned 100 total points
ID: 1185133

void ROSTER::sortbyNumber()
{
   STUD *tmp;
   STUD *min;
   for(min=claslist;min->next;min=min->next)
   {
      if(min->score>classlist->score)
      {
          tmp=classlist;
          classlist=min;
          min=tmp;
      }
   }
}

I didn't compile it; but the idea is simple.
If you ever arranged arrays of main types - it will be obviously for you. In loop untill last element is reached(min->next!=NULL) compare score of current node and first in list. If necessary-change them to each other.
Hope that will help you.
Regards.
0
 

Author Comment

by:184197
ID: 1185134
I was getting stuck on the for loop.  Thank you for helping me.  
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question