Linked List Class node

Hello. I am studying this sample code for linked list class node implementation.
I've studied this code for hours and I am not 100% sure how this code keeps track of the head node.

Here;s what I know so far, and if you want to add/correct anything feel free to do so:

Thank you 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
class Node
{
public:
  Node();                                       // constructor of class node without arguments
  Node(int, Node*);                           //constructor with arguments of int type and a pointer node type
  Node* next() const;                  
  void setNext(Node*);                        //member fun, returning a pointer of node type
  void setKey(int);
  int  getKey() const;
  int done;
private:
  Node* ptr;
  int key;
};

Node::Node()                            // constructor intialisation
{
  ptr = NULL;
  key = -1;
  done = 1;
}
Node::Node(int tkey, Node* tnode)                   // intialisation to private members
{
  ptr = tnode;
  key = tkey;
  done = 1;
}
Node* Node::next() const                                  
{
  return ptr;
}
int Node::getKey() const                                // returning the key to be inserted
{
  return key;
}
void Node::setNext(Node* tnode)                     // setting the link to next node in ptr
{
  ptr = tnode;
}
void Node::setKey(int tkey)                      // setting the value to be inserted in key {
  key = tkey;
}
  
void insert(Node* head, int key)                  // function taking the starting pointer of list and key to be inserted {
  Node* temp  = new Node(key, NULL);      // creating a new node by calling function  Node(int, Node*)
  Node* index;
  if(key < head->getKey())          // comparing the key to be inserted and present starting key of the list
    {
      index = new Node(key, head);     // if true, make the key to be inserted as the starting key
      head = index;                                 
      return;
    }
  index = head;
  while(index->next()!=NULL && index->next()->getKey() < key)       //else traverse the list until key to be inserted is greater than the values in the list.  
    {                                                                                               


 index = index->next();  // when found the place to insert the key, store the pointer in index.

 }   


temp->setNext(index->next());                                         // point temp->link to index.
  index->setNext(temp);
  return;
}
void printList(Node * head)                                          // print the list, starting form head of the list
{
  Node* index = head;                                              // storing value head in pointer index
  cout << index->getKey() << ", ";                             
  while(index->next()!= NULL)                       //till the end of list or index->next is not equal to NULL
    {
      index = index->next();                             //traversing node by node
      cout << index->getKey() << ", ";
    }
  cout << "\n";
Do you have a question?
How does this program keep track of node?

Thank you for helping
It looks like you commented the code pretty well, I am not sure how you're confused. Which objects are you concerned about, or which lines still confuse you?
I know the link list always keeps track of the head node, but how does it do so in this program.

If you enter 1 -> 2 -> 3-> 4 -> 5, how does it know that the head node is 1.

Because of line 12? If you lose he head node, you've basically screwed yourself over, but as long as you have the head node, you have the whole list.
Oh. Is this the reason why in Node::Node(), ptr is NULL? Since this is first node, its pointer is null because it's not pointing to anything. Is this right?
Yes, there are no nodes so the head pointer is null.
Okay, everything makes sense now. Thanks for helping
Topic archived. No new replies allowed.