方法一:模板实现队列
#pragma once
#include<iostream>
using namespace std;
#include"List.h"
template<class T,class Container = List<T> >
class Queue
{
public:
Queue()
{}
void Push(const T& data)
{
_con.PushBack(data);
}
void Pop()
{
_con.PopFront();
}
T& Front()
{
return _con.Front();
}
const T& Front()const
{
return _con.Front();
}
T& Back()
{
return _con.Back();
}
const T& Back()const
{
return _con.Back();
}
size_t Size()const
{
return _con.Size();
}
bool Empty()const
{
return _con.Empty();
}
private:
Container _con;
};
void TestQueue()
{
Queue<int> q;
q.Push(1);
q.Push(2);
q.Push(3);
q.Push(4);
q.Push(5);
cout << q.Front() << endl;
cout << q.Back() << endl;
cout << q.Size() << endl;
q.Pop();
q.Pop();
cout << q.Front() << endl;
cout << q.Back() << endl;
cout << q.Size() << endl;
}
方法二:将第二个模板参数给成一个模板类
#pragma once
#include<iostream>
using namespace std;
#include"List.h"
template<class T, template<class> class Container = List>
class Queue
{
public:
Queue()
{}
void Push(const T& data)
{
_con.PushBack(data);
}
void Pop()
{
_con.PopFront();
}
T& Front()
{
return _con.Front();
}
const T& Front()const
{
return _con.Front();
}
T& Back()
{
return _con.Back();
}
const T& Back()const
{
return _con.Back();
}
size_t Size()const
{
return _con.Size();
}
bool Empty()const
{
return _con.Empty();
}
private:
Container<T> _con;
};
void TestQueue1()
{
Queue<int> q;
q.Push(1);
q.Push(2);
q.Push(3);
q.Push(4);
q.Push(5);
cout << q.Front() << endl;
cout << q.Back() << endl;
cout << q.Size() << endl;
q.Pop();
q.Pop();
cout << q.Front() << endl;
cout << q.Back() << endl;
cout << q.Size() << endl;
}
具体的List相关操作请查看模板实现带头结点的双向循环链表List