队列与循环队列(queue、deque)的循环数组描述

队列与循环队列

ADT

  • 队列
    bool empty();
    
    int size();

    T& front();

    void pop();

    void push(const T&);

  • 循环队列
    循环队列比队列要多几个功能—— 允许在头部插入,在尾部删除,查看尾部元素。
    bool empty();

    int size();

    T& front();

    T& back();

    void pop_front();

    void push_back(const T&);

    void pop_back();

    void push_front(const T&);

实现方案

实现方案一:
由于不需要随机访问,使用链表实现再合适不过。
既可以单独写一个类,也可以私有继承线性表的链表描述,“窄化”线性表的功能。

实现方案二:
如果仅仅“窄化”线性表的数组描述,插入删除的性能太大。
解决方案就是循环数组。

循环数组

关键点:

  • 将数组连接起来,形成一个环(就像一个12点的钟表一样),
  • 使用theFront、theBack来分别表示数组头部元素的前一个位置尾部元素的位置。
  • 当且仅当 theFront = theBack 时, 队列为空
  • 为了避免数组存满的时候 theFront = theBack 这种情况, 所以在任意时刻队列中的元素的个数最多为 arrayLength-1,这个时候需要去扩容。
  • 注意在循环数组的意义下,向后移动是 i = ( i + 1 ) % a r r a y L e n g t h i = (i+1)\%arrayLength i=(i+1)%arrayLength,
  • 向前移动是 i = ( i − 1 + a r r a y L e n g t h ) % a r r a y L e n g t h i = (i-1+arrayLength)\%arrayLength i=(i1+arrayLength)%arrayLength(减法时,为了避免为负,要加上模数)。
    实际上,c++的stl使用的是数组来实现队列,因为,在性能在数组描述确实更加优秀。

完整代码

//
// Created by MAC on 2020/10/7.
//

#ifndef DATASTRUCTURE_ARRAYDEQUE_H
#define DATASTRUCTURE_ARRAYDEQUE_H

#include <iostream>
using std::cout;

template<class T>
class ArrayDeque {
    
    
    static const int DEFAULT_CAPACITY = 8;
    T* arr;
//  length是队列的长度,也就是队列里面元素的个数, arrayLength是数组的长度
//  theFront 是真实队首元素的前一个位置, theBack就是队尾元素的位置
    int length, arrayLength, theFront, theBack;
    void expand();
    void checkEmpty(){
    
    
        if(empty()){
    
    
            throw "queue is empty.";
        }
    }

public:
    ArrayDeque();

    virtual ~ArrayDeque();

    virtual bool empty();

    virtual int size();

    virtual T& front();

    T& back();

    void pop_front();

    void push_back(const T&);

    void pop_back();

    void push_front(const T&);

};

template<class T>
ArrayDeque<T>::ArrayDeque() {
    
    
    length = 0;
    theFront = theBack = 0;
    arrayLength = DEFAULT_CAPACITY;
    arr = new T[DEFAULT_CAPACITY];
}

template<class T>
ArrayDeque<T>::~ArrayDeque() {
    
    
    delete [] arr;
}

template<class T>
bool ArrayDeque<T>::empty() {
    
    
    return theFront==theBack;
}

template<class T>
int ArrayDeque<T>::size() {
    
    
    return (arrayLength+theBack-theFront)%arrayLength;
}

template<class T>
T &ArrayDeque<T>::front() {
    
    
    checkEmpty();
    return arr[(theFront+1)%arrayLength];
}


template<class T>
T &ArrayDeque<T>::back() {
    
    
    checkEmpty();
    return arr[theBack];
}

template<class T>
void ArrayDeque<T>::pop_front() {
    
    
    checkEmpty();
    theFront = (theFront+1)%arrayLength;
//  队首元素要析构
    arr[theFront].~T();
    length--;
}

template<class T>
void ArrayDeque<T>::push_back(const T & t) {
    
    
    if( (theBack+1)%arrayLength == theFront ){
    
    
        expand();
    }
    theBack = (theBack+1)%arrayLength;
    arr[theBack] = t;
    length++;
}

template<class T>
void ArrayDeque<T>::pop_back() {
    
    
    checkEmpty();
    arr[theBack].~T();
    theBack = (theBack-1+arrayLength)%arrayLength;
    length--;
}

template<class T>
void ArrayDeque<T>::push_front(const T & t) {
    
    
    if( (theBack+1)%arrayLength == theFront){
    
    
        expand();
    }
    arr[theFront] = t;
    theFront = (theFront-1+arrayLength)%arrayLength;
    length++;
}

