There is one thing regarding the usage of pointers here I do not understand Cell *curr = head; here takes the address of head and what does it do with it?
Cell *ConvertToListIter(Vector<int>& vector)
{
Cell *head = new Cell;
head->next = NULL;
head->value = vector[0];
Cell *curr = head;
for (int i = 1; i < vector.size(); i++) {
Cell *newCell = new Cell;
newCell->next = NULL;
newCell->value = vector[i];
curr->next = newCell;
newCell = curr;
}
return head;
}
Cell *curr = head;does not take the address ofhead— it takes the value ofhead. It just so happens thatheadis a pointer:So, after
Cell *curr = head;has executed, bothcurrandheadpoint to the same thing.Edit per your comment:
That’s correct. The two pointers point to a single object in memory. That object needs to be
deleteed only once. In fact, attempting todeleteit twice will result in Undefined Behavior, and very often it will crash your program.