I Would Like Help Understanding Linked Lists.

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// LinkedListData - store data in a linked list of objects
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define NULLPTR 0   // use nullptr when supported

using namespace 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;
}
pNDS already is a pointer (address). If you did &pNDS that would give you a pointer that points to a pointer and that's not what you want.
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.
Thank you both so much! I really appreciate your help!
Topic archived. No new replies allowed.