• 队列的链式存储结构


    直接上代码吧

    package com.sbtufss.test;
    /**
     * 队列是从队尾插入,队头出去,当为空队列时,队尾的next指向对头,队头和队尾的data都不存储数据
     */
    public class LinkQueue<T> {
        Node<T> front;//队头
        Node<T> rear;//队尾
        public LinkQueue(){
            super();
            initQueue();
        }
        //当为空队列是,头节点和尾节点指向同一个节点
        private void initQueue(){
            front=new Node<T>();
            rear=new Node<T>();
            rear.setNext(front);
            front.setPre(rear);
        }
        
        /**
         * 插入数据,数据是从队尾插入
         */
        public void add(T data){
            Node<T> node=new Node<T>();
            node.setData(data);
            node.setNext(rear.next);//将该节点的next指向队尾的next
            node.setPre(rear);
            rear.getNext().setPre(node);
            rear.setNext(node);//然后再将队尾的next指向该节点,从而建立了关系,插入了链表
        }
        
        /**
         * 获取数据,获取数据是从对头出队列的
         */
        public T poll(){
            if(rear.getNext()==front){
                return null;
            }
            Node<T> node=front.getPre();
            node.pre.setNext(front);//将对头的上一个指针的上一个指针的next设置为队头,然后该指针出队列
            front.setPre(node.pre);
            return node.getData();
        }
        
        /**
         * 清空队列
         */
        public void clear(){
            while(rear.getNext()!=front){
                Node<T> node=rear.getNext();
                rear.setNext(node.getNext());
                node.getNext().setPre(rear);
                node=null;//清空数据,等待虚拟机回收
            }
        }
        
        /**
         * 打印队列里面的数据,从对头开始打
         */
        @Override
        public String toString() {
            Node<T> node=front.getPre();
            StringBuilder sb=new StringBuilder();
            sb.append("[");
            while(node!=rear){
                sb.append(node.getData()).append(",");
                node=node.getPre();
            }
            if(sb.length()!=1){
                sb.deleteCharAt(sb.length()-1);
            }
            sb.append("]");
            return sb.toString();
        }
        
        private class Node<M>{
            private M data;
            private Node<M> next;
            private Node<M> pre;
            public Node() {
                super();
            }
            public M getData() {
                return data;
            }
            public void setData(M data) {
                this.data = data;
            }
            public Node<M> getNext() {
                return next;
            }
            public void setNext(Node<M> next) {
                this.next = next;
            }
            public Node<M> getPre() {
                return pre;
            }
            public void setPre(Node<M> pre) {
                this.pre = pre;
            }
        }
    }

    测试

    package com.sbtufss.test;
    
    
    public class Test {
        
        public static void main(String[] args) {
            LinkQueue<String> queue=new LinkQueue<>();
            queue.add("a");
            queue.add("b");
            queue.add("c");
            queue.add("d");
            System.out.println("queue.toString():"+queue);
            String s=queue.poll();
            System.out.println("取出对头的值:"+s);
            System.out.println("queue.toString():"+queue);
            queue.clear();
            System.out.println("queue.toString():"+queue);
            queue.add("e");
            queue.add("f");
            System.out.println("queue.toString():"+queue);
        }
        
    }

    测试结果

  • 相关阅读:
    Android与WebView的插件管理机制
    在mac下搭建Apacheserver
    “懒”也要有境地---大部分程序猿都在的地方,再不来就out了。
    codeforces Looksery Cup 2015 H Degenerate Matrix
    HDU 1247 Hat’s Words(字典树变形)
    SICP 习题 (1.38)解题总结
    scikit-learn:4.2. Feature extraction(特征提取,不是特征选择)
    iOS_高效开发之道
    亚马逊2014在线面试第一题
    通过AO连接多个EO并进行使用
  • 原文地址:https://www.cnblogs.com/pig-brother/p/7452965.html
Copyright © 2020-2023  润新知