美文网首页收集
队列的图文解析和对应语言的实现

队列的图文解析和对应语言的实现

作者: Bobby0322 | 来源:发表于2017-10-13 19:59 被阅读22次

队列的图文解析和对应语言的实现

队列的介绍

Queue是一种先进先出的数据结构,和Stack一样,他也有链表和数组两种实现,理解了Stack的实现后,Queue的实现就比较简单了。

Queue
名称 描述
Stack<T>() 创建一个空的队列
void Enqueue(T s) 往队列中添加一个新的元素
T Dequeue() 移除队列中最早添加的元素
boolean IsEmpty() 队列是否为空
int Size() 队列中元素的个数

队列的操作

Queue示意

队列主要有两种操作:从队尾添加元素,从对头删除元素。添加元素称为入队,删除元素称为出队。如图:

Queue Operations

队列的创建

队列的创建有两种形式:基于数组结构实现(顺序队列)、基于链表结构实现(链式队列)

代码实现:

入栈时,和在普通的链表中添加结点的操作是一样的;出队时,出的永远都是head结点。

package com.pingkeke.rdf.sample;

/**
 * 通过链表的形式来创建队列,这样的话,队列在扩充时会比较方便。队列在出队时,从头结点head开始.
 */
public class Queue01 {

    class Node {
        int data;
        Node next;

        public Node(int data) {
            this.data = data;
        }
    }

    public Node head;
    public Node curent;

    /**
     * 链表中添加结点
     * @param data
     */
    public void add(int data) {
        if (head == null) {
            head = new Node(data);
            curent = head;
        } else {
            curent.next = new Node(data);
            curent = curent.next;
        }
    }

    /**
     * 出队操作
     * @return
     * @throws Exception
     */
    public int pop() throws Exception {
        if (head == null) {
            throw new Exception("队列为空");
        }

        Node node = head;  // node结点就是我们要出队的结点
        head = head.next;  // 出队之后,head指针向下移

        return node.data;
    }

    public static void main(String[] args) throws Exception {

        Queue01 queue = new Queue01();
        // 入队操作
        for (int i = 0; i < 5; i++) {
            queue.add(i);
        }

        //出队操作
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());

        /**
         * 运行结果:
         * 0
         1
         2
         */

    }

}

使用数组实现队列

package com.pingkeke.rdf.sample;

/**
 * 使用数组实现队列.
 */
public class Queue03 {

    public static void main(String[] args) throws Exception {

        ArrayQueue queue = new ArrayQueue(3);

        System.out.println(queue.isEmpty());

        for (int i = 0; i < 3; i++) {
            queue.insert(i);
        }
        System.out.println(queue.isFull());

        while (!queue.isEmpty()) {
            System.out.println(queue.remove());
        }

        /**
         * 运行结果:
         * false
         true
         0
         1
         2
         *
         */
    }
}


class ArrayQueue {

    private int[] arrInt;   // 内置数组
    private int front;      // 头指针
    private int rear;       // 尾指针

    public ArrayQueue(int size) {
        this.arrInt = new int[size];
        front = 0;
        rear = -1;
    }

    /**
     * 判断队列是否为空
     * @return
     */
    public boolean isEmpty() {
        return front == arrInt.length;
    }


    /**
     * 判断队列是否已满
     * @return
     */
    public boolean isFull() {
        return arrInt.length - 1 == rear;
    }


    /**
     * 向队列的队尾插入一个元素
     * @param item
     */
    public void insert(int item) {
        if (isFull()) {
            throw new RuntimeException("队列已满");
        }
        arrInt[++rear] = item;
    }


    /**
     * 获得队头元素
     * @return
     */
    public int peekFront() {
        return arrInt[front];
    }


    /**
     * 获得队尾元素
     * @return
     */
    public int peekRear() {
        return arrInt[rear];
    }

    /**
     * 从队列的对头移除一个元素
     * @return
     */
    public int remove() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arrInt[front++];
    }
}

两个栈实现一个队列

思路: 栈1用于存储元素,栈2用于弹出元素,负负得正。
说的通俗一点,现在把数据1、2、3分别入栈一,然后从栈一中出来(3、2、1),放到栈二中,那么,从栈二中出来的数据(1、2、3)就符合队列的规律了,即负负得正。

完整版代码实现:

package com.pingkeke.rdf.sample;

import java.util.Stack;

/**
 * 两个栈实现一个队列.
 */
public class Queue02 {

    private Stack<Integer> stack1 = new Stack<>();  // 执行入队操作的栈
    private Stack<Integer> stack2 = new Stack<>();  // 执行出队操作的栈

    /**
     * 给队列增加一个入队的操作
     * @param data
     */
    public void push(int data) {
        stack1.push(data);
    }


    /**
     * 给队列增加一个出队的操作
     * @return
     * @throws Exception
     */
    public int pop() throws Exception {
        /**
         * stack1中的数据放到stack2之前,先要保证stack2里面是空的
         * (要么一开始就是空的,要么是stack2中的数据出完了),
         * 不然出队的顺序会乱的,这一点很容易忘
         */

        if (stack2.empty()) {
            while (!stack1.empty()) {
                stack2.push(stack1.pop()); // 把stack1中的数据出栈,放到stack2中
            }
        }


        /**
         * stack2为空时,有两种可能:
         * 1、一开始,两个栈的数据都是空的;
         * 2、stack2中的数据出完了
         */
        if (stack2.empty()) {
            throw new Exception("队列为空");
        }

        return stack2.pop();
    }


    public static void main(String[] args) throws Exception {

        Queue02 queue = new Queue02();
        queue.push(1);
        queue.push(2);
        queue.push(3);

        System.out.println(queue.pop());

        queue.push(4);

        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());

        /**
         * 运行结果:
         * 1
         2
         3
         4
         */

    }


}

相关文章

  • 队列的图文解析和对应语言的实现

    队列的图文解析和对应语言的实现 队列的介绍 Queue是一种先进先出的数据结构,和Stack一样,他也有链表和数组...

  • 栈的图文解析和对应语言的实现

    栈的介绍 栈(stack),是一种线性存储结构,它有以下几个特点: 栈中数据是按照"后进先出(LIFO, Last...

  • 二叉堆

    https://zh.visualgo.net/heap 二叉堆例程二叉堆(一)之 图文解析 和 C语言的实现 -...

  • 连个栈实现一个队列

    用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。 语言java 用两个栈实现一...

  • 用两个栈实现队列

    题目描述用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。 解析栈是“先进后出”...

  • spring boot 集成rabbitmq 实现延迟队列

    rabbitmq 实现延迟队列 什么是延迟队列 延迟队列存储的对象肯定是对应的延时消息,所谓”延时消息”是指当消息...

  • 数据结构——栈和队列

    用数组实现栈和队列 用栈实现队列 用队列实现栈 栈和队列的经典算法题最小间距栈宠物收养所 数组实现栈和队列 用数组...

  • Algorithm小白入门 -- 队列和栈

    队列和栈队列实现栈、栈实现队列单调栈单调队列运用栈去重 1. 队列实现栈、栈实现队列 队列是一种先进先出的数据结构...

  • 17爬虫项目总结

    1.项目架构 项目架构 2.详细技术点 1.解析,(依赖注入)2,使用queue队列实现循环抓取3.实现优先级队列...

  • GCD与NSOperation的区别

    GCD和NSOperation 用于实现多线程:GCD会自动将队列中的任务取出,放到对应的线程中执行,任务的取出遵...

网友评论

    本文标题:队列的图文解析和对应语言的实现

    本文链接:https://www.haomeiwen.com/subject/xlltuxtx.html