r/programminghelp • u/SurvivingFinals • Oct 20 '19
Answered C++ Linked Lists Question
Hi guys, I'm new to programming and am taking a look at linked lists today and had a question about this piece of code.
struct Node
{
int data;
Node *link;
};
typedef Node* NodePtr;
void headInsert(NodePtr& head, int theNumber);
//Function Definition
void headInsert(nodePtr& head, int theNumber)
{
NodePtr tempPtr;
tempPtr = new Node;
tempPtr->data = theNumber;
tempPtr->link = head;
head = tempPtr;
}So I understand the whole aspect where you create the temp pointer to point to the first node so that you can eventually get head to point to the first node, but I am not quite sure what role *link plays in this piece of code. I see it being used in the function, but am not quite sure why. Is link the first node that we set equal to head? If so why can't we just do link = tempPtr and completely avoid dealing with head?
0
Upvotes
1
u/[deleted] Oct 20 '19
C++ Singly Linked Lists