自己实现容器适配器的stack栈、queue队列
实现stack栈
#include <iostream>
using namespace std;
template <class T>
class Mystack
{
private:
T* first;
T* last;
T* end;;
public:
//构造函数
Mystack()
{
first = new T[1];
last = first;
end = first+1;
}
//析构函数
~Mystack()
{
delete []first;
first = last = end;
}
//返回容纳的元素数
int size()const
{
return this->last-this->first;
}
//拷贝构造函数
Mystack(const Mystack& other)
{
int n = other.end-other.first;
this->first = new T[sizeof(n)];
memcpy(this->first,other.first,n*sizeof(T));
this->last = this->first + other.size();
this->end = this->first + n;
}
//拷贝赋值函数
Mystack& operator=(const Mystack& other)
{
int n = other.end-other.first;
this->first = new T[sizeof(n)];
memcpy(this->first,other.first,n*sizeof(T));
this->last = this->first + other.size();
this->end = this->first + n;
return *this;
}
//判空
bool empty()
{
return this->first == this->last;
}
bool full()
{
return this->end == this->last;
}
//扩容
void greate()
{
int n = this->end-this->first;
T* temp = new T[2*n];
memcpy(temp,this->first,n*sizeof(T));
delete []first;
this->first = temp;
this->last = this->first+n;
this->end = this->first+2*n;
}
//向栈顶插入元素
void push(const T val)
{
if(this->full())
{
this->greate();
}
*last = val;
last++;
}
//删除栈顶元素
void pop()
{
if(this->empty())
{
cout<<"栈为空"<<endl;
return;
}
this->last--;
}
//访问栈顶元素
T top()
{
if(this->empty())
{
cout<<"栈为空"<<endl;
return (T)-1;
}
return *(this->last-1);
}
};
int main()
{
Mystack<int> s;
for(int i=1;i<=10;i++)
{
s.push(i);
}
cout<<s.top()<<endl;
s.pop();
cout<<s.top()<<endl;
cout<<"s.size="<<s.size()<<endl;
Mystack<int> s2 = s;
Mystack<int> s3;
s3 = s;
cout<<"s2:"<<s2.top()<<endl;
cout<<"s3:"<<s3.top()<<endl;
return 0;
}
实现queue队列
#include <iostream>
using namespace std;
template <class T>
class Myqueue
{
private:
T* first;
T* last;
T* end;;
public:
//构造函数
Myqueue()
{
first = new T[1];
last = first;
end = first+1;
}
//析构函数
~Myqueue(){}
//拷贝构造函数
Myqueue(const Myqueue& other)
{
int n = other.end-other.first;
this->first = new T[sizeof(n)];
memcpy(this->first,other.first,n*sizeof(T));
this->last = this->first + other.size();
this->end = this->first + n;
}
//拷贝赋值函数
Myqueue& operator=(const Myqueue& other)
{
int n = other.end-other.first;
this->first = new T[sizeof(n)];
memcpy(this->first,other.first,n*sizeof(T));
this->last = this->first + other.size();
this->end = this->first + n;
return *this;
}
//判空
bool empty()
{
return this->first == this->last;
}
//判满
bool full()
{
return this->end == this->last;
}
//扩容
void greate()
{
int n = this->end-this->first;
T* temp = new T[2*n];
memcpy(temp,this->first,n*sizeof(T));
delete []first;
this->first = temp;
this->last = this->first+n;
this->end = this->first+2*n;
}
//向队列尾部插入元素
void push(const T val)
{
if(this->full())
{
this->greate();
}
*last = val;
last++;
}
//删除首个元素
int pop()
{
if(this->empty())
{
cout<<"队列为空"<<endl;
return -1;
}
delete first;
first++;
return 0;
}
//访问第一个元素
T front()
{
return *(this->first);
}
//访问最后一个元素
T back()
{
return *(this->last-1);
}
int size()
{
return this->last-this->first;
}
};
int main()
{
Myqueue<int> q;
for(int i=1;i<=10;i++)
{
q.push(i);
}
q.pop();
cout<<"第一个元素:"<<q.front()<<endl;
cout<<"最后一个元素:"<<q.back()<<endl;
cout<<q.size()<<endl;
return 0;
}