View Single Post
Join Date: Jan 2008
Posts: 3,757
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Help with Linked List Project!

 
0
  #2
Jul 2nd, 2008
I would actually take the nxt data attribute out of the Student class and put it in the StudentList class. It gives you far more options and is generally the way things are done What if you want a student to be in two or more separate lists? The "next" student could be different in different lists. Currently you can't do that. If you change it and put nxt in StudentList instead of Student, you'll be able to do that.

This is a prime candidate for the Merge Sort. You're merging two ordered lists, so you'll need to write the second half of a Merge Sort (the "Merge" part of it). Since the two lists are already sorted when you merge them, you don't have to worry about the first part of the Merge Sort (splitting a list into two and then recursively calling the Merge Sort on the individual lists till THEY are sorted), so you won't need to use recursion. You just need to do the Merge part.

Your job is even easier since you've presumably already written "insert" and "delete" functions. So you have two linked lists. Create a third linked list, initially empty. Set "current" to the front of the two original lists. Compare the "current" elements of the original two linked lists. Pick the Student with the smaller studentID. Insert that student into the third linked list. If you are deleting the original linked lists when you merge, delete that "current" element from the original list. If not just change its "current" pointer to the next node in the list. Keep the "current" pointer of the list that had the LARGER studentID where it is. Continually do this until you've gotten to the end of one of the original lists. Then put the remaining lists at the end of the new third list(no need for any more comparisons after that).

[EDIT]
Just reread my post. Don't put nxt in StudentList. I was thinking of having a class called StudentNode. Sometimes I make a class called Student, then a class called StudentNode, where Student would have these attributes:

  1. string sname; // Data Members
  2. string department;
  3. string major;
  4. int ID;

and StudentNode would have these:

  1. Student* student;
  2. Student* next;

and then StudentList would have these attributes:

  1. StudentNode* head;
  2. StudentNode* current;

But I hereby withdraw my earlier advice to put nxt in StudentList. If you don't want to have a StudentNode class and just stick nxt in Student, that's cool too. I just like making them separate. That way I can use my Student class completely independently of any linked list if I want to and I can create any number of different options of having several different lists that use Student.
[/EDIT]
Last edited by VernonDozier; Jul 2nd, 2008 at 9:59 pm.
Reply With Quote