Solved

SortbyNumber NODEs

Posted on 1999-01-25
3
206 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

914 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

13 Experts available now in Live!

Get 1:1 Help Now