| | |
Linked list help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2007
Posts: 12
Reputation:
Solved Threads: 0
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..
__________________
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..
__________________
•
•
Join Date: Sep 2007
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
>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.
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)
using namespace std; struct studentNode { string name; studentNode *next; }; struct gradeNode { int grade; gradeNode *next; }; int _tmain(int argc, _TCHAR* argv[]) { studentNode* sHead=NULL; studentNode* sTail=NULL; gradeNode* gHead=NULL; gradeNode* gTail=NULL; sHead = new studentNode; sTail = sHead;// We want to make sTail (the "current" pointer) be where the first node is! sTail->next=NULL; gHead = new gradeNode; gTail=gHead; gTail->next=NULL; //Counts number of students int count=0; //reads in students from file ifstream inputFile("students.txt",ios::in); if(!inputFile) { cerr << "File could not be opened" << endl; exit(1); } string fullName; string first; string last; while(inputFile >> first >> last) { fullName = first + " " + last; sTail->next=new studentNode; sTail=sTail->next; //reads name from text file and inputs into "current" node sTail->name = fullName; //have grade point to where student is? // for(int i =0;i<4;i++) //initializes each student's grade score lists to NULL { gTail->next=new gradeNode; gTail=gTail->next; gTail->grade=NULL; } count++; }
Last edited by chris53825; Sep 16th, 2007 at 1:21 pm.
Like this:
For each student node, grades is the head of the grade list. That means each student node has an independent grade list.
C++ Syntax (Toggle Plain Text)
struct gradeNode { int grade; gradeNode *next; }; struct studentNode { string name; gradNode *grades; studentNode *next; };
I'm here to prove you wrong.
•
•
Join Date: Sep 2007
Posts: 12
Reputation:
Solved Threads: 0
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)
struct gradeNode { int grade; gradeNode *next; }; struct studentNode { string name; gradeNode *grades; // For each student node,grades is the head of the grade list. //Thus, each student node has an independent grade list studentNode *next; }; int _tmain(int argc, _TCHAR* argv[]) { studentNode* sHead=NULL; studentNode* sTail=NULL; //gradeNode* gHead=NULL; //gradeNode* gTail=NULL; sHead = new studentNode; sTail = sHead;// We want to make sTail (the "current" pointer) be where the first node is! sTail->next=NULL; gHead = new gradeNode; gTail=gHead; gTail->next=NULL; //Counts number of students int count=0; //reads in students from file ifstream inputFile("students.txt",ios::in); if(!inputFile) { cerr << "File could not be opened" << endl; exit(1); } string fullName; string first; string last; while(inputFile >> first >> last) { fullName = first + " " + last; sTail->next=new studentNode; sTail=sTail->next; //reads name from text file and inputs into "current" node sTail->name = fullName; sTail->grades=new gradeNode; for(int i =0;i<4;i++) //initializes each student's grade score lists to NULL { sTail->grades->next=new gradeNode; sTail->grades=sTail->grades->next; sTail->grades->grade=NULL; } count++; }
Last edited by chris53825; Sep 16th, 2007 at 8:35 pm.
>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.
>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.
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- help implementing singly linked list (C++)
- How to read in a sentence and insert in to linked list? (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why doesn't this remove the last node in a linked list? (C++)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
- Linked List (C++)
- help by sorting a simply linked list (C)
Other Threads in the C++ Forum
- Previous Thread: vector addition
- Next Thread: Graph a line, totally lost
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






