封装栈和队列
队列
#include <iostream>
#include <cstring>
using namespace std;
class Myqueue
{
private:
int data[256];
int size=0;
public:
Myqueue(){}//无参构造
~Myqueue(){}//析构
//拷贝赋值
Myqueue & operator=(const Myqueue &other)
{
if(this!=&other)
{
memset(this->data,0,this->size);
for(int i=0;i<size;i++)
{
this->data[i]=other.data[i];
}
}
return *this;
}
//判空
bool queue_empty()
{
return size==0;
}
//访问第一个元素
int queue_front()
{
if(queue_empty())
{
cout<<"队列为空"<<endl;
return 0;
}
return data[0];
}
//访问最后一个元素
int queue_back()
{
if(queue_empty())
{
cout<<"队列为空"<<endl;
return 0;
}
return data[size-1];
}
int queue_size()
{
return size;
}
//队尾插入元素
Myqueue & queue_push(int n)
{
if(size==256)
{
cout<<"队列已满"<<endl;
return *this;
}
data[size]=n;
size++;
return *this;
}
//删除首个元素
Myqueue & queue_pop()
{
if(queue_empty())
{
cout<<"队列已空"<<endl;
return *this;
}
for(int i=0;i<size-1;i++)
{
data[i]=data[i+1];
}
data[size-1]=0;
size--;
return *this;
}
//遍历输出队列
void show()
{
if(queue_empty())
{
cout<<"队列空"<<endl;
return;
}
for(int i=0;i<size;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
};
int main()
{
Myqueue t,p;
t.queue_push(1);
t.queue_push(2);
t.queue_push(3);
t.show();
cout<<"********************"<<endl;
p.queue_push(7);
p.queue_push(8);
p.queue_push(9);
p.show();
cout<<"*********************"<<endl;
t=p;
t.show();
cout<<"队列第一个元素 "<<t.queue_front()<<" 队列最后一个元素 "<<t.queue_back()<<" 元素个数 "<<t.queue_size()<<endl;
cout<<"*********************"<<endl;
t.queue_pop();
t.show();
cout<<"队列第一个元素 "<<t.queue_front()<<" 队列最后一个元素 "<<t.queue_back()<<" 元素个数 "<<t.queue_size()<<endl;
return 0;
}
栈
#include <iostream>
#include <cstring>
using namespace std;
//封装栈
class Mystack
{
private:
int data[256];//数组
int top=-1;//栈顶位置
public:
Mystack(){};//无参构造
~Mystack(){};//析构函数
//拷贝赋值
Mystack & operator=(const Mystack &other)
{
if(this!=&other)
{
memset(data,0,this->top+1);//初始化原栈
this->top=other.top;//修改栈顶位置
//修改各元素
for(int i=0;i<=other.top;i++)
{
this->data[i]=other.data[i];
}
}
return *this;
}
//判空
bool stack_empty()
{
return top==-1;//判断栈顶是否为-1
}
//返回栈顶位置
int stack_size()
{
return top+1;//返回栈顶位置
}
//访问栈顶元素
int stack_top()
{
//判空
if(stack_empty())
{
cout<<"栈为空"<<endl;
return 0;
}
return data[top];
}
//入栈
Mystack & stack_push(int n)
{
if(top==255)
{
cout<<"栈已满"<<endl;
return *this;
}
//栈顶位置加1,栈顶位置赋值
top++;
this->data[top]=n;
return *this;
}
//出栈
Mystack & stack_pop()
{
if(stack_empty())
{
cout<<"栈为空"<<endl;
return *this;
}
data[top]=0;//栈顶元素为0
top--;//栈顶位置减一
return *this;
}
//遍历输出栈
void show()
{
if(stack_empty())
{
cout<<"栈空"<<endl;
return;
}
for(int i=0;i<=top;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
};
int main()
{
Mystack t,p;
t.stack_push(1);
t.stack_push(2);
t.stack_push(3);
t.show();
cout<<"********************"<<endl;
p.stack_push(7);
p.stack_push(8);
p.stack_push(9);
p.show();
cout<<"*********************"<<endl;
t=p;
t.show();
cout<<"栈顶元素 "<<t.stack_top()<<" 元素个数 "<<t.stack_size()<<endl;
cout<<"*********************"<<endl;
t.stack_pop();
t.show();
cout<<"栈顶元素 "<<t.stack_top()<<" 元素个数 "<<t.stack_size()<<endl;
return 0;
}