java中堆栈的基本操作_玩儿转队列和栈的基本操作及其应用:Java 版

队列的基本操作

队列入队出队实现

队列是种先进先出的数据结构。

队列的基本操作主要是入队和出队。

数据从队尾进入队列,从队首出队列。

861814be57a2f93b5f7b1746d78d99c1.png

下面来写一个简单的队列:

public class MyQueue {

private List data;

private int pointer;

public MyQueue() {

data = new ArrayList<>();

pointer = 0;

}

public boolean isEmpty() {

return pointer >= data.size();

}

/**

* Get the front item from the queue.

*/

public int Front() {

return data.get(pointer);

}

/**

* Insert an element into the queue. Return true if the operation is successful.

*/

public boolean enQueue(int x) {

data.add(x);

return true;

}

/**

* Delete an element from the queue. Return true if the operation is successful.

*/

public boolean deQueue() {

if (isEmpty()) {

return false;

}

pointer++;

return true;

}

}

其中,pointer代表头队首第一个位置的数据。

a5fcedbf71bd63f797077e30936d8fe8.png

当头部有数据出队时,对应的pointer指针会往后移动一位。

f647268fc8a1cd92dced982351e082aa.png

如果有数据入队,会直接加在队尾:

ce30debf0732dc3360569b8658734b25.png

我们会发现,随着队首数据的数据的出队,队首指针之前的空间都会被浪费掉,而且随着使用次数变多,空间浪费会越来越高。

为了重复使用这部分空间,避免浪费,我们可以设计一个队首与队尾相连接的队列,重复利用空间。

循环队列设计

ca7830daa962959bd8ba0fe2f81f1395.png

如上图所示,数据1 ~ 10依次入队,队首head在1处,队尾tail在10处,如果将1出队列,head头指针将移动到2:

53a9035c2a1dfa4a9d0becdb67e3b42b.png

此时,有一个空位,我们还可以继续入队:

a9ac49fe591e0c51b28e81ea52bf8214.png

这样利用空间,刚好可以把队首浪费的空间利用上。

class MyCircularQueue {

private Integer[] data; //队列数据

private Integer size; //队列大小

private Integer head = -1; //头指针

private Integer tail = -1; //尾指针

/**

* 初始化队列

*/

public MyCircularQueue(int k) {

this.data = new Integer[k];

this.size = k;

}

/**

* 入队操作

*/

public boolean enQueue(int value) {

if (isFull()) {

return false;

}

if (head == -1) {

head++;

}

tail++;

data[tail % size] = value;

return true;

}

/**

* 出队操作

*/

public boolean deQueue() {

if (isEmpty()) {

return false;

}

if (head == tail % size) {

head = -1;

tail = -1;

} else {

head++;

}

return true;

}

/**

* 获取队首元素

*/

public int Front() {

if (isEmpty()) {

return -1;

}

return data[head];

}

/**

* 获取队尾元素

*/

public int Rear() {

if (isEmpty()) {

return -1;

}

return data[tail % size];

}

/**

* 判断队列是不是为空

*/

public boolean isEmpty() {

return tail == -1 && head == -1;

}

/**

* 检查队列是不是已经满了

*/

public boolean isFull() {

return (tail % size - head) == size - 1 || (head - tail % size) == 1;

}

}

广度优先搜索(BFS)及其实现

上面我们已经实现过基本的队列已经如果优化队列,下面我们来看一个队列在 BFS(广度优先搜索)算法中的应用。

首先我们来定义结点:

@Getter

@Setter

@EqualsAndHashCode

@NoArgsConstructor

class Node implements Serializable {

private static final long serialVersionUID = 3687337665231315466L;

String value;

Collection previews; //前置结点

Collection tails; //后置结点

public Node(String value, Collection previews, Collection tails) {

this.value = value;

}

public Node(String value) {

this.value = value;

}

}

之后,我们先用队列来实现简单的BFS:

int BFS(Node root, Node target) {

Queue queue = new LinkedBlockingQueue(); // store all nodes which are waiting to be processed

int step = 0; // number of steps neeeded from root to current node

queue.add(root);

while (!queue.isEmpty()) {

step++;

int size = queue.size();

for (int i = 0; i < size; i++) {

Node cur = queue.poll();

if (cur.equals(target)) {

return step;

}

if (cur.tails != null) {

for (Node node : cur.tails) {

queue.add(node);

}

}

}

}

return -1;

}

