使用模板实现deque的问题

时间:2017-12-02 21:18:04

标签: c++ templates

最近我尝试使用模板实现续集。这是结果

#pragma once
#include <iostream>
using namespace std;

template<typename Type>
struct Node {
    Node();
    Node<Type>* next = NULL;
    Node<Type>* prev = NULL;
    Type data;
};

template<typename Type>
class deque{
 private:
     Node<Type>* front = NULL;
     Node<Type>* back = NULL;

 public:
     deque(Type key);
     ~deque();
     Node<Type>* back();
     Node<Type>* front();
     int size();
     bool empty();
     void push_front(Type key);
     void push_back(Type key);
     void pop_front();
     void pop_back();
     void print_in_order();
};


 template<typename Type>
  deque<Type>::deque(Type key)
{
     if (front != NULL || back != NULL)
         return
     else
     {
         front = new Node<Type>();
         front->data = key;
         prev = front;
     }
}

 template<typename Type>
  deque<Type>::~deque()
{
     Node<Type>* delPtr;
     while (front->prev != NULL)
    {
         delPtr = front;
         cout << "Deleted " << front->data<<endl;
         front = front->prev;
         delete delPtr;

    }
     back = NULL;
     delete front;
}

 template<typename Type>
  Node<Type>* deque<Type>::back()
{
      if(back!= NULL)
         return back;
     else 
          return NULL
}

 template<class Type>
  Node<Type>* deque<Type>::front()
{
     return front;
}

 template<typename Type>
  int deque<Type>::size()
{
     int counter = 0;
     Node<Type>* temp = front;
     while (temp->back != NULL)
    {
         counter++;
         temp = temp->back;
    }
     return counter;
}

 template<typename Type>
  bool deque<Type>::empty()
{
     if (front == NULL && back == NULL)
         return true;
     else
         return false;
}

 template<typename Type>
  void deque<Type>::push_front(Type key)
{
     Node<Type>* temp = new Node<Type>();
     temp->data = key;
     temp->prev = front;
     front->next = temp;
     front = temp;
}

 template<typename Type>
  void deque<Type>::push_back(Type key)
{
     Node<Type>* temp = new Node<Type>();
     temp->data = key;
     temp->next = back;
     back->prev = temp;
     back = temp;
}

 template<typename Type>
 inline void deque<Type>::pop_front()
{
     if (front != NULL) {
         Node<Type>* delPtr = front;
         front = front->prev;
         front->next = NULL;
         delete delPtr;
    }
     else
         cout << "There is no front in empty deque" << endl;
}

 template<typename Type>
  void deque<Type>::pop_back()
{
     if (back != NULL) {
         Node<Type>* delPtr = back;
         back = back->next;
         back->prev = NULL;
         delete delPtr;
    }
     else
         cout << "There is no back in empty deque " << endl;
}

 template<typename Type>
  void deque<Type>::print_in_order()
{
     if (front == NULL)
         return;
     Node<Type> temp = front;
     while (temp->prev != NULL) {
         cout << temp->data << " ";
         temp = temp->prev;
    }
     cout << endl;
}

 template<typename Type>
 inline Node<Type>::Node()
{
}

然后一些代码检查它是否正常工作

int main(){
    deque<int> qq(1);
    qq.push_front(2);
    qq.push_front(3);
    qq.print_in_order();
    return 0;
}

此代码不想编译。我曾尝试寻找类似的项目或错误,但无法找到有用的东西。因此,如果有人能解释出了什么问题,那将会很棒。 大多数错误都是一样的。例如,不允许使用back()front()因为它们不是函数而不是静态成员。或者back()front()作为课程模板的成员无法接收功能类型。希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:2)

之间存在冲突
Node<Type>* front = NULL;
Node<Type>* back = NULL;

Node<Type>* back();
Node<Type>* front();

首先,将Node<Type>* front = NULL;更改为Node<Type>* m_front = NULL;,将back更改为Node<Type> temp = front; 。您的代码还有其他错误,例如

Node<Type>* temp = front;

必须是

{{1}}

最后,您可以在ideone

上看到没有编译错误的代码

祝你好运!