C++实现队列与循环队列(queue、deque)的循环数组描述(1)

解决方案就是循环数组。

循环数组

关键点:

  • 将数组连接起来,形成一个环(就像一个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=(i−1+arrayLength)%arrayLength(减法时,为了避免为负,要加上模数)。

实际上,c++的stl使用的是数组来实现队列,因为,在性能在数组描述确实更加优秀。

完整代码


//

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

//

#ifndef DATASTRUCTURE_ARRAYDEQUE_H

#define DATASTRUCTURE_ARRAYDEQUE_H

#include

using std::cout;

template

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

ArrayDeque::ArrayDeque() {

length = 0;

theFront = theBack = 0;

arrayLength = DEFAULT_CAPACITY;

arr = new T[DEFAULT_CAPACITY];

}

template

ArrayDeque::~ArrayDeque() {

delete [] arr;

}

template

bool ArrayDeque::empty() {

return theFront==theBack;

}

template

int ArrayDeque::size() {

return (arrayLength+theBack-theFront)%arrayLength;

}

template

T &ArrayDeque::front() {

checkEmpty();

return arr[(theFront+1)%arrayLength];

}

template

T &ArrayDeque::back() {

checkEmpty();

return arr[theBack];

}

template

void ArrayDeque::pop_front() {

checkEmpty();

theFront = (theFront+1)%arrayLength;

// 队首元素要析构

arr[theFront].~T();

length–;

}

template

void ArrayDeque::push_back(const T & t) {

if( (theBack+1)%arrayLength == theFront ){

expand();

}

theBack = (theBack+1)%arrayLength;

arr[theBack] = t;

length++;

}

template

void ArrayDeque::pop_back() {

checkEmpty();

arr[theBack].~T();

theBack = (theBack-1+arrayLength)%arrayLength;

length–;

}

template

void ArrayDeque::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

void ArrayDeque::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

#include “ArrayDeque.h”

#include

// 0 1 2 3 分别表示 size() front() push(x) pop();

// 下面开始对拍

//using namespace std;

//int main(){

// srand(100);

// deque dq;

// ArrayDeque 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()){

总结

本文从基础到高级再到实战,由浅入深,把MySQL讲的清清楚楚,明明白白,这应该是我目前为止看到过最好的有关MySQL的学习笔记了,我相信如果你把这份笔记认真看完后,无论是工作中碰到的问题还是被面试官问到的问题都能迎刃而解!

MySQL50道高频面试题整理:

//

// }

// break;

// case 2:

// dq.push_back(x);

// mq.push_back(x);

// break;

// case 3:

// if(!dq.empty()){

总结

本文从基础到高级再到实战,由浅入深,把MySQL讲的清清楚楚,明明白白,这应该是我目前为止看到过最好的有关MySQL的学习笔记了,我相信如果你把这份笔记认真看完后,无论是工作中碰到的问题还是被面试官问到的问题都能迎刃而解!

MySQL50道高频面试题整理:

[外链图片转存中…(img-GMWBU38E-1719250836152)]

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值