顺序栈的模板类:
#include <iostream>
using namespace std;
template <typename T>
class Stacklist
{
protected:
T *data;
int MAXSIZE;
int top;
public:
Stacklist():MAXSIZE(10),top(-1)
{
data=new T[MAXSIZE];
cout<<"Stacklist::无参构造"<<endl;
}
Stacklist(int m):MAXSIZE(m),top(-1)
{
data=new T[MAXSIZE];
cout<<"Stacklist::有参构造"<<endl;
}
~Stacklist()
{
delete [] data;
cout<<"Stacklist::析构函数"<<endl;
}
//拷贝构造
Stacklist(const Stacklist &other):
data(new T(*(other.data))),
MAXSIZE(other.MAXSIZE),
top(other.top)
{
cout<<"Stacklist::拷贝构造"<<endl;
}
//拷贝赋值
Stacklist &operator=(const Stacklist &other)
{
if(this!=&other)
{
data=new T(*(other.data));
MAXSIZE=(other.MAXSIZE),
top=(other.top);
}
cout<<"Stacklist::拷贝赋值"<<endl;
return *this;
}
//访问栈顶元素
void stacklist_top()
{
cout<<"栈顶元素:"<<data[top]<<endl;
}
//访问任意位置的元素(从0位开始)
void stacklist_sub(int sub)
{
if(sub<0||sub>top)
return;
cout<<"sub位置的元素:"<<data[sub]<<endl;
}
//判断栈是否为空
bool empty()
{
return top==-1?1:0;
}
//判断栈是否为满
bool full()
{
return top==MAXSIZE?1:0;
}
//返回容纳元素个数
void stacklist_size()
{
cout<<"元素个数:"<<top+1<<endl;;
}
//向栈顶插入数据
void push(T m)
{
if(full())
return ;
data[++top]=m;
}
//删除栈顶元素
void stacklist_delete()
{
if(empty())
return;
top--;
}
//遍历栈
void show()
{
if(empty())
return;
for(int i=0;i<=top;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
};
int main()
{
Stacklist<int>s1(10);
int c;
for(int i=0;i<5;i++)
{
cin>>c;
s1.push(c);
}
s1.show();
s1.stacklist_size();
s1.stacklist_top();
s1.stacklist_sub(3);
s1.stacklist_delete();
s1.show();
Stacklist<char>s2;
s2.push('c');
s2.show();
Stacklist<char>s3;
s3=s2;
s3.show();
return 0;
}
循环顺序队列的模板类:
#include <iostream>
using namespace std;
template <typename T>
class Queue
{
protected:
int MAXSIZE;
T *data;
int front; //队头
int rear; //队尾
public:
Queue():MAXSIZE(10),front(0),rear(0)
{
data=new T[MAXSIZE];
cout<<"Queue::无参构造"<<endl;
}
Queue(int m):MAXSIZE(m),data(new T(MAXSIZE)),front(0),rear(0)
{
cout<<"Queue::有参构造"<<endl;
}
~Queue()
{
delete []data;
cout<<"Queue::析构函数"<<endl;
}
//拷贝构造
Queue(const Queue &other):
MAXSIZE(other.MAXSIZE),
data(new T(*(other.data))),
front(other.front),
rear(other.rear)
{
cout<<"Queue::拷贝构造"<<endl;
}
//拷贝赋值
Queue &operator=(const Queue &other)
{
if(this!=&other)
{
this->MAXSIZE=other.MAXSIZE;
this->data=new T(*(other.data));
this->front=other.front;
this->rear=other.rear;
}
cout<<"Queue::拷贝赋值"<<endl;
return *this;
}
//判断循环队列是否为空
bool empty()
{
return front==rear?1:0;
}
//判断循环队列是否为满
bool full()
{
return front==(rear+1)%MAXSIZE?1:0;
}
//访问第一个元素
void queue_first()
{
if(empty())
return;
cout<<"第一个元素是:"<<data[front]<<endl;
}
//访问最后一个元素
void queue_last()
{
if(empty())
return;
cout<<"最后一个元素是:"<<data[rear-1]<<endl;
}
//返回元素的个数
void size()
{
cout<<"循环队列元素的个数: "<<(MAXSIZE-front+rear)%MAXSIZE<<endl;
}
//插入元素(尾插)
void push(T m)
{
if(full())
return;
data[rear]=m;
rear=(rear+1)%MAXSIZE;
}
//删除元素(头删)
void queue_delete()
{
if(empty())
return ;
front=(front+1)%MAXSIZE;
}
//遍历循环队列
void show()
{
if(empty())
return;
for(int i=front;i!=rear;i=(i+1)%MAXSIZE)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
};
int main()
{
Queue<int>s1(10);
int m;
for(int i=0;i<5;i++)
{
cin>>m;
s1.push(m);
}
s1.show();
s1.queue_first();
s1.queue_last();
s1.size();
s1.queue_delete();
s1.show();
Queue<char>s2;
s2.push('p');
s2.show();
return 0;
}