line 18 p_head = p_curr; memory leak
line 35 while ( p_itr != NULL ) given that line 31 is commented out, this condition makes no sense
line 39 delete p_itr;next iteration you'll try to dereference p_itr, but you just delete it
Node *p_head = new Node; //¿what is the purpose of this line?
Node *p_curr = new Node;
p_head = p_curr;
Node *p_curr = new Node;
Node *p_head = p_curr;
At line 13 you've got one node, but the user has not inputted anything yet. That node is uninitialized.
Later in line 23, after the user input, you create another node. So you've got two nodes but only one valid value.
The last node that has valid data is pointing to a node that points to NULL. You've got one extra superfluous node.
line 38: delete p_itr; ¿what about all the others nodes that were allocated? ¿would you not delete them?
//head.val is invalid, the first value is at head.next->val
Node head; //note that is an object, not a pointer
Node *top = &head;
while( std::cin>>val and val not_eq 0 ){
top->next = new Node(val); //push_back
top = top->next;
}
//traversal
Node *it = &head;
while( it->next ){
std::cout << it->next->val < '\n';
it = it->next;
}
a) especial case for first insertion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Node *head = NULL; //now it is a pointer
Node *top = head;
while( std::cin>>val and val not_eq 0 ){
if( top == NULL )
top = head = new Node(val);
else{
top->next = new Node(val); //push_back
top = top->next;
}
}
//traversal
Node *it = &head;
while( it ){
std::cout << it->val < '\n';
it = it->next;
}
This is simply a debugging HW, I have to fix it as is. I know how to make a linked list via class, the issue here is I have to understand why and how to fix the simple program.