另外,考虑到图结构中,像上面这样访问,可能会存在访问重复结点的情况,所以,我们记录下访问过的结点,访问过就直接跳过。

下面是改进算法:

/**

* 避免一个节点访问两次,单独检查访问过的结点

*

* @param root

* @param target

* @return

*/

int BFS(Node root, Node target) {

Queue queue = new LinkedBlockingQueue(); // store all nodes which are waiting to be processed

Set userd = new HashSet<>(); // node that be used

int step = 0; // number of steps neeeded from root to current node

queue.add(root);

userd.add(root);

while (!queue.isEmpty()) {

step++;

int size = queue.size();

for (int i = 0; i < size; i++) {

Node cur = queue.poll();

if (cur.equals(target)) {

return step;

}

if (cur.tails != null) {

for (Node node : cur.tails) {

if (!userd.contains(node)) {

queue.add(node);

userd.add(node);

}

}

}

}

}

return -1;

}

Stack

与队列相反,栈是种先入后出的结构。

下面我们来看下Java里面的栈基本入栈和出栈是如何实现的:

首先是入栈操作:

/**

* Pushes an item onto the top of this stack. This has exactly

* the same effect as:

*

* addElement(item)

*

* @param item the item to be pushed onto this stack.

* @return the item argument.

* @see java.util.Vector#addElement

*/

public E push(E item) {

addElement(item);

return item;

}

/**

* Adds the specified component to the end of this vector,

* increasing its size by one. The capacity of this vector is

* increased if its size becomes greater than its capacity.

*

*

This method is identical in functionality to the

* {@link #add(Object) add(E)}

* method (which is part of the {@link List} interface).

*

* @param obj the component to be added

*/

public synchronized void addElement(E obj) {

modCount++;

ensureCapacityHelper(elementCount + 1);

elementData[elementCount++] = obj;

}

入栈 操作即将数据插入数组尾部。ps:入栈之前会去进行容量检查,如果不够,会进行内部数组的扩容操作,会重新产生大容量数组,并将原来老数组拷贝到新数组,完成扩容。过程跟 ArrayList 的类似。

下面来看下出栈:

/**

* Removes the object at the top of this stack and returns that

* object as the value of this function.

*

* @return The object at the top of this stack (the last item

* of the Vector object).

* @throws EmptyStackException if this stack is empty.

*/

public synchronized E pop() {

E obj;

int len = size();

obj = peek();

removeElementAt(len - 1);

return obj;

}

public synchronized void removeElementAt(int index) {

modCount++;

if (index >= elementCount) {

throw new ArrayIndexOutOfBoundsException(index + " >= " +

elementCount);

}

else if (index < 0) {

throw new ArrayIndexOutOfBoundsException(index);

}

int j = elementCount - index - 1;

if (j > 0) {

System.arraycopy(elementData, index + 1, elementData, index, j);

}

elementCount--;

elementData[elementCount] = null; /* to let gc do its work */

}

这里,直接将数组尾部的数移除。

深度优先搜索(DFS)

首先来看DFS的简单递归实现:

/*

* 基于递归实现 DFS

*/

boolean DFS(Node cur, Node target, Set visited) {

if (cur == target) {

return true;

}

if (cur.tails == null || cur.tails.size() < 1) {

return false;

}

for (Node n : cur.tails) {

visited.add(n);

if (DFS(n, target, visited)) {

return true;

}

}

return false;

}

基于递归的实现,系统会自动帮我们生成堆栈调用,但是如果递归的深度过高的话,终将造成堆栈溢出。这时候,我们就需要自己用Stack实现这一过程:

/**

* 基于 stack

*

* @param cur

* @param target

* @return

*/

