Linked list help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 12
Reputation: chris53825 is an unknown quantity at this point 
Solved Threads: 0
chris53825 chris53825 is offline Offline
Newbie Poster

Re: Singly-Linked Lists: Ultimate

 
0
  #1
Sep 16th, 2007
Hey guys,

I'm trying to make a linked list in C++ such that:

it creates a node for each student in a classroom, AND
that each student has it's own 4 nodes for their 4 test grades.

I've created two structs - studentNode, and gradeNode

studentNode containing "name" and "*next"
gradeNode containing "grade" and "*next"

I'm going crazy as I can get it to create the student nodes properly but I CANNOT get the grade nodes to correspond.

it should basically go like this...

[ pHead ]
|
V
[ studentName1 ] --> [ grade1]-->[grade2]-->...[x]

|
V
[ studentName2 ] --> [ grade1]-->[grade2]-->...[x]

Any ideas? Thank you..
__________________
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Linked list help

 
0
  #2
Sep 16th, 2007
>but I CANNOT get the grade nodes to correspond.
How so? You didn't post any code, so we have no idea what you've tried. The grade list is just another linked list. There's nothing different just because it's a member of a node in another list.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Linked list help

 
0
  #3
Sep 16th, 2007
Without seeing your code or header file it's impossible to know where the problem is, but my implementation would have each node with two pointers. One to students name and other to array of grades. That way each node is exactly 8 bytes, which reduces overhead for sorting and seach algo's.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 12
Reputation: chris53825 is an unknown quantity at this point 
Solved Threads: 0
chris53825 chris53825 is offline Offline
Newbie Poster

Re: Linked list help

 
0
  #4
Sep 16th, 2007
Originally Posted by Narue View Post
>but I CANNOT get the grade nodes to correspond.
How so? You didn't post any code, so we have no idea what you've tried. The grade list is just another linked list. There's nothing different just because it's a member of a node in another list.
"The grade list is just another linked list" .. how would I set this up though?

