栈的模板类
#include <iostream>
#include <cstring>
using namespace std;
#define MAXSIZE 20 //栈的大小
#define Elemtype char
template<typename T>
class Stack
{
protected:
T *str; //执行堆区空间
int top; //记录栈顶元素的位置
public:
Stack():str(new T[MAXSIZE-1]),top(-1){} //无参构造
Stack(int size):str(new T[size]),top(-1){} //有参构造
~Stack(){} //析构函数
Stack &operator=(const Stack &other) //拷贝赋值函数
{
this->top=other.top;
delete []str;
this->str=new T;
for(int i=0;i<=top;i++)
{
str[i]=other.str[i];
}
return *this;
}
T Top(); //类内声明,返回栈顶元素
bool Empty(); //类内声明,判断是否栈空
bool Full(); //类内声明,判断是否栈满
int Size(); //类内声明,返回容纳的元素数
void Push(T &elem); //类内声明,入栈操作
void Pop(); //类内声明,出栈操作
void Output(); //类内声明,遍历栈内元素
};
//类外定义,判断是否栈空
template <typename T>
bool Stack<T>::Empty() //<>为类型,这样是显性定义
{
if(top==-1)
return true;
return false;
}
//类外定义,返回栈顶元素
template <typename T>
T Stack<T>::Top()
{
if(Empty())
return false;
return *(str+top);
}
//类外定义,判断是否栈满
template <typename T>
bool Stack<T>::Full()
{
if(top=MAXSIZE-1)
return true;
}
//类外定义,返回容纳的元素数
template <typename T>
int Stack<T>::Size()
{
return top+1;
}
//类外定义,入栈操作
template <typename T>
void Stack<T>::Push(T &elem)
{
if(Full())
cout<<"栈满,入栈失败!"<<endl;
str[++top]=elem;
}
//类外定义,出栈操作
template <typename T>
void Stack<T>::Pop()
{
if(Empty())
cout<<"栈空,出栈失败!"<<endl;
top--;
}
//类外定义,,遍历栈内元素
template <typename T>
void Stack<T>::Output()
{
if(Empty())
cout<<"栈空,无元素输出!"<<endl;
for(int i=0;i<=top;i++)
{
cout<<*(str+i)<<endl;
}
}
int main()
{
Stack<Elemtype> s; //无参构造,显性定义
char ch;
Elemtype elem=0; //存储入栈元素,初始化为0
//循环入栈
do{
if(s.Full()) //栈满就结束
break;
cout<<"请输入入栈元素:"<<endl;
cin>>elem;
s.Push(elem);
cout<<"是否继续入栈?(Y/N)"<<endl;
cin>>ch;
}while(ch=='y'||ch=='Y');
//循环出栈
do
{
if(s.Empty()) //栈空就结束
break;
Pop();
cout<<"是否继续出栈?(Y/N)"<<endl;
cin>>ch;
}while(ch=='y'||ch=='Y');
cout<<"栈顶元素为:"<<s.Top()<<endl; //输出栈顶元素
cout<<"栈内元素有"<<s.Size()<<"个"<<endl; //栈内元素个数
cout<<"循环遍历输出:"<<endl; //循环遍历输出
s.Output();
int size;
cout<<"输入栈的大小"<<endl;
cin>>size;
Stack<Elemtype> m(size); //有参构造
m=s; //拷贝赋值
m.Output();
return 0;
}
循环队列的模板类
#include <iostream>
#include <cstring>
using namespace std;
#define MAXSIZE 20 //栈的大小
#define Elemtype char
template<typename T>
class Queue
{
protected:
T *str; //执行堆区空间
int front; //记录对头元素的位置
int rear; //记录对尾元素的位置
public:
Queue():str(new T[MAXSIZE-1]),front(0),rear(0){} //无参构造
~Queue(){} //析构函数
Queue &operator=(const Queue &other) //拷贝赋值函数
{
this->front=other.front;
this->rear=other.rear;
delete []str;
this->str=new T;
for(int i=0;i<=rear;i++)
{
str[i]=other.str[i];
}
return *this;
}
T Top(); //类内声明,返回队头元素
bool Empty(); //类内声明,判断是否队空
bool Full(); //类内声明,判断是否队满
int Size(); //类内声明,返回容纳的元素数
void Enqueue(T &elem); //类内声明,入队操作
void Dequeue(); //类内声明,出队操作
void Output(); //类内声明,遍历队内元素
};
//类外定义,判断是否队空
template <typename T>
bool Queue<T>::Empty() //<>为类型,这样是显性定义
{
if(front==rear)
return true;
return false;
}
//类外定义,返回队头元素
template <typename T>
T Queue<T>::Top()
{
if(Empty())
return false;
return *(str+front);
}
//类外定义,判断是否队满
template <typename T>
bool Queue<T>::Full()
{
if(front==(rear+1)%MAXSIZE)
return true;
}
//类外定义,返回容纳的元素数
template <typename T>
int Queue<T>::Size()
{
return (MAXSIZE+rear-front)%MAXSIZE;
}
//类外定义,入队操作
template <typename T>
void Queue<T>::Enqueue(T &elem)
{
if(Full())
cout<<"队满,入队失败!"<<endl;
str[rear]=elem;
rear=(rear+1)%MAXSIZE;
}
//类外定义,出队操作
template <typename T>
void Queue<T>::Dequeue()
{
if(Empty())
cout<<"队空,出队失败!"<<endl;
front=(front+1)%MAXSIZE;
}
//类外定义,,遍历队内元素
template <typename T>
void Queue<T>::Output()
{
if(Empty())
cout<<"栈空,无元素输出!"<<endl;
for(int i=front;i!=rear;i=(i+1)%MAXSIZE)
{
cout<<*(str+i)<<" ";
}
}
int main()
{
Queue<Elemtype> q; //无参构造,显性定义
char ch;
Elemtype elem=0; //存储入队元素,初始化为0
//循环入队
do{
if(q.Full()) //队满就结束
break;
cout<<"请输入入队元素:"<<endl;
cin>>elem;
q.Enqueue(elem);
cout<<"是否继续入队?(Y/N)"<<endl;
cin>>ch;
}while(ch=='y'||ch=='Y');
//循环出队
do
{
if(q.Empty()) //队空就结束
break;
q.Dequeue();
cout<<"是否继续出队?(Y/N)"<<endl;
cin>>ch;
}while(ch=='y'||ch=='Y');
cout<<"队头元素为:"<<q.Top()<<endl; //输出对头元素
cout<<"队内元素有"<<q.Size()<<"个"<<endl; //队内元素个数
cout<<"循环遍历输出:"<<endl; //循环遍历输出
q.Output();
Queue<Elemtype> m;
m=q; //拷贝赋值函数
q.Output();
return 0;
}
思维导图