Beginner Level Linked List Qs

Codes are included, and I only have specific questions about lines of code below, please kindly help if you know what's going on:

Node *link // is this saying link pointer points to Node? if not, what is it?

typedef Node* nodePtr; // Why is the asterisk close to "Node" but not the nodePtr??? What does it do?

insert(nodePtr& head, int data) // is nodePtr& head the address of head? And what is ampersand sign is close to nodePtr, not the head?

nodePtr head; //so head is the pointer?
head = new Node; // what does it mean by "new Node"? Is it the second node that first one points to?

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
#include <iostream> 
using namespace std; 

struct Node
{
    int data;
    Node *link;                
};
typedef Node* nodePtr;
void insert(nodePtr& head, int data);
int main()
{
   nodePtr head;
   head = new Node;
	head->data = 20;
 	head->link = NULL;
  
 
 insert (head, 30);
 nodePtr tmp;
 tmp = head;

 while(tmp->link != NULL)
 {
 	cout << tmp->data << endl;
 	tmp = tmp->link;
 }
 return 0;
}
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.
Last edited on
Thank you Jonnin. I learned a LOT from you.
Topic archived. No new replies allowed.