MosesA
MosesA

Reputation: 965

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();

Upvotes: 1

Views: 6942

Answers (1)

masoud
masoud

Reputation: 56479

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)
{
}

Upvotes: 3

Related Questions