I tried following the queue headers from /reference/queue/queue/front/ , this code gives the expected output but crashes at the end. Why is it crashing?
if (tmp->left) // ; ***
x.push(tmp->left);
if (tmp->right) // ; ***
x.push(tmp->right);
The first thing to do, when there is a problem in the code is: compile the code with warnings enabled (ideally with more than one compiler), and pay attention to the warnings that are generated.
clang++
ain.cpp:27:23: warning: if statement has empty body [-Wempty-body]
if (tmp->left);
^
main.cpp:27:23: note: put the semicolon on a separate line to silence this warning
main.cpp:29:24: warning: if statement has empty body [-Wempty-body]
if (tmp->right);
^
main.cpp:29:24: note: put the semicolon on a separate line to silence this warning
g++
main.cpp: In function 'void levelorder(node*)':
main.cpp:27:23: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
if (tmp->left);
^
main.cpp:27:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if (tmp->left);
^~
main.cpp:28:13: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
x.push(tmp->left);
^
main.cpp:29:24: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
if (tmp->right);
^
main.cpp:29:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if (tmp->right);
^~
main.cpp:30:13: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
x.push(tmp->right);
^
source_file.cpp(27): warning C4390: ';': empty controlled statement found; is this the intent?
source_file.cpp(29): warning C4390: ';': empty controlled statement found; is this the intent?