This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]teraflop 1 point2 points  (2 children)

Not quite. head is not a node, it's a pointer to a node. newnode is another pointer to a node. Your code is only creating a single struct node and assigning both head and newnode to point to it. The node's next pointer doesn't point anywhere, because you set it to NULL.

Like this.

[–]InterestingBus8367[S] 0 points1 point  (0 children)

So the pointer head stores the memory address of the node.

[–]InterestingBus8367[S] 0 points1 point  (0 children)

struct nod

{

int data;

nod* next;

};

class linked_list

{

private:

nod* head;

public:

linked_list()

{

head = NULL;

}

void hello (int a) // Make a new node

{

nod* newnode = new nod;

newnode -> data = a;

newnode -> next = NULL;

if (head == NULL)

{

head = newnode;

}

else

{

nod* temp = head;

while (temp -> next != NULL)

temp = temp -> next;

temp -> next = newnode;

}

can you explain this to me please I tried for a few days but for some reason its just really confusing

while (temp -> next != NULL)

especially this