Java 模拟队列(一般队列、双端队列、优先级队列)[通俗易懂]

Java 模拟队列(一般队列、双端队列、优先级队列)

大家好,又见面了,我是全栈君。

队列:

先进先出,处理类似排队的问题,先排的。先处理,后排的等前面的处理完了,再处理

对于插入和移除操作的时间复杂度都为O(1)。从后面插入,从前面移除

双端队列:

即在队列两端都能够insert和remove:insertLeft、insertRight。removeLeft、removeRight

含有栈和队列的功能,如去掉insertLeft、removeLeft,那就跟栈一样了。如去掉insertLeft、removeRight。那就跟队列一样了

一般使用频率较低,时间复杂度 O(1)

优先级队列:

内部维护一个按优先级排序的序列。插入时须要比較查找插入的位置,时间复杂度O(N), 删除O(1)

/* * 队列	先进先出。一个指针指示插入的位置,一个指针指示取出数据项的位置 */public class QueueQ<T> {	private int max;	private T[] ary;	private int front; //队头指针  指示取出数据项的位置	private int rear;  //队尾指针  指示插入的位置	private int nItems; //实际数据项个数		public QueueQ(int size) {		this.max = size;		ary = (T[]) new Object[max];		front = 0;		rear = -1;		nItems = 0;	}	//插入队尾	public void insert(T t) {		if (rear == max - 1) {//已到实际队尾,从头開始			rear = -1;		}		ary[++rear] = t;		nItems++;	}	//移除队头	public T remove() {		T temp = ary[front++];		if (front == max) {//列队到尾了,从头開始			front = 0;		}		nItems--;		return temp;	}	//查看队头	public T peek() {		return ary[front];	}		public boolean isEmpty() {		return nItems == 0;	}		public boolean isFull() {		return nItems == max;	}		public int size() {		return nItems;	}		public static void main(String[] args) {		QueueQ<Integer> queue = new QueueQ<Integer>(3);		for (int i = 0; i < 5; i++) {			queue.insert(i);			System.out.println("size:" + queue.size());		}		for (int i = 0; i < 5; i++) {			Integer peek = queue.peek();			System.out.println("peek:" + peek);			System.out.println("size:" + queue.size());		}		for (int i = 0; i < 5; i++) {			Integer remove = queue.remove();			System.out.println("remove:" + remove);			System.out.println("size:" + queue.size());		}				System.out.println("----");				for (int i = 5; i > 0; i--) {			queue.insert(i);			System.out.println("size:" + queue.size());		}		for (int i = 5; i > 0; i--) {			Integer peek = queue.peek();			System.out.println("peek:" + peek);			System.out.println("size:" + queue.size());		}		for (int i = 5; i > 0; i--) {			Integer remove = queue.remove();			System.out.println("remove:" + remove);			System.out.println("size:" + queue.size());		}	}	}

/*
 * 双端队列<span style="white-space:pre">	</span>两端插入、删除
 */
public class QueueQT<T> {
	private LinkedList<T> list;

	public QueueQT() {
		list = new LinkedList<T>();
	}

	// 插入队头
	public void insertLeft(T t) {
		list.addFirst(t);
	}

	// 插入队尾
	public void insertRight(T t) {
		list.addLast(t);
	}

	// 移除队头
	public T removeLeft() {
		return list.removeFirst();
	}

	// 移除队尾
	public T removeRight() {
		return list.removeLast();
	}

	// 查看队头
	public T peekLeft() {
		return list.getFirst();
	}

	// 查看队尾
	public T peekRight() {
		return list.getLast();
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public int size() {
		return list.size();
	}

}
/*
 * 优先级队列	队列中按优先级排序。是一个有序的队列
 */
public class QueueQP {
	private int max;
	private int[] ary;
	private int nItems; //实际数据项个数
	
	public QueueQP(int size) {
		this.max = size;
		ary =  new int[max];
		nItems = 0;
	}
	//插入队尾
	public void insert(int t) {
		int j;
		if (nItems == 0) {
			ary[nItems++] = t;
		} else {
			for (j = nItems - 1; j >= 0; j--) {
				if (t > ary[j]) {
					ary[j + 1] = ary[j]; //前一个赋给后一个  小的在后		相当于用了插入排序。给定序列本来就是有序的。所以效率O(N)
				} else {
					break;
				}
			}
			ary[j + 1] = t;
			nItems++;
		}
		System.out.println(Arrays.toString(ary));
	}
	//移除队头
	public int remove() {
		return ary[--nItems]; //移除优先级小的
	}
	//查看队尾 优先级最低的
	public int peekMin() {
		return ary[nItems - 1];
	}
	
	public boolean isEmpty() {
		return nItems == 0;
	}
	
	public boolean isFull() {
		return nItems == max;
	}
	
	public int size() {
		return nItems;
	}
	
	public static void main(String[] args) {
		QueueQP queue = new QueueQP(3);
		queue.insert(1);
		queue.insert(2);
		queue.insert(3);
		int remove = queue.remove();
		System.out.println("remove:" + remove);
		
	}
	
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116477.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • lamda中stream的forEach与for循环对比

    lamda中stream的forEach与for循环对比对比方式将一个字符串数组进行输出的方式:代码publicstaticvoidmain(String[]args)throwsIOException{intn=500000;String[]strings=newString[n];LongstreamStart=System.currentTimeMillis();Arrays.stream(strings).forEach(System

    2025年6月5日
    0
  • lua中的weak table及内存回收collectgarbage

    弱表(weaktable)是一个很有意思的东西,像C++/Java等语言是没有的。弱表的定义是:Aweaktableisatablewhoseelementsareweakreferences,元素为弱引用的表就叫弱表。有弱引用那么也就有强引用,有引用那么也就有非引用。我们先要厘这些基本概念:变量、值、类型、对象。(1)变量与值:Lua是一个dynamicallyty

    2022年4月7日
    92
  • 前端开发项目经验_项目管理体系包括哪些

    前端开发项目经验_项目管理体系包括哪些前端开发传统的web开发管理端、H5/小程序、可视化、游戏等Node.js开发服务端接入层、构建工具、云服务等终端开发reactnative、flutter、electron等项目开发过程中涉及的系统涉及、方案调研、技术选型、性能优化、效能提升这些都是想通的这是怎样的一个项目?他遇到什么问题、存在着怎样的瓶颈?又需要怎么去解决?前端面试相关知识点前端常见的框架和工具库重要的是要知道各个框架的区别、掌握框架设计和实现原理Node.js和服务.

    2022年10月21日
    1
  • django常用命令_我的世界好玩指令大全

    django常用命令_我的世界好玩指令大全前言我们掌握了如何在命令提示符或PyCharm下创建Django项目和项目应用,无论是创建项目还是创建项目应用,都需要输入相关的指令才能得以实现,这些都是Django内置的操作指令。在PyChar

    2022年7月29日
    4
  • Android Sdk版本、Support包版本及常用框架最新版本汇总

    Android Sdk版本、Support包版本及常用框架最新版本汇总1.SDKVerion数据来源于维基百科,和一篇博客Api版本号代号发布时间主要更新内容11.0无2008-09-23Web浏览器显示,短信,媒体播放器,相机,Wifi及蓝牙支持21.1PetitFour(花式小蛋糕)2009-02-09邮件中保存附件31….

    2022年5月29日
    49
  • 数据库建立索引常用的规则

    数据库建立索引常用的规则数据库建立索引常用的规则如下:1、表的主键、外键必须有索引; 2、数据量…

    2022年7月24日
    11

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号