Here is a snippet of my code. Maybe I'm just approaching this incorrectly? i.e. should I be using only one struct? Pointers are driving me crazy!! =)

  1. using namespace std;
  2.  
  3. struct studentNode
  4. {
  5. string name;
  6. studentNode *next;
  7. };
  8. struct gradeNode
  9. {
  10. int grade;
  11. gradeNode *next;
  12. };
  13.  
  14.  
  15. int _tmain(int argc, _TCHAR* argv[])
  16. {
  17.  
  18. studentNode* sHead=NULL;
  19. studentNode* sTail=NULL;
  20.  
  21. gradeNode* gHead=NULL;
  22. gradeNode* gTail=NULL;
  23.  
  24. sHead = new studentNode;
  25. sTail = sHead;// We want to make sTail (the "current" pointer) be where the first node is!
  26. sTail->next=NULL;
  27.  
  28. gHead = new gradeNode;
  29. gTail=gHead;
  30. gTail->next=NULL;
  31.  
  32. //Counts number of students
  33. int count=0;
  34.  
  35. //reads in students from file
  36.  
  37. ifstream inputFile("students.txt",ios::in);
  38. if(!inputFile) {
  39. cerr << "File could not be opened" << endl;
  40. exit(1);
  41. }
  42. string fullName;
  43. string first;
  44. string last;
  45.  
  46. while(inputFile >> first >> last) {
  47. fullName = first + " " + last;
  48.  
  49. sTail->next=new studentNode;
  50. sTail=sTail->next;
  51.  
  52. //reads name from text file and inputs into "current" node
  53. sTail->name = fullName;
  54.  
  55.  
  56. //have grade point to where student is?
  57.  
  58. //
  59.  
  60. for(int i =0;i<4;i++) //initializes each student's grade score lists to NULL
  61. {
  62.  
  63. gTail->next=new gradeNode;
  64. gTail=gTail->next;
  65. gTail->grade=NULL;
  66.  
  67. }
  68.  
  69. count++;
  70. }
Last edited by chris53825; Sep 16th, 2007 at 1:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,707
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 275
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked list help

 
0
  #5
Sep 16th, 2007
Each student has a grade list, but each grade doesn't have a student list so the lists/nodes aren't mutually dependent. Therefore declare the gradeNode before the studentNode and have each studentNode contain a pointer to a gradeNode as a data member.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 12
Reputation: chris53825 is an unknown quantity at this point 
Solved Threads: 0
chris53825 chris53825 is offline Offline
Newbie Poster

Re: Linked list help

 
0
  #6
Sep 16th, 2007
So, having two different strucs as I have done already is fine?
Last edited by chris53825; Sep 16th, 2007 at 1:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Linked list help

 
0
  #7
Sep 16th, 2007
Like this:
  1. struct gradeNode {
  2. int grade;
  3. gradeNode *next;
  4. };
  5.  
  6. struct studentNode {
  7. string name;
  8. gradNode *grades;
  9. studentNode *next;
  10. };
For each student node, grades is the head of the grade list. That means each student node has an independent grade list.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 12
Reputation: chris53825 is an unknown quantity at this point 
Solved Threads: 0
chris53825 chris53825 is offline Offline
Newbie Poster

Re: Linked list help

 
0
  #8
Sep 16th, 2007
I've tried the above code (thank you very much!) and tried to work with it.. but I still have no success getting the "second" column of grades to be read in.

  1. struct gradeNode
  2. {
  3. int grade;
  4. gradeNode *next;
  5. };
  6.  
  7. struct studentNode
  8. {
  9. string name;
  10. gradeNode *grades; // For each student node,grades is the head of the grade list.
  11. //Thus, each student node has an independent grade list
  12. studentNode *next;
  13. };
  14.  
  15. int _tmain(int argc, _TCHAR* argv[])
  16. {
  17.  
  18. studentNode* sHead=NULL;
  19. studentNode* sTail=NULL;
  20.  
  21. //gradeNode* gHead=NULL;
  22. //gradeNode* gTail=NULL;
  23.  
  24. sHead = new studentNode;
  25. sTail = sHead;// We want to make sTail (the "current" pointer) be where the first node is!
  26. sTail->next=NULL;
  27.  
  28. gHead = new gradeNode;
  29. gTail=gHead;
  30. gTail->next=NULL;
  31.  
  32. //Counts number of students
  33. int count=0;
  34.  
  35. //reads in students from file
  36.  
  37. ifstream inputFile("students.txt",ios::in);
  38. if(!inputFile) {
  39. cerr << "File could not be opened" << endl;
  40. exit(1);
  41. }
  42. string fullName;
  43. string first;
  44. string last;
  45.  
  46.  
  47.  
  48. while(inputFile >> first >> last) {
  49. fullName = first + " " + last;
  50.  
  51. sTail->next=new studentNode;
  52. sTail=sTail->next;
  53.  
  54. //reads name from text file and inputs into "current" node
  55. sTail->name = fullName;
  56.  
  57. sTail->grades=new gradeNode;
  58.  
  59. for(int i =0;i<4;i++) //initializes each student's grade score lists to NULL
  60. {
  61.  
  62. sTail->grades->next=new gradeNode;
  63. sTail->grades=sTail->grades->next;
  64. sTail->grades->grade=NULL;
  65.  
  66. }
  67.  
  68. count++;
  69. }
Last edited by chris53825; Sep 16th, 2007 at 8:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Linked list help

 
0
  #9
Sep 17th, 2007
>sTail->grades->next=new gradeNode;
>sTail->grades=sTail->grades->next;
Okay, so you're creating a new node without a back link and then setting your only reference to the list to that new node. You should expect the list to be trashed unless you leave yourself a way to get back to the front of the list. That means either making it double linked, or using a separate pointer for making updates anywhere except the front. This link might help.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC