Member Avatar for chris53825

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..
__________________

>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.

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.

Member Avatar for chris53825

>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!! =)

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++;
	}

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.

Member Avatar for chris53825

So, having two different strucs as I have done already is fine?

Like this:

struct gradeNode {
  int grade;
  gradeNode *next;
};

struct studentNode {
  string name;
  gradNode *grades;
  studentNode *next;
};

For each student node, grades is the head of the grade list. That means each student node has an independent grade list.

Member Avatar for chris53825

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.

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++;
	}

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.