// 当且仅当 (theBack+1)%arrayLength == theFront 的时候,才会去扩容
// 此时 length = arrayLength - 1;
// 这个时候不仅仅要扩充容量,并且要重新调整元素的位置,满足"环形要求"
// 调整的方式有很多,不妨这样调整,所有元素靠前放置,也就是填充 [0,length-1] i,e, [0,arrayLength - 2]
// 具体来说,就要判断是否成环(头是否在尾的后面),来具体操作
template<class T>
void ArrayDeque<T>::expand() {
    
    
    T* newArray = new T[arrayLength*2];
    int start = (theFront+1)%arrayLength;
    if(start<2){
    
    
        for(int i = start;i<arrayLength-1+start;i++){
    
    
            newArray[i-start] = arr[i];
        }
    }else{
    
    
        int pos = 0;
        for(int i  = start;i<arrayLength;i++){
    
    
            newArray[pos++] = arr[i];
        }
        for(int i=0;i<=theBack;i++){
    
    
            newArray[pos++] = arr[i];
        }
    }
    theFront = arrayLength*2 - 1;
    theBack = arrayLength - 2;
    arrayLength = arrayLength*2;
    delete [] arr;
    arr = newArray;
}

#endif //DATASTRUCTURE_ARRAYDEQUE_H

对拍测试

//
// Created by MAC on 2020/10/7.
//
#include "ArrayQueue.h"
#include <queue>
#include "ArrayDeque.h"
#include <deque>


//   0 1 2 3 分别表示 size() front() push(x) pop();
//   下面开始对拍

//using namespace std;
//int main(){
    
    
//    srand(100);
//    deque<int> dq;
//    ArrayDeque<long> mq;
//    for(int i=0;i<100000;i++){
    
    
//        long x = rand();
//        long op = x%7;
//        switch (op) {
    
    
//            case 0:
//                if(dq.size()!=mq.size()) {
    
    
//                    cout<<"error"<<endl;
//                    return 0;
//                }
//                cout<<dq.size()<<" = "<<mq.size()<<endl;
//                break;
//            case 1:
//                if(!dq.empty()){
    
    
//                    if(mq.empty() || dq.front()!=mq.front()) {
    
    
//                        cout<<"error"<<endl;
//                        return 0;
//                    }
//                    cout<<dq.front()<<" = "<<mq.front()<<endl;
//
//                }
//                break;
//            case 2:
//                dq.push_back(x);
//                mq.push_back(x);
//                break;
//            case 3:
//                if(!dq.empty()){
    
    
//                    if(mq.empty()) {
    
    
//                        cout<<"error"<<endl;
//                        return 0;
//                    }
//                    dq.pop_back();
//                    mq.pop_back();
//                }
//                break;
//
//            case 4:
//                dq.push_front(x);
//                mq.push_front(x);
//                break;
//            case 5:
//                if(!dq.empty()){
    
    
//                    if(mq.empty()){
    
    
//                        cout<<"error"<<endl;
//                        return 0;
//                    }
//                    dq.pop_front();
//                    mq.pop_front();
//                }
//                break;
//            case 6:
//                if(!dq.empty()){
    
    
//                    if(mq.empty() || dq.back()!=mq.back()) {
    
    
//                        cout<<"error"<<endl;
//                        return 0;
//                    }
//                    cout<<dq.back()<<" = "<<mq.back()<<endl;
//
//                }
//                break;
//        }
//    }
//    cout<<endl<<"Accept"<<endl;
//    return 0;
//};

using namespace std;
int main(){
    
    
    srand(100);
    queue<int> dq;
    ArrayQueue<long> mq;
    for(int i=0;i<100000;i++){
    
    
        long x = rand();
        long op = x%4;
        switch (op) {
    
    
            case 0:
                if(dq.size()!=mq.size()) {
    
    
                    cout<<"error"<<endl;
                    return 0;
                }
                cout<<dq.size()<<" = "<<mq.size()<<endl;
                break;
            case 1:
                if(!dq.empty()){
    
    
                    if(mq.empty() || dq.front()!=mq.front()) {
    
    
                        cout<<"error"<<endl;
                        return 0;
                    }
                    cout<<dq.front()<<" = "<<mq.front()<<endl;

                }
                break;
            case 2:
                dq.push(x);
                mq.push(x);
                break;
            case 3:
                if(!dq.empty()){
    
    
                    if(mq.empty()) {
    
    
                        cout<<"error"<<endl;
                        return 0;
                    }
                    dq.pop();
                    mq.pop();
                }
                break;
        }
    }
    cout<<endl<<"Accept"<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44846324/article/details/108978912