Node *link // is this saying link pointer points to Node? if not, what is it?
yes. specifically, link is a variable of type "node pointer". "link" is YOUR variable name, not some magic word.
The * means pointer. It can be on either side so node* x and node *x are the same thing.
The same is true of your similar & question. Its just personal preference which side you place it, but please be consistent in your code.
& has two uses. As a parameter to a function, and in other places that you are not worried about now, it means 'reference'. THIS IS AN EXTREMELY CRITICAL CONCEPT. A reference parameter can be changed inside the function and it changes the original thing passed into the function. For pointers, there is a critical concept here. The & here makes the POINTER VARIABLE able to be changed inside the function. However, pointers are an address, and the DATA THERE can be changed whether the POINTER VARIABLE has a reference or not. That is, the data pointed to can be changed with or without the & here: it is the data passed in. But if you change the pointer (eg a new, delete, increment, swap or similar operation) the & is required to make it point to a different piece of memory and retain that change in the original.
Do you understand all this? I can't stress it enough; this paragraph is extremely important. The rest of my drivel is just syntax.
The other use of & is addres of. eg int *x = &y; //take address of y, assign to pointer
nodePtr head; //so head is the pointer?
this is important to learning to read C++ code.
this says that head is a variable, and its type is nodePtr, same as int x means x is a variable, of type int. but what is nodePtr?
unsurprisingly, it is indeed a pointer to a type node: see your code line that made it:
typedef Node* nodePtr; //typedef is a little dated, we prefer 'using' now, but not critical they do same thing in this sense.
head = new Node; // what does it mean by "new Node"? Is it the second node that first one points to?
And now we get to another key concept. Pointers need to point to valid memory. new asks the operating system for memory. Every new needs to be paired with a delete statement, so later you need to delete node as well, literally: delete Node;
there is also an array form of new and delete, largely not used much now due to <vector> class. That looks like x = new int[100]; and delete[] x;
You are getting ahead of yourself. Linked lists are a little complex and trying to do one before you understand the syntax and what pointers ARE and how they work will be too hard.
Stop fooling with the linked list for a little bit and read this:
https://www.cplusplus.com/doc/tutorial/pointers/
and check out the first example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
// my first pointer
#include <iostream>
using namespace std;
int main ()
{
int firstvalue, secondvalue;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << "firstvalue is " << firstvalue << '\n';
cout << "secondvalue is " << secondvalue << '\n';
return 0;
}
|
Pointers are important. Dynamic memory is not used as much as it used to be now; the c++ containers handle that for us 99% of the time. But you need to know the basics in case you need to edit old code, write in pure C, or that 1% of the time case.