boolean DFS(Node cur, Node target) {

Set visited = new HashSet<>();

Stack stack = new Stack();

stack.push(cur);

while (!stack.isEmpty()) {

Node temp = stack.pop();

if (temp == target) {

return true;

}

for (Node n : temp.tails) {

if (!visited.contains(n)) {

visited.add(n);

stack.push(n);

}

}

}

return false;

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/528240.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java规定日期格式输出_Java格式化输出日期百分比时间等

Java格式化输出Java的格式化输出等同于String.Format&#xff0c;与C有很大的相似&#xff0c;比如System.out.printf(“%8.2f”, x);在printf中&#xff0c;可以使用多个参数&#xff0c;例如&#xff1a;System.out.printf(“Hello, %s. Next year, you’ll be %d”, name, a…

java界面编辑教程_java程序设计基础教程第六章图形用户界面编辑.docx

java程序设计基础教程第六章图形用户界面编辑.docx还剩27页未读&#xff0c;继续阅读下载文档到电脑&#xff0c;马上远离加班熬夜&#xff01;亲&#xff0c;很抱歉&#xff0c;此页已超出免费预览范围啦&#xff01;如果喜欢就下载吧&#xff0c;价低环保&#xff01;内容要点…

c盘java文件误删_java获取C盘下的隐藏目录文件名称

题记—— 执剑天涯&#xff0c;从你的点滴积累开始&#xff0c;所及之处&#xff0c;必精益求精&#xff0c;即是折腾每一天。网易云课堂在Java中&#xff0c; File类用来将文件或者文件夹封装成对象&#xff0c;方便对文件与文件夹的属性信息进行操作。File对象可以作为参数传…

对java这门课程的认识_关于java课程的总结

前言本次博客主要内容为此次三次作业的总结&#xff0c;整门java课程学习的总结&#xff0c;以及在此次java课程中的收获&#xff0c;和对课程的意见。作业过程总结第一次作业主要考察的是对程序的可扩展性&#xff0c;实现开闭原则非常重要&#xff0c;因为程序随着时间&#…

linux php和java环境变量配置_Linux下配置Java环境变量

一般来说&#xff0c;我们都会把Java安装到 /usr/local 目录 或者 /opt 目录下。这里假设java安装包已解压在了 /opt下&#xff0c;具体目录为&#xff1a;/opt/java8/java1.8.0_45目录(注意&#xff1a;如果是生产环境中&#xff0c;一定要root用户来安装配置)。下面我们来配置…

java多线程多态_Java学习之多线程

多线程&#xff1a;(一)进程与线程进程特点并发与并行的区别&#xff1a;多线程编程的好处&#xff1a;(二)多线程的建立1&#xff0c;通过继承Thread类&#xff0c;代码如下&#xff1a;class MyThread extendsThread {private static int K 10;//类共享变量private int M10;…

java 执行存储过程报语法错误_为什么我在批处理从Java上执行PostgreSQL上的存储过程时收到错误通知“结果不合理”?...

我在数据库中有这个过程&#xff1a;CREATE OR REPLACE FUNCTION replacePageRelevance(id INT, value REAL) RETURNS VOID AS $$BEGININSERT INTO pageRelevance VALUES (id,value);EXCEPTION WHEN unique_violation THENUPDATE pageRelevance SET relevance value WHERE pag…

python staticmethod有什么意义_关于静态方法:python中的@staticmethod有什么意义?

为了更好地理解静态方法在Python中的工作方式&#xff0c;我开发了这个简短的测试/示例代码。class TestClass:def __init__(self, size):self.size sizedef instance(self):print("regular instance method - with self")staticmethoddef static():print("sta…

java fx border_JavaFx UI控件与代码间的绑定方法

JavaFx初探一&#xff0c;UI控件的使用&#xff0c;具体内容如下方式一&#xff1a;使用纯代码直接new view控件&#xff0c;这样就不涉及到与fxml文件之间的交互了方式二&#xff1a;使用fxml编写界面文件&#xff0c;用可视化工具scene builder 来构建交互界面。分两种方式绑…

java mysql ssl警告_连接到MySQL数据库时有关SSL连接的警告

用于初始化与MySQL服务器的连接的默认值在最近已更改&#xff0c;并且(通过快速查看堆栈溢出时最流行的问题和答案)新值引起了很多混乱。更糟糕的是&#xff0c;标准建议似乎是完全禁用SSL&#xff0c;这在制造过程中有点麻烦。现在&#xff0c;如果您的连接确实没有暴露给网络…

如何将php改成mp4,PHP 将视频转成 MP4 并获取视频预览图(用到ffmpeg)

搜索热词下面是编程之家 jb51.cc 通过网络收集整理的代码片段。编程之家小编现在分享给大家&#xff0c;也给大家做个参考。flv_convert_get_thumb(input.avi,output.jpg,output.ogm);// code provided and updated by steve of PHPsnaps ! thanks// accepts:// 1: the input v…

php无嵌套遍历多维数组,不递归怎么遍历多维数组(维数不定)

不递归如何遍历多维数组(维数不定)现有数组$tree array (array (ID > 1,PARENT > 0,NAME > 祖父,CHILD > array (array (ID > 3,PARENT > 1,NAME > 叔伯),array (ID > 4,PARENT > 1,NAME > 父亲,CHILD > array (array (ID > 5,PARENT >…

c 如何操作php,thinkphp的c方法使用示例

1.C方法的作用a. 加载设置用户的配置&#xff0c;保存在一个C函数内的静态变量$_config 中b. 读取用户的配置 (从$_congig 中读取)2. 需求分析:1.设置变量1.二维数组C(array(DB_PASSWORD>root,DB_USERNAME>root),DB);C(DB.USER_NAME,XIAOCHEN);2.一维数组C(USER_NAME,小陈…

php seaslog安装,浅谈win10下安装php seaslog扩展的方法

本篇文章给大家分享一下win10 php安装seaslog扩展的方法。有一定的参考价值&#xff0c;有需要的朋友可以参考一下&#xff0c;希望对大家有所帮助。【推荐学习&#xff1a;《PHP视频教程》】一、检查系统环境情况使用phpinfo()检查系统环境情况&#xff0c;找到需要下载的配置…

php 图片 处理,php图片处理类

本篇文章主要介绍php图片处理类&#xff0c;感兴趣的朋友参考下&#xff0c;希望对大家有所帮助。示例代码如下&#xff1a;<?php /*已知问题&#xff1a;1.在图片缩放功能中&#xff0c;使用imagecreatetruecolor函数创建画布&#xff0c;并使用透明处理算法&#xff0c;但…

php里push的用法,php array_push函数怎么用?

php array_push函数用于向数组尾部插入一个或多个元素&#xff0c;其语法是array_push(array,value1,value2...)&#xff0c;参数array必需&#xff0c;指规定一个数组&#xff1b;value1必需&#xff0c;指规定要添加的值。php array_push函数怎么用&#xff1f;定义和用法arr…

php 内容编码错误,PHP输出缓冲,ob_gzhandler引起的内容编码错误?

应用程序的输出应该只包含一个输出编码.如果您有多个编码方式不同的块,那么浏览器将得到一个无法使用的结果.因此编码错误.Kohana本身已经使用了输出缓冲区.如果你想将它与你的ob_gzhandler输出缓冲区结合起来,你需要在kohana初始化它之前启动你的缓冲区.那是因为输出缓冲区是可…

php铺满,重复铺满水印 - Jun. - OSCHINA - 中文开源技术交流社区

/*$tmp"tmp/a.jpg";$obj new WaterMask($tmp);$obj->waterImg sy_logo.png;$obj->transparent 50;$obj->output();*/class WaterMask{public $pos 0; //水印位置public $transparent 45; //水印透明度public $waterImg ; //水印图片private $srcImg …

java if (name!=null name!=),java中的NullPointerException异常

java中的NullPointerException异常关注:176 答案:3 mip版解决时间 2021-01-27 20:59提问者侢遇噹姩揂2021-01-27 02:10Login.jsp提供登录表单。到LoginCheck.jsp发生空指针异常错误。LoginCheck.jsp:String userName request.getParameter("userName");String pas…

fullcalendar php,日历插件fullcalendar+php的使用教程 — 读取json数据

根据FullCalendar日历插件说明文档中的介绍&#xff0c;日历主体事件数据的来源有三&#xff0c;一是直接以javascript数组的形式显示日历事件&#xff0c;二是获取JSON数据形式显示日历事件&#xff0c;三是函数回调的形式显示日历数据&#xff0c;三种调用数据的方式各有所用…