zoukankan      html  css  js  c++  java
  • Java之使用链表实现队列

    import java.util.Iterator;
    import java.util.NoSuchElementException;

    /**
     * 使用链表来实现队列
     *  1.考虑结点的结构,包括当前结点的元素和模拟的指针指向下一个元素
     *  2.结点的结构使用内部类来进行设计
     *  3.队列的结构:队列的长度,队列的首节点,队列的尾结点
     *  4.考虑队列需要的操作:
     *      1.使用构造函数进行初始化
     *      2.判断队列是否为空
     *      3.返回队列的长度
     *      4.向队列尾部中添加元素
     *      5.将队列首部元素删除
     *      6.格式化输出
     *      7.迭代器遍历
     * @author WZLOVE
     * @create 2018-07-14 18:03
     */
    public class LinkedQueue<Item> implements Iterable<Item> {
    
        /**
         * 定义队列基本元素
         */
    
        /**
         *  队列的长度
         */
        private int n;
        /**
         * 队列的首节点
         */
        private Node first;
        /**
         * 队列的尾节点
         */
        private Node last;
    
        /**
         *  定义结点类
         */
        private class Node{
            private Item item;
            private Node next;
        }
    
    
        /**
         * 使用构造方法初始化队列
         * @return
         */
        public LinkedQueue() {
            this.first = null;
            this.last = null;
            n = 0;
            assert check();
        }
    
        /**
         * 判空
         * @return
         */
        public boolean isEmpty(){
            // 如果对头为null,则表示队列为空
            return first == null;
        }
    
        /**
         * 求队列的长度
         * @return
         */
        public int size(){
            return n;
        }
    
        /**
         * 获取对头的元素
         * @return
         */
        public Item peek(){
            if(isEmpty()){
                throw new NoSuchElementException("队列为空,没有元素");
            }
            return first.item;
        }
    
        /**
         * 添加元素
         * @return
         */
        public void addQueue(Item item){
            Node oldNode = last;
            last = new Node();
            last.item = item;
            last.next = null;
            if(isEmpty()){
                first = last;
            } else{
                oldNode.next = last;
            }
            n ++;
            assert check();
        }
    
        /**
         * 删除元素
         * @return
         */
        public Item deQueue(){
            if(isEmpty()){
                throw new NoSuchElementException("队列为空,不能进行该操作");
            }
            Item item = first.item;
            first = first.next;
            n --;
            if(isEmpty()){
                last = null;
            }
            assert check();
            return item;
        }
    
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            for(Item item : this){
                sb.append(item + " ");
            }
            return sb.toString();
        }
    
        /**
         * 返回迭代器
         * @return
         */
        @Override
        public Iterator<Item> iterator() {
            return new ListIterator();
        }
    
        /**
         * 实现迭代器接口
         */
        private class ListIterator implements Iterator<Item> {
            /**
             * 队首的元素
             */
            private Node current = first;
    
            /**
             * 判断有无更多元素
             * @return
             */
            @Override
            public boolean hasNext()  { return current != null;                     }
            @Override
            public void remove()      { throw new UnsupportedOperationException();  }
    
            /**
             * 返回当前元素,移动指针(这样理解就好,实际java中无指针)
             * @return
             */
            @Override
            public Item next() {
                if (!hasNext()) throw new NoSuchElementException();
                Item item = current.item;
                current = current.next;
                return item;
            }
        }
    
        /**
         *检查队列的结构是否符合要求
         * @return
         */
        private boolean check() {
            if (n < 0) {
                return false;
            }
            else if (n == 0) {
                if (first != null) return false;
                if (last  != null) return false;
            }
            else if (n == 1) {
                if (first == null || last == null) return false;
                if (first != last)                 return false;
                if (first.next != null)            return false;
            }
            else {
                if (first == null || last == null) return false;
                if (first == last)      return false;
                if (first.next == null) return false;
                if (last.next  != null) return false;
    
                // check internal consistency of instance variable n
                int numberOfNodes = 0;
                for (Node x = first; x != null && numberOfNodes <= n; x = x.next) {
                    numberOfNodes++;
                }
                if (numberOfNodes != n) return false;
    
                // check internal consistency of instance variable last
                Node lastNode = first;
                while (lastNode.next != null) {
                    lastNode = lastNode.next;
                }
                if (last != lastNode) return false;
            }
    
            return true;
        }
    
        public static void main(String[] args) {
            LinkedQueue<String> linkedQueue = new LinkedQueue<>();
            linkedQueue.addQueue("迪丽热巴");
            linkedQueue.addQueue("杨紫");
            linkedQueue.addQueue("李溪芮");
            Iterator<String> it = linkedQueue.iterator();
            while(it.hasNext()){
                System.out.println(it.next());
            }
    
            System.out.println("============================================================");
            System.out.println("队列中有" + linkedQueue.size()  + "个元素");
            System.out.println("============================================================");
            System.out.println(linkedQueue);
        }
    }
  • 相关阅读:
    ios NSFileHandle
    ios NSString crash
    触摸方法
    代理中方法
    初始化时加载的方法
    [leetCode]209. 长度最小的子数组
    [leetCode]669. 修剪二叉搜索树
    [leetCode]763. 划分字母区间
    7.Jedis
    6.Redis事务(乐观锁)
  • 原文地址:https://www.cnblogs.com/wadmwz/p/9311077.html
Copyright © 2011-2022 走看看