您的位置:首页 > 编程语言 > C语言/C++

C++用模板实现双链表和队列

2016-06-11 18:52 609 查看
下面介绍模板类来实现双链表表和队列,由于队列是先进先出原则,所以可以通过调用双链表实现进队和出队等操作,Container<T>为“容器”,利用模板的模板参数--容器适配器调用双链表。下面用模板实现了顺序表的头插、尾插、头删、尾删、逆置、排序、去重、合并两个链表及增删查改等操作。如下所示:
   queue是容器配接器C的一个示例,容器配接器C将一些基础容器转换成类C的容器。容器配接器queue、stack、priority_queue——与标准模板库的其他处理是截然不同的。他们的方法和定义要调用基础容器类的方法。queue的基础类可以为list,list类中有size,empty,push_back,pop_front,front,back方法。deque类也可以作为基础类,而且是默认的基础类。vector类不能作为基础类,vector类没有pop_front方法。
函数列表:
empty() 如果队列空则返回真
front() 返回第一个元素
pop() 删除第一个元素
push() 在末尾加入一个元素
size() 返回队列中元素的个数
队列的实现入队、出队、判空、队头、队尾及队中元素个数等操作
template<class T,template<class>class Container=List>
class Queue
{
public:
bool Empty()//判空
{
return _con.Size() == 0;
}
size_t Size()//求队列大小
{
return _con.Size();
}
T& Front()//求出队头元素
{
assert(_con._head);
return _con._head->_data;
}
T& Back()//求出队尾元素
{
assert(_con._tail);
return _con._tail->_data;
}
void PushBack(const T& x)//入队
{
_con.PushBack(x);
}
void PopFront()//出队
{
_con.PopFront();
}
void PrintQueue()
{
_con.PrintList();
}
protected:
Container<T> _con;
};
双链表及队列的测试用例如下:
void Test1()
{//尾插尾删
//List<int> s1;
//s1.PushBack(1);
//s1.PushBack(2);
//s1.PushBack(3);
//s1.PrintList();
List<string> s2;
s2.PushBack("xxxxxx");
s2.PushBack("yyyyyy");
s2.PushBack("zzzzzz");
s2.PrintList();

List<string> s4(s2);
s4.PrintList();
List<string> s3;
s3 = s2;
s3.PrintList();
//s2.PopBack();
//s2.PopBack();
//s2.PrintList();
//s2.PopBack();
//s2.PopBack();
//s2.PrintList();
}

void Test2()
{//头插头删
List<int> s1;
s1.PushFront(1);
s1.PushFront(2);
s1.PushFront(3);
s1.PrintList();
List<string> s2;
s2.PushFront("xxxxxx");
s2.PushFront("yyyyyy");
s2.PushFront("zzzzzz");
s2.PrintList();
s2.PopFront();
s2.PopFront();
s2.PrintList();
s2.PopFront();
s2.PopFront();
s2.PrintList();
}

void Test3()
{//查找,增加,删除,逆置
List<string> s;
s.PushBack("xxxxxx");
s.PushBack("yyyyyy");
s.PushBack("zzzzzz");
s.PrintList();
s.Insert(s.Find("xxxxxx"), "ffffff");
s.PrintList();
s.Erase(s.Find("ffffff"));
s.PrintList();
s.Reverse();
s.PrintList();
}

void Test4()
{//冒泡排序,去重
List<string> s1;
s1.PushBack("yyyyyy");
s1.PushBack("zzzzzz");
s1.PushBack("xxxxxx");
List<string> s2;
s2.PushBack("qqqqqq");
s2.PushBack("ffffff");
s2.PushBack("ssssss");
s1.PrintList();
s2.PrintList();
s1.Sort();
s2.Sort();
s1.PrintList();
s2.PrintList();
List<string> s3;
s3.PushBack("yyyyyy");
s3.PushBack("qqqqqq");
s3.PushBack("zzzzzz");
s3.PushBack("xxxxxx");
s3.PushBack("qqqqqq");
s3.PushBack("xxxxxx");
s3.PushBack("xxxxxx");
s3.PushBack("ssssss");
s3.Sort();
s3.PrintList();
s3.Unique();
s3.PrintList();
}

void Test5()
{//合并链表,某一链表插入到pos后面
List<string> s1;
s1.PushBack("xxxxxx");
s1.PushBack("rrrrrr");
s1.PushBack("pppppp");
s1.PushBack("qqqqqq");
List<string> s2;
s2.PushBack("wwwwww");
s2.PushBack("vvvvvv");
s2.PushBack("uuuuuu");
s1.Sort();
s1.PrintList();
s2.Sort();
s2.PrintList();
//s1.Merge(s2);
//s1.PrintList();
//s1.Sort();
//s1.PrintList();
//s2._head->_prev = NULL;
//s1._tail->_next = NULL;
ListNode<string>* pos = s1.Find("rrrrrr");
s1.Splice(pos, s2);
s1.PrintList();
pos->_prev = NULL;
pos->_next = NULL;
s2._head->_prev = NULL;
s2._tail->_next = NULL;
}
void Test6()
{
//Queue<int> s1;
//s1.PushBack(1);
//s1.PushBack(2);
//s1.PushBack(3);
//cout << s1.Empty() << endl;
//cout << "Front->" << s1.Front() << endl;
//cout << "Back->" << s1.Back() << endl;
////s1.PopFront();
////s1.PrintQueue();
////s1.PopFront();
////s1.PopFront();
////s1.PopFront();
////s1.PrintQueue();

Queue<string> s2;
s2.PushBack("ssssss");
s2.PushBack("xxxxxx");
s2.PushBack("yyyyyy");
s2.PushBack("zzzzzz");
cout << s2.Empty() << endl;
cout << "Size->" << s2.Size() << endl;
cout << "Front->" << s2.Front() << endl;
cout << "Back->" << s2.Back() << endl;
string out = s2.Front();
//访问队列
while (!s2.Empty())
{
cout << s2.Front() << "->";
s2.PopFront();
}
cout << "NULL" << endl;
}
本文出自 “Scen” 博客,请务必保留此出处http://10741357.blog.51cto.com/10731357/1751926
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: