具有特定模板的双链接队列

Double Linked Queue with specific template

本文关键字:链接 队列      更新时间:2023-10-16

我打算做一个基于双链接双链接的程序,其中节点有双链接,我想做的是每个节点都是一个"具有名称和优先级的文档"(优先级 = 1 个从头部插入,prio. = 2 从后面插入(。实际上我的代码是这样的,但我没有得到所有内容都可以使用我的模板,因为我不知道如何在 Node.h 中正确声明它。

文档.h

template<class T>
class Document
{
 private:
  T name;
  T pri;
 public:
  Document(T name, T pri);
};
 template <class T> 
 Document<T>::Document(T name, T pri){
   this->name = name;
   this->pri = pri;      
}

节点.h

#include "Document.h"
template <class T> class Node{
 public:
  Node();
  Node(const T& item);
  (...other functions...)
private:
 Document<T> document;  //WHERE MY PROBLEM IS!
 Node<T>* next;
 Node<T>* previous;    
};
template <class T> 
Node<T>::Node(const T& item) {
    this->document= new Document<T>(item); //Problem with the declaration
    this->next = nullptr;
    this->previous = nullptr;
}

LinkedDeque.h

#include "Node.h"
#include "Document.h"
template <class T> class LinkedDeque {
 public:
  LinkedDeque();
  void insertFront(const T& element);
  void insertRear(const T& element);
  (... other functions ...)
private:
  Node<T>* front; //Front de la cua
  Node<T>* rear; //rear de la cua
  int num_elements;
};
template <class T>
void LinkedDeque<T>::insertFront(const T& element){
    Node<T>* aux = new Node<T>(element);
    (...rest of the function...)
}

主.cpp

LinkedDeque<Document<string>> deque;
cin >> name >> pri;
Document<string> document(name,pri);
if (pri == "1") {
  deque.insertFront(document);
}else {
  deque.insertRear(document);
}

我看到的第一件事是您将构造函数声明为

     Document(T name, T pri)

但是您正在尝试仅使用类似

    this->document= new Document<T>(item)

所以我建议将名称和优先级传递给您的构造函数,这将起作用。