簡體   English   中英

節點隊列(鏈接列表)C ++

[英]Queue of Nodes (Linked-list) C++

我正在嘗試將節點存儲在隊列(STL)中,但出現錯誤。

首先,我想知道Node的結構是否正確。

確實,我想按整數順序(從小到大)存儲節點,聽說了優先級隊列,嘗試使用它,但是遇到了一個很大的錯誤,所以我回到了隊列。

然后,我看到了有關節點的運算符重載的一些信息,但是我沒有真正了解如何使用它。 將不得不制作一個Node.h文件嗎?

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: void value not ignored as it ought to be // Node *e = list.pop();

popvoid函數。 您需要front

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

下一個問題是構造函數:

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

我的建議是如下編寫您的構造函數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM