#include <iostream>
#include <stack>
using namespace std;
template <typename T>
class mysta
{
private:
T * front;
T * end;
int len;
public:
//构造
mysta(int size=20){
front = new T[size];
end=front;
len=0;
}
//析构
~mysta()
{
delete []front;
front=end=nullptr;
}
//拷贝构造函数
mysta(const mysta &other)
{
int len=other.end-other.front;
this->front=new T[20];
memcpy(this->front,other.front,20*sizeof(T));
this->end=this->front+len;
}
//判空
bool empty()
{
return this->front == this->end;
}
//判满
bool full()
{
return len>=20?true:false;
}
//进栈
void push_back(const T val)
{
if(this->full())
{
cout<<"stark is full"<<endl;
return;
}
*end=val;
end++;
len++;
}
//出栈
void pop_back()
{
if(this->empty())
{
return;
}
--end;
len--;
}
//访问栈顶元素
T top() const
{
return *(end-1);
}
//计算元素个数
int size()
{
return len;
}
};
int main()
{
mysta<int> s1;
s1.push_back(5);
s1.push_back(4);
s1.push_back(3);
cout<<"size="<<s1.size()<<endl;
cout<<s1.top()<<endl;
s1.pop_back();
cout<<"size="<<s1.size()<<endl;
s1.push_back(6);
cout<<s1.top()<<endl;
mysta<int> s2=s1;
cout<<s2.top()<<endl;
s2.pop_back();
cout<<s2.top()<<endl;
cout << "Hello World!" << endl;
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
template <typename T>
class mysta
{
private:
int front;
int end;
int len;
T queue[20];
public:
//构造
mysta(){len=end=front=0;}
//析构
~mysta(){}
//拷贝构造函数
mysta(const mysta &other)
{
this->end=other.end;
this->front=other.front;
this->len=other.len;
memcpy(this->queue,other.queue,sizeof(other.queue));
}
//判空
bool empty()
{
return this->front == this->end;
}
//判满
bool full()
{
return (end+1)%20==front;
}
//进队列
void push_back(const T val)
{
if(this->full())
{
cout<<"queue is full"<<endl;
return;
}
queue[end]=val;
end=(end+1)%20;
len++;
}
//出队列
void pop_back()
{
if(this->empty())
{
return;
}
front=((++front)%20);
len--;
}
//访问第一个元素
T top() const
{
return queue[front];
}
T tail() const
{
return queue[(end+19)%20];
}
//计算元素个数
int size()
{
return len;
}
};
int main()
{
mysta<int> s1;
s1.push_back(5);
s1.push_back(4);
s1.push_back(3);
cout<<"size="<<s1.size()<<endl; //3
cout<<s1.top()<<endl; //5
cout<<s1.tail()<<endl; //3
s1.pop_back();
cout<<"size="<<s1.size()<<endl; //2
s1.push_back(6);
cout<<s1.top()<<endl; //4
cout<<s1.tail()<<endl; //6
mysta<int> s2=s1;
cout<<s2.top()<<endl; //4
s2.pop_back();
cout<<s2.top()<<endl; //3
cout << "Hello World!" << endl;
return 0;
}