问题描述
源代码
这里是用vector数组实现:
//class.h
#include <vector>
using namespace std;
class IntQueue{
vector <int> element;
int sizes;
public:
IntQueue();
IntQueue(int sizes);
IntQueue(const IntQueue& x);
IntQueue& operator=(const IntQueue& x);
~IntQueue();
int getSizes();
void enterQueue(int value);
int quitQueue();
bool isEmpty();
};
//class.cpp
#include <iostream>
#include "class.h"
using namespace std;
IntQueue::IntQueue(){
vector <int> temp(1);
element = temp;
sizes = 1;
}
IntQueue::IntQueue(int sizes){
vector <int> temp(sizes);
element = temp;
this->sizes = sizes;
}
IntQueue::IntQueue(const IntQueue& x){
element = x.element;
sizes = x.sizes;
}
IntQueue& IntQueue::operator=(const IntQueue& x){
if(this == &x)
return *this;
element = x.element;
sizes = x.sizes;
}
IntQueue::~IntQueue(){};
int IntQueue::getSizes(){
return sizes;
}
void IntQueue::enterQueue(int value){
int temp = element.size();
if(temp == 0){
element.push_back(value);
sizes++;
}
else if(temp == sizes){
vector<int> t(sizes*2);
for(int i = 0;i<temp;i++)
t[i] = element[i];
element = t;
element[sizes++] = value;
}
else
element[sizes++] = value;
cout << "volume:" << element.size() << endl;
}
bool IntQueue::isEmpty(){
if(sizes == 0)
return true;
return false;
}
int IntQueue::quitQueue(){
bool temp = this->isEmpty();
if(temp){
cout << "this queue is empty!" << endl;
return 0;
}
int t = element[0];
vector <int> tt(sizes-1);
for(int i = 1;i < sizes;i++)
tt[i-1] = element[i];
element = tt;
sizes--;
return t;
}
//main.cpp
#include <iostream>
#include "class.h"
using namespace std;
int main()
{
IntQueue q1;
int flag = q1.quitQueue();
cout << "enter the queue:" << endl;
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(1);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(2);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(3);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(4);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(5);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(6);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(7);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
cout << endl << endl << endl;
cout << "quit the queue:" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
return 0;
}
测试结果:
这里使用动态数组实现:
//class.h
#include <vector>
using namespace std;
class IntQueue{
int* element;
int sizes;
int msize;
public:
IntQueue();
IntQueue(int sizes);
IntQueue(const IntQueue& x);
IntQueue& operator=(const IntQueue& x);
~IntQueue();
int getSizes();
void enterQueue(int value);
int quitQueue();
bool isEmpty();
};
//class.cpp
#include <iostream>
#include "class.h"
using namespace std;
IntQueue::IntQueue(){
element = new int[1];
sizes = 1;
msize = 1;
}
IntQueue::IntQueue(int sizes){
element = new int[sizes];
this->sizes = sizes;
msize = sizes;
}
IntQueue::IntQueue(const IntQueue& x){
element = new int[x.sizes];
for(int i =0;i < x.sizes;i++)
*(element+i) = x.element[i];
sizes = x.sizes;
msize = x.msize;
}
IntQueue& IntQueue::operator=(const IntQueue& x){
if(this == &x)
return *this;
element = new int[x.sizes];
for(int i =0;i < x.sizes;i++)
*(element+i) = x.element[i];
sizes = x.sizes;
msize = x.msize;
}
IntQueue::~IntQueue(){
delete []element;
};
int IntQueue::getSizes(){
return sizes;
}
void IntQueue::enterQueue(int value){
if(sizes == 0){
element = new int[1];
*element = value;
sizes++;
}
else if(sizes == msize){
int* p = new int[sizes*2];
for(int i = 0;i<sizes;i++)
*(p+i) = *(element+i);
delete[] element;
element = p;
p = nullptr;
msize = sizes*2;
element[sizes++] = value;
}
else
element[sizes++] = value;
cout << "volume:" << msize << endl;
}
bool IntQueue::isEmpty(){
if(sizes == 0)
return true;
return false;
}
int IntQueue::quitQueue(){
bool temp = this->isEmpty();
if(temp){
cout << "this queue is empty!" << endl;
return 0;
}
int t = *element;
int* p = new int[sizes-1];
for(int i = 1;i < sizes;i++)
*(p+i-1) = *(element+i);
delete[] element;
element = p;
p = nullptr;
sizes--;
return t;
}
//main.cpp
#include <iostream>
#include "class.h"
using namespace std;
int main()
{
IntQueue q1;
int flag = q1.quitQueue();
cout << "enter the queue:" << endl;
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(1);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(2);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(3);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(4);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(5);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(6);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
q1.enterQueue(7);
cout << q1.getSizes() << endl;
cout << "*********************************" << endl;
cout << endl << endl << endl;
cout << "quit the queue:" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
cout << q1.quitQueue() << endl;
cout << q1.getSizes() << endl;
cout << "------------------------------------------" << endl;
return 0;
}
测试结果:
更多相关内容请参见