Hello! I have been learning C++ for a couple months now and am needing help to understand linked lists. I get the general ideas surrounding it in the code like in this code pNDS->pHead passes it to the first class in the list, but I am not sure where or how the address of the objects are being passed around in this code (view line 57 and 58 to see what I mean). I don't see an & operator anywhere. Can someone explain how the address is being procured and passed around? Thank you all in advance!
// LinkedListData - store data in a linked list of objects
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define NULLPTR 0 // use nullptr when supported
usingnamespace std;
// NameDataSet - stores a person's name (these objects
// could easily store any other information
// desired).
class NameDataSet
{
public:
string sName;
// the link to the next entry in the list
NameDataSet* pNext;
};
// the pointer to the first entry in the list
NameDataSet* pHead = NULLPTR;
// add - add a new member to the linked list
void add(NameDataSet* pNDS)
{
// point the current entry to the beginning of
// the list
pNDS->pNext = pHead;
// point the head pointer to the current entry
pHead = pNDS;
}
// getData - read a name and social security
// number; return null if no more to
// read
NameDataSet* getData()
{
// read the first name
string name;
cout << "Enter name:";
cin >> name;
// if the name entered is 'exit'...
if (name == "exit")
{
// ...return a null to terminate input
return NULLPTR;
}
// get a new entry and fill in values
NameDataSet* pNDS = new NameDataSet;
pNDS->sName = name;
pNDS->pNext = NULLPTR; // zero link
// return the address of the object created
return pNDS;
}
int main(int nNumberofArgs, char* pszArgs[])
{
cout << "Read names of students\n"
<< "Enter 'exit' for first name to exit"
<< endl;
// create (another) NameDataSet object
NameDataSet* pNDS;
while (pNDS = getData())
{
// add it to the list of NameDataSet objects
add(pNDS);
}
// to display the objects, iterate through the
// list (stop when the next address is NULL)
cout << "\nEntries:" << endl;
for(NameDataSet *pIter = pHead;
pIter; pIter = pIter->pNext)
{
// display name of current entry
cout << pIter->sName << endl;
}
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
Your getData function is creating a new instance of your NameDataSet in memory, and then returning a pointer to that instance (effectively it is returning the address). NameDataSet* pNDS on line 53 is creating a pointer to a NameDataSet object. When you put "= new NameDataSet" you are creating an actual space in memory to store the instance, then saving the address of that new instance to pNDS.
You are then saving the student name to the instance, and returning the address of it by returning the pointer. So line 69 is setting the address of your local copy of pNDS and then calling your add(...) function to push it into the front of your list by first assigning the pNext pointer (defined on line 18) to the current head (line 29) and then assigning the current pHead pointer to the new instance you just created (line 32). In that way, head will always point to the last element inserted, and you can access the "next" element in your list by using pHead->pNext;
The for loop used to print the information is using another pointer (pIter) to point to the current element you are looking at starting with the head. Then it is stepping through and reassigning pIter to the next element by doing pIter = pIter->pNext; until pIter is NULL and the loop exits.