day41 继承和多态
手动实现一个循环顺序队列
#include <iostream>
using namespace std;
class queue
{
private:
char* arr;//存储数据的数组
int head;//记录队头的下标变量
int tail;//记录队尾的下标变量
int MAX;//队列的最大容量
public:
queue()
{
MAX=0;
head=0;
tail=0;
arr=new char[MAX];
cout<<"无参构造"<<endl;
}
queue(int M)
{
MAX=M;
head=0;
tail=0;
arr=new char[MAX];
cout<<"有参构造"<<endl;
}
~queue()
{
delete [] arr;
cout<<"析构函数"<<endl;
}
queue(const queue &other)
{
this->head=other.head;
this->tail=other.tail;
this->MAX=other.MAX;
this->arr=new char[MAX];
for(int i=0;i<MAX;i++)
{
arr[i]=other.arr[i];
}
cout<<"拷贝构造函数"<<endl;
}
queue & operator=(const queue &other)
{
if(this!=&other)
{
this->head=other.head;
this->tail=other.tail;
this->MAX=other.MAX;
this->arr=new char[MAX];
for(int i=0;i<MAX;i++)
{
arr[i]=other.arr[i];
}
}
cout<<"拷贝赋值函数"<<endl;
return *this;
}
//判空
bool queue_empty()
{
return head==tail;
}
//判满
bool queue_full()
{
return (tail+1)%MAX==head;
}
//size函数
int size()
{
return tail;
}
//push函数(入队尾插)
void push(char e)
{
if(queue_full())
{
cout<<"插入失败"<<endl;
return;
}
arr[tail]=e;
tail=(tail+1)%MAX;
cout<<"插入成功"<<endl;
}
//pop函数(出队头删)
void pop()
{
if(queue_empty())
{
cout<<"删除失败"<<endl;
return;
}
head=(head+1)%MAX;
cout<<"删除成功"<<endl;
}
//front函数(访问第一个元素)
char front()
{
if(queue_empty())
{
cout<<"队列为空,访问失败"<<endl;
return -1;
}
return arr[head];
}
//back函数(访问最后一个元素)
char back()
{
if(queue_empty())
{
cout<<"队列为空,访问失败"<<endl;
return -1;
}
return arr[tail-1];
}
//show函数
void show()
{
if(queue_empty())
{
cout<<"遍历失败"<<endl;
return;
}
for(int i=head;i!=tail;i=(i+1)%MAX)
{
printf("%c\t",arr[i]);
}
cout<<endl;
}
};
int main()
{
queue q1;
queue q2(8);
queue q3(q2);
queue q4;
cout<<q2.queue_empty()<<endl;
q2.push('f');
q2.push('c');
q2.push('a');
q2.push('4');
q2.push('b');
q2.push('c');
q2.push('d');
q2.push('e');
cout<<q2.queue_empty()<<endl;
cout<<q2.queue_full()<<endl;
q2.show();
q2.pop();
q2.show();
cout<<q2.queue_full()<<endl;
q4=q2;
q4.show();
return 0;
}
原文地址:https://blog.csdn.net/m0_72595899/article/details/145149319
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!