Solved

SortbyNumber NODEs

Posted on 1999-01-25
3
205 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_
Comment Utility
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
Comment Utility

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
Comment Utility
I was getting stuck on the for loop.  Thank you for helping me.  
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now