Queue.c
#include <iostream>
using namespace std;
class Queue
{
private:
int len;
int *data; //容器
int size=15; //最大容量
public:
Queue();
~Queue();
Queue &operator=(const Queue &other);
void push(int value);
void pop();
int get_size();
bool empty();
int front();
int back();
void show();
void junzi();
};
Queue::Queue()
{
this->len = 0;
data = new int [this->size];
int i = 0;
while (i<this->size) {
this->data[i]=0;
i++;
}
}
Queue::~Queue()
{
delete []data;
}
Queue &Queue ::operator=(const Queue &other)
{
int i =0;
if(this!=&other)
{
this->len=other.len;
this->size = other.size;
while( i<other.len){
data[i] = other.data[i];
i++;
}
data[other.len]='\0';
}
return *this;
}
void Queue::push(int value)
{
if(this->len>=this->size)
{
junzi();
}
data[this->len++] = value;
}
void Queue::pop()
{
int i = 0;
while(i<this->len)
{
data[i] = data[i+1];
i++;
}
this->len--;
}
int Queue::get_size()
{
return this->size;
}
bool Queue::empty()
{
return this->data==NULL||this->len==0;
}
int Queue::front()
{
if(empty())
{
return 0;
}
else{
return data[0];
}
}
int Queue::back()
{
if(empty())
{
return 0;
}
else{
return data[this->len-1];
}
}
void Queue::show()
{
int i = 0;
while(i<this->len)
{
cout<<data[i]<<" ";
i++;
}
cout<<endl;
}
void Queue::junzi()
{
int *temp = new int[this->size*2];
int i=0;
while(i<this->len)
{
temp[i] = data[i];
i++;
}
delete []data;
this->data= temp;//是this->data改变
this->size *=2;
}
int main()
{
Queue q;
q.push(10);
q.push(12);
q.push(13);
cout<<"q.front = "<<q.front()<<endl;
q.pop();
cout<<"q.back = "<<q.back()<<endl;
q.show();
cout<<"q.size = "<<q.get_size()<<endl;
Queue q1;
q1=q;
q1.show();
return 0;
}
思维导图