zoukankan      html  css  js  c++  java
  • 队列的链式存储结构

    直接上代码吧

    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);
        }
        
    }

    测试结果

  • 相关阅读:
    PopupWindow设置动画效果
    android判断是否含有某权限
    每日一更提醒
    利用Pattern和Mather来禁止特殊字符的输入
    Android毛玻璃处理代码(Blur)
    how to render html tag
    数组
    复杂度分析
    书写markdown的利器
    cannot insert multiple commands into a prepared statement问题原因及解决办法
  • 原文地址:https://www.cnblogs.com/pig-brother/p/7452965.html
Copyright © 2011-2022 走看看