源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

java数据结构与算法之双向循环队列的数组实现方法

  • 时间:2020-12-30 08:55 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:java数据结构与算法之双向循环队列的数组实现方法
本文实例讲述了java数据结构与算法之双向循环队列的数组实现方法。分享给大家供大家参考,具体如下: 需要说明的是此算法我并没有测试过,这里给出的相当于伪代码的算法思想,所以只能用来作为参考!
package source;
public class Deque {
 private int maxSize;
 private int left;
 private int right;
 private int nItems;
 private long[] myDeque;
 //constructor
 public 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
 public 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
 public void insertRight(long n){
 if(this.right==this.maxSize-1) this.right = -1;
 this.myDeque[++this.right] = n;
 this.nItems++;
 }
 //remove from left
 public long removeLeft(){
 long temp = this.myDeque[this.left++];
 if(this.left==this.maxSize) this.left = 0;
 this.nItems--;
 return temp;
 }
 //remove from right
 public 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
 public boolean isEmpty(){
 return (this.nItems==0);
 }
 //return size of the deQue
 public int size(){
 return this.nItems;
 }
}

[b]PS:双向循环队列的用处很大,可以做为普通队列,也可以用来做栈来用![/b] 更多关于java算法相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/632.htm]Java数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/830.htm]Java操作DOM节点技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/687.htm]Java文件与目录操作技巧汇总[/url]》和《[url=http://www.1sucai.cn/Special/682.htm]Java缓存操作技巧汇总[/url]》 希望本文所述对大家java程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部