• 队列


    FILO,后进先出;单向队列尾插头删;rear为尾端、front为头端。

    用数组模拟,当插入时,rear索引+1;当删除时,front索引+1;

    顺序队列代码:

    package com.zxc.queuelearning;
    import org.junit.Before;
    import org.junit.Test;
    
    /**
     * Created by Administrator on 2018/2/17 0017.
     */
    public class SequenceQueue {
        private int DEFAULT_SIZE=10;//默认长度
        private int capacity;//保存数组的长度
        private String[] elementData;//定义一个数组用于保存顺序队列的元素
        private int front=0;//
        private int rear=0;//
    
        @Before
        public void init(){
            elementData=new String[DEFAULT_SIZE];
            capacity=elementData.length-1;
        }
    
        public void add(String data) {//进队
            if(rear>capacity){
                try {
                    throw new Exception();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            elementData[rear++]=data;
        }
    
        public String remove(){//出队
            String s=elementData[front];
            elementData[front++]=null;
            return s;
        }
    
        @Test
        public void test(){
            add("A");
            add("B");
            add("C");
            System.out.println(remove());
        }
    }

    循环队列(可以覆盖)

    package com.zxc.queuelearning;
    
    import org.junit.Before;
    import org.junit.Test;
    
    /**
     * Created by Administrator on 2018/2/17 0017.
     * 循环队列
     */
    public class LoopQueue {
    
        private int capacity;//保存数组的长度
        private String[] elementData;//定义一个数组用于保存顺序队列的元素
        private int front=0;//
        private int rear=0;//
    @Before
        public void init(){
            elementData=new String[4];
            capacity=elementData.length;
        }
    
        public void add(String data){
            elementData[rear++]=data;
            rear=rear==capacity?0:rear;
        }
    
        public String remove(){
            String s=elementData[front];
            elementData[front++]=null;
            front=front==capacity?0:front;
            return s;
        }
        @Test
        public void test(){
            add("A");
            add("B");
            add("C");
            add("D");
            add("E");
            System.out.println(remove());
            System.out.println(remove());
            System.out.println(remove());
            System.out.println(remove());
        }
    }

    单向链式队列:

    package com.zxc.queuelearning;
    
    import org.junit.Test;
    
    /**
     * Created by Administrator on 2018/2/17 0017.
     * 链式单向队列
     */
    public class ChainQueue {
    
        private class Node{
            private String data;
            private Node next;
    
            public Node(String data,Node next){
                this.data=data;
                this.next=next;
            }
        }
        private Node front;//保存链队列的头结点
        private Node rear;//保存链队列的尾节点
        private int size;//保存该链队列中已经包含的节点数
    
    
    
        public void add(String data){
            if(front==null){
                front=new Node(data,null);
                rear=front;
            }else{
                Node newNoed=new Node(data,null);
                rear.next=newNoed;
                rear=newNoed;
            }
            size++;
        }
    
        public String remove(){
            Node oldNode=front;
            front=front.next;
            oldNode.next=null;
            size--;
            return oldNode.data;
        }
        @Test
        public void test(){
            add("A");
            add("B");
            add("C");
            add("D");
            System.out.println(remove());
        }
    }

    双向队列:

    JDK带的ArrayDeque:代表顺序存储结构的双向队列。

        LinkedList:代表链式存储结构的双向队列。

        LinkedBlockingList:一个线程安全的链式结构的双向队列。

    package com.zxc.queuelearning;
    
    import org.junit.Test;
    
    /**
     * Created by Administrator on 2018/2/17 0017.
     * 链式双向队列
     */
    public class Deque {
        public class Node{
            String data;
            Node next;
            Node previous;
            Node(String data){
                this.data=data;
            }
        }
    
        private Node front;
        private Node rear;
        @Test
        public void test(){
            insertFront("A");
            insertFront("B");
            insertLast("C");
            insertFront("D");
            System.out.println(deleteRear());
            System.out.println(deleteFront());
    
        }
    
        public void insertFront(String data){
            Node newNode=new Node(data);
            if(front==null){
                rear=newNode;
            }else{
                front.previous=newNode;
            }
            newNode.next=front;
            front=newNode;
        }
    
        public void insertLast(String data){
            Node newNode=new Node(data);
            if(front==null){
                front=newNode;
            }else{
                rear.next=newNode;
            }
            newNode.previous=rear;
            rear=newNode;
        }
    
        public String deleteFront(){
            Node node=front;
            front=node.next;
            if(front!=null){//至少原来有两个节点
                front.previous=null;
            }else{
                rear=null;
            }
            return node.data;
        }
    
        public String deleteRear(){
            Node node=rear;
            rear=node.previous;
            if(rear!=null){
                rear.next=null;
            }else{
                front=null;
            }
            return node.data;
        }
    }

    队列高级使用技巧:由两个栈组成一个队列

      题目:编写一个类,用两个栈实现一个队列,支持队列的基本操作:

      

    package com.zxc.queuelearning;
    
    import org.junit.Test;
    
    import java.util.Stack;
    
    /**
     * Created by Administrator on 2018/2/17 0017.
     *用两个栈实现一个队列
     **/
    public class StacktoQueue {
        private Stack s1=new Stack();
        private Stack s2=new Stack();
    
        public void add(String data){
            s1.push(data);
        }
        public void push(){
            while(!s1.empty()){
                s2.push(s1.pop());
            }
        }
        public void remove(){
            while(!s2.empty()){
                System.out.println(s2.pop());
            }
        }
    @Test
        public void test(){
            add("A");
            add("B");
            add("C");
            push();
            remove();
        }
    
    }
  • 相关阅读:
    ORACLE 11g RACRAC DG Duplicate 搭建(生产操作文档)
    oracle跨数据库
    Linux查看网络流量
    数据库性能监控脚本
    三节点DG环境主库单机转RAC(DG主备切换)
    Oracle dblink介绍
    script 加载失败触发onerror事件
    流程图
    The State of JavaScript 2019
    throw , console.error, new Error
  • 原文地址:https://www.cnblogs.com/television/p/8451858.html
Copyright © 2020-2023  润新知