c++ - Issue with pushing a Struct object into queue -


i have structure , want create queue of struct. have problem pushing new element. please.

#include <iostream> #include <queue> #include <deque> #include <list> using namespace std;  struct node { node *left, *right; int key; };  queue<node> q;  void updatelevel( node &n, int &level){ if(n.left!=null && n.right!=null){     level+=2;     q.push(n.left);     q.push(n.right);     }     else if(n.left!=null || n.right!=null){     level++;     if(n.left!=null) q.push(n.left);     if(n.right!=null) q.push(n.right); } }; void printtree(node root){ //if(root!=null){     q.push(root);     node n;     while(!q.empty()){         n =q.pop();         updatelevel(n,nextlevel);         curlevel--;         cout<<n.key;         if(curlevel<=0){             cout<<endl;             curlevel=nextlevel;             nextlevel=0;         }      }  //}     };     int main() {  node roottree;  printtree(roottree);  return 0; 

}

i calling function main. have got error checking in if condition null. please

at least 1 error here:

queue<node> q; //a queue of node 

however:

q.push(n.left); q.push(n.right); //n.left , n.right node*.  

you pushing node* queue accepts node.

you may have other errors, code have shown, @ least 1 of them.

edit: based on updated code.

node roottree; 

you did not define constructor in node, members of roottree not initialized. left , right not null start with. therefore, useless check null on them.

updatelevel(n,nextlevel); 

what nextlevel, seems not defined.

try set breakpoints in code , locate problem. error says if condition check has problem, root cause may somewhere else.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -