简体   繁体   中英

Queue of Nodes (Linked-list) C++

I'm trying to store Nodes in a queue (STL), but I'm getting an error.

First of all, I want to know if the struct of the Node is right.

Really, I want to store the Nodes in order of integers (small to big), I heard of the priority queue, I tried to use it, but I was getting a big error so I went back to a queue.

Then I saw something about operator overloading of the Nodes but I don't re4ally geting how to use that. Will have have to make a Node.h file?

struct Node{
  int freq;
  char Char;
  struct Node *left;
  struct Node *right;
  Node(int freq, char Char){
  freq = freq;
  Char = Char;
 }
};

queue<Node*> list;
Node *a = new Node(2, '4');

list.push(a);

Node *e = list.pop();
cout << e->freq;

ERROR:

error: void value not ignored as it ought to be // Node *e = list.pop();

pop is void function. You need front :

list.pop();
Node *e = list.front();

Next problem is the constructor:

Node(int freq, char Char){
  this->freq = freq; // <------- 'this->' is added to access to right variables
  this->Char = Char; // <-------
 }

My suggestion is write your constructor like below:

Node(int freq, char Char) : freq(freq), Char(Char)
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM