Linked List problem!!
no compiling error but it crushes!
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
|
struct Node{
int val;
Node* nxt, *prev;
};[\code]
[code]
void headInsertion(Node*& head, int x)
{
Node* n = new Node;
n->val=x;
if(head ==NULL) {
n->prev=head;
n->nxt=head;
head=n;
} else {
n->prev=head;
n->nxt=head->nxt;
head->nxt->prev=n;
head->nxt=n;
}
}
|
Line 22: if you insert the second node, head->nxt
will be NULL and hence it will crash.
Topic archived. No new replies allowed.