利用模板实现分别实现一个顺序表和链表,再用着两个顺序表链表来实现栈和队列的容器适配器
顺序表和链表代码博客链接:https://mp.csdn.net/postedit/83096307
stack.h
#pragma once
#include"Seqlist.h"
#include"list.h"
//使用容器适配器实现栈
template<class T,class Container=Seqlist<T>>
class stack
{
public:
void Push(const T& x)
{
_con.PushBack(x);
}
void Pop()
{
_con.PopBack();
}
const T & Top()
{
}
bool Empty()
{
return _con.Empty();
}
size_t Size()
{
return _con.Size();
}
void Print()
{
_con.Print();
}
private:
Container _con;
};
void Teststack()
{
stack<int, Seqlist<int>> s1;
s1.Push(1);
s1.Push(2);
s1.Push(3);
s1.Push(4);
s1.Push(5);
s1.Print();
s1.Pop();
s1.Print();
std::cout << s1.Size() << std::endl;
std::cout << s1.Empty() << std::endl;
stack<char, Seqlist<char>> s2;
s2.Push('s');
s2.Push('h');
s2.Push('i');
s2.Push('o');
s2.Print();
}
queue.h
#pragma once
#include"Seqlist.h"
#include"list.h"
//使用容器适配器实现队列
template<class T, class Container = list<T>>
class Queue
{
public:
void PopFront()
{
_con.PopFront();
}
void PushBack(const T& x)
{
_con.PushBack(x);
}
size_t Size()
{
return _con.Size();
}
bool Empty()
{
return _con.Empty();
}
void Print()
{
_con.Print();
}
private:
Container _con;
};
void TestQueue()
{
Queue<int, list<int>> q1;
q1.PushBack(1);
q1.PushBack(2);
q1.PushBack(3);
q1.PushBack(4);
q1.PushBack(5);
q1.Print();
q1.PopFront();
q1.Print();
std::cout << q1.Size() << std::endl;
std::cout << q1.Empty() << std::endl;
}