Line 11: You're trying to assign an int to a node pointer.
Line 12: You're trying to assign a node pointer to an int passed by value.
1 2 3 4 5 6 7 8
node* start = nullptr; // Globals are initialized to null by default, but good style to explicitly initialize.
void insertbeg(int x)
{ node * temp = new node; // Need to allocate a new instance
temp->data = x; // Assign the data
temp->next = start; // Assign the link to the current start of the list
start = temp; // Assign this node to the new front of the list
}
I gave you comments explaining each line of my insertbeg().
Regarding display:
Line 18: Create a pointer (uninitialized) called np.
Line 19: Assign the np pointer to the first node (if any). If the list is empty, start and np will be null.
Line 20: Loop while np is pointing to a valid node. When the end of list is reached, np is null and the loop exits.
Line 21: cout the value of the data stored in the node.
Line 22: Update np to point to the next node (if any).