Dream home

Use the technology to change the way of life

双向循环队列解析

 

 一,定义

       双向循环队列是在循环队列的基础之上,对头上添加了插入操作,在队尾添加了删除操作,最后此循环队列在对头和队尾均有插入和删除的功能。

二:判断队列的一些操作需注意的事情

       假如队列的数据结构为

       typedef struct node {

                            datatype    data[maxsize];

                            int        front;

                            int         rear;

                   };

      struct node Q;

      队列长度:(Q.rear-Q.front+maxsize)%maxsize;

      队列满:(Q.rear+1)%maxsize==Q.front

      对头向前移动:Q.front=(Q.front+1)%maxsize

      队尾向前移动:Q.rear=(Q.rear+1)%maxsize

      队空:Q.rear==Q.front

三:循环队列的操作定义

 

   class Deque {   

    private: 
             int maxSize;   
             int left;   
             int right;   
             int nItems;   
             long[] myDeque;   
public:
    //constructor   
     Deque(int maxSize){   
        this.maxSize = maxSize;   
        this.myDeque = new long[this.maxSize];   
        this.nItems = 0;   
        this.left = this.maxSize;   
        this.right = -1;   
    }   
    //insert a number into left side   
    void insertLeft(long n){   
        if(this.left==0) this.left = this.maxSize;   
        this.myDeque[--this.left] = n;   
        this.nItems++;   
    }   
    //insert a number into right side   
    void insertRight(long n){   
        if(this.right==this.maxSize-1) this.right = -1;   
        this.myDeque[++this.right] = n;   
        this.nItems++;   
    }   
    //remove from left   
     long removeLeft(){   
        long temp = this.myDeque[this.left++];   
        if(this.left==this.maxSize) this.left = 0;   
        this.nItems--;   
        return temp;   
    }   
    //remove from right   
    long removeRight(){   
        long temp = this.myDeque[this.right--];   
        if(this.left==-1) this.left = this.maxSize-1;   
        this.nItems--;   
        return temp;   
    }   
    //return true if deQue is empty   
    boolean isEmpty(){   
        return (this.nItems==0);   
    }   
    //return size of the deQue   
     int size(){   
        return this.nItems;   
    }   
}  ;


 

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wuchuanpingstone/article/details/6881091
个人分类: Data structure
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