bad pointer

 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
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <stdlib.h>
struct qnode {
int value;
qnode* next;
};
void insertNode(qnode*& ntop, qnode* newNode) {
if(ntop == NULL) {
ntop = newNode;
} else {
newNode->next = ntop;
ntop = newNode;
}
}
void visit(qnode* ntop) {
if (ntop == NULL) {
std::cout<<"end queue.";
} else {
std::cout<<ntop->value<<"->";
visit(ntop->next);
}
}
int main() {
qnode* mqueue = new qnode;
mqueue->value = 12;
qnode* inode = new qnode;
inode->value = 34;
insertNode(mqueue, inode);
visit(mqueue);
delete inode;
delete mqueue;
return 0;
}