1.封装栈
template <typename T>
class my_stack{
public:
my_stack():buttom(NULL),top(NULL),len(0){} //无参构造
my_stack(int num,T data):buttom(new T[num]),len(num),maxsize(num){ //有参构造
for(int i=0;i<len;i++){
this->buttom[i]=data;
}
top=num-1;
}
my_stack(const my_stack& s):buttom(new T[s.len]),len(s.len),maxsize(s.maxsize){ //拷贝构造
for(int i=0;i<len;i++){
this->buttom[i]=s.buttom[i];
}
this->top=len-1;
}
my_stack& operator=(const my_stack& s){
if(this->top!=-1){ //有原始数据
delete buttom[top];
top--;
len--; //释放原有内存
buttom=NULL;
}
this->bottom = new T[s.len];
this->len=s.len;
this->maxsize=s.maxsize;
for(int i=0;i<this->len;i++){
this->bottom[i]=s.bottom[i];
}
this->top=len-1;
}
bool empty(){
if(this->top==-1){
return true; //空为真
}else{
return false; // 非空为假
}
}
int size(){
return this->len;
}
void push(T data){
int tempnum=top+1;
if(maxsize==tempnum){
T *temp=new T[this->len]; //创建临时空间存储原始数据
for(int i=0;i<len;i++){
temp[i]=buttom[i];
}
delete []buttom; //释放原有空间
this->buttom=NULL;
this->maxsize=maxsize*2; //扩容
this->buttom=new T[this->maxsize];
for(int i=0;i<len;i++){ //重新赋值
buttom[i]=temp[i];
}
buttom[top+1]=data; //入栈
top++;
len++;
}else{
buttom[top+1]=data;
top++;
len++;
}
}
void pop(){
if(top!=-1){
top--;
len--;
}else{
cout<<"STACK IS NULL"<<endl;
}
}
T& get_top(){ //获取栈顶元素
return buttom[top];
}
void show(){ //遍历
for(int i=0;i<len;i++){
cout<<buttom[i]<<" ";
}
cout<<endl;
}
void show_mem(){
cout<<"top="<<top<<endl;
cout<<"len="<<len<<endl;
cout<<"maxsize="<<maxsize<<endl;
}
~my_stack(){
while(top!=-1){
delete &buttom[top];
top--;
}
buttom=NULL;
}
private:
T *buttom;
int top;
int len;
int maxsize;
};
主函数
2.封装队列
template <typename T>
class my_queue{
public:
my_queue():base(NULL),front(0),rear(0),len(0),maxsize(0){}
my_queue(int num,T data):base(new T[num+1]),front(0),rear(num),len(num),maxsize(num+1){
for(int i=0;i<len;i++){
base[i]=data;
}//满
}
my_queue(const my_queue& q):base(new T[q.len+1]),front(q.front),rear(q.rear),len(q.len),maxsize(q.maxsize){
for(int i=0;i<len;i++){
base[i]=q.base[i];
}
}
my_queue& operator=(const my_queue& q){
if(this->base!=NULL){
delete []base;
}
front=0;
rear=0;
this->base=new T[q.len];
len=q.len;
maxsize=q.maxsize;
for(int i=0;i<len;i++){
base[i]=q.base[i];
}
front=q.front;
rear=q.rear;
}
int size(){
return len;
}
bool empty(){
if(rear==front){
return true; //空
}else{
return false; //非空
}
}
T& get_front(){
if(rear!=front)
return base[front];
}
T& back(){
if(rear!=front)
return base[rear-1];
}
void pop(){
if(rear==front){
cout<<"QUEUE IS NULL"<<endl;
}else{
front++;
len--;
}
}
void push(T data){
if((rear+1)%maxsize==front){ //满
T *newspace=new T[len]; //存储原始值
for(int i=0;i<len;i++){
newspace[i]=base[i];
}
delete []base; //释放原始内存
this->maxsize=2*maxsize; //扩容
base=new T[maxsize+1];
for(int i=0;i<len;i++){
base[i]=newspace[i];
}
base[rear]=data;
rear=(rear+1)%maxsize;
len++;
}else{
base[rear]=data;
rear=(rear+1)%maxsize;
len++;
}
}
void show(){
for(int i=0;i<len;i++){
cout<<base[i]<<" ";
}
cout<<endl;
}
void show_mem(){
cout<<"len="<<len<<endl;
cout<<"maxsize="<<maxsize<<endl;
}
~my_queue(){
delete []base;
base=NULL;
}
private:
T *base;
int front;
int rear;
int len;
int maxsize;
};