get the last adress of queue node.

 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <stdlib.h>
struct qnode {
int value;
qnode* next;
qnode() {
next = NULL;
}
};
void insertNode(qnode*& ntop, qnode* newNode) {
if(ntop == NULL) {
ntop = newNode;
} else {
newNode->next = ntop;
ntop = newNode;
}
}
void npop(qnode*& ntop) {
if (ntop == NULL) {
exit(0);
}
qnode** last = &ntop;
while((*last) != NULL) {
last = &((*last)->next);
}
last = NULL;
/*
if(ntop->next == NULL) {
ntop = NULL;
} else {
npop(ntop->next);
}*/
}
void nvisit(qnode* ntop) {
if(ntop == NULL) {
std::cout<<"end.";
} else {
std::cout<<ntop->value<<"->";
nvisit(ntop->next);
}
}
int main() {
qnode* mquque = new qnode;
mquque->value = 12;
qnode* n1 = new qnode;
qnode* saved = mquque;
n1->value = 21;
insertNode(mquque, n1);
npop(mquque);
nvisit(mquque);
delete saved;
delete n1;
return 0;
}