C++ IntQueue类的设计

问题描述

problem


源代码

这里是用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;
}

测试结果:
在这里插入图片描述在这里插入图片描述


更多相关内容请参见

我的博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值