Hi All. I am trying to learn linked lists, and I am a little bit confused what the next pointer as well as the head pointer does. I was reading a tutorial online, and they gave this example.

#include <iostream>
#include <string>
using namespace std;

struct Family
{
	string nameOfFamilyMember;
	int ageOfFamilyMember;
	string jobOfFamilyMember;
	double salaryOfFamilyMember;
	Family* Next;
};
class ListOfFamilyMembers
{
private:
	int size;
public:
	Family* Head;
	
	ListOfFamilyMembers()
	{
		size=0;
		Head=NULL;
	}
	~ListOfFamilyMembers()
	{
	}
	Family* retrieve(int pos)
	{
		Family *current=new Family;
		current=Head;
		for(int i = count()-1; i > pos && current != NULL; i--) 
		{
			current = current->Next;

		}
						return current;	
	}
	int add(Family* item)
	{
		Family* test=new Family;
		test=item;
		test->Next=Head;
		Head=test;
		 return size++;
	}
	int count()
	{
		return size;
	}
};


void main()
{
	
	
	ListOfFamilyMembers* classs=new ListOfFamilyMembers();
	Family* structt;
	Family* pointer;

	structt=new Family;
	structt->nameOfFamilyMember="Steve";
	structt->ageOfFamilyMember=20;
	structt->jobOfFamilyMember="Student";
	structt->salaryOfFamilyMember=700;
	classs->add(structt);
	//classs->Head=structt;

	structt=new Family;
	structt->nameOfFamilyMember="Tara";
	structt->ageOfFamilyMember=29;
	structt->jobOfFamilyMember="Doctor";
	structt->salaryOfFamilyMember=220000;
	classs->add(structt);
	//classs->Head=structt;
		structt=new Family;
	structt->nameOfFamilyMember="Joy";
	structt->ageOfFamilyMember=24;
	structt->jobOfFamilyMember="Doctor";
	structt->salaryOfFamilyMember=120000;
	classs->add(structt);

	for(int i=0;i<classs->count();i++)
	{
		Family* one= classs->retrieve(i);
		cout<<"Family List: "<<endl;
		cout<<"Name: "<<one->nameOfFamilyMember<<endl;
		cout<<"Age: "<<one->ageOfFamilyMember<<endl;
		cout<<"Job: "<<one->jobOfFamilyMember<<endl;
		cout<<"Salary: $"<<one->salaryOfFamilyMember<<" per month"<<endl;
	cout<<endl;

	}


	system("PAUSE");
}

This is the part of the program above that I am a little bit confused on.

int add(Family* item)
	{
		Family* test=new Family;
		test=item;
		test->Next=Head;
		Head=test;
		 return size++;
	}

This is what I think it does. The variable test is created and is assigned a new Family. Meaning everything in the struct Family is initialized to NULL, and then assigned to the variable 'test'. Next 'item' which contains the names,jobs,ages, and salary of everything that we just passed to it is assigned to the variable 'test'. And I have no idea what

test->Next=Head;
Head=test;

does.
What I am thinking is that 'Head' which contains the entire list is assigned to Next which is empty right now because we initialized 'test' to a NEW family. And then 'test' which now contains the names,jobs,ages, and salary that we just passed to the function is now assigned to the 'Head'. So Head is appended to now include everything that we just passed to it.

Is my thinking correct?

And this is the last part of the code that I don't understand.

Family* retrieve(int pos)
	{
		Family *current=new Family;
		current=Head;
		for(int i = count()-1; i > pos && current != NULL; i--) 
		{
			current = current->Next;

		}
						return current;	
	}

This is what I think it does. So lets say '0' is passed to this function as the argument.
Then we create a new variable called 'current'. Then a NEW family is assigned to 'current' which means everything in the struct family is initialized to NULL and then assigned to 'current'. And then 'Head' which contains the entire list thus far, is assigned to 'current'. So now 'current' contains the entire list.

What I don't understand is what

current = current->Next;

is doing.
current->Next should be NULL right now, so why are we assigning it to 'current'? Wouldn't that make 'current' equal to NULL?

You need to understand the basics of pointers, before dealing with linked lists.

test->Next=Head;
Head=test;

It adds the newly created test node as the first node in the list. Before adding the list is

Head -> First -> Second -> Third...

After adding, it will be

Head -> Test -> First -> Second -> Third...

Edited 5 Years Ago by vidit_X: n/a

but wouldn't 'head' be overridden by 'test'?

That's what we want to do..

Head is just a pointer which point to the first element of our list. We make the test->next pointer to contain what's in head and then point head to test.

This article has been dead for over six months. Start a new discussion instead.