943,991 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1638
  • C++ RSS
Sep 16th, 2007
0

Re: Singly-Linked Lists: Ultimate

Expand Post »
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..
__________________
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chris53825 is offline Offline
12 posts
since Sep 2007
Sep 16th, 2007
0

Re: Linked list help

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 16th, 2007
0

Re: Linked list help

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.
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Sep 16th, 2007
0

Re: Linked list help

Click to Expand / Collapse  Quote originally posted by Narue ...
>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!! =)

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chris53825 is offline Offline
12 posts
since Sep 2007
Sep 16th, 2007
0

Re: Linked list help

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Sep 16th, 2007
0

Re: Linked list help

So, having two different strucs as I have done already is fine?
Last edited by chris53825; Sep 16th, 2007 at 1:51 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chris53825 is offline Offline
12 posts
since Sep 2007
Sep 16th, 2007
0

Re: Linked list help

Like this:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 16th, 2007
0

Re: Linked list help

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chris53825 is offline Offline
12 posts
since Sep 2007
Sep 17th, 2007
0

Re: Linked list help

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: vector addition
Next Thread in C++ Forum Timeline: Graph a line, totally lost





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC