• 数据结构学习(五) Java链表实现队列


    package queue;
    
    /**
     * @Title: LinkedListQueue
     * @ProjectName demo
     */
    public class LinkedListQueue<E> implements Queue<E> {
    
        private class Node {
            private Node next;
            private E e;
    
            public Node(E e, Node next) {
                this.next = next;
                this.e = e;
            }
    
            public Node(E e) {
                this(e, null);
            }
    
            @Override
            public String toString() {
                return e.toString();
            }
        }
    
        private int size;
        private Node head, tail;
    
        public LinkedListQueue() {
            size = 0;
            head = null;
            tail = null;
        }
    
        @Override
        public int getSize() {
            return size;
        }
    
        @Override
        public boolean isEmpty() {
            return size == 0;
        }
    
        @Override
        public void enqueue(E e) {
            if (tail == null) {
                tail = new Node(e);
                head = tail;
            }else{
                tail.next = new Node(e);
                tail = tail.next;
            }
            size ++;
        }
    
        @Override
        public E dequeue() {
            if (isEmpty()){
                throw new IllegalArgumentException("dequeue error..");
            }
            Node cur = head;
            head = head.next;
            cur.next = null;
            if (head == null){
                tail = null;
            }
            size--;
            return cur.e;
        }
    
        @Override
        public E getFront() {
            if (isEmpty()){
                throw new IllegalArgumentException("GetFront error..");
            }
            return head.e;
        }
    
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("front:");
            Node h = head;
            while (h != null) {
                sb.append("--->"+h.e);
                h = h.next;
            }
            sb.append("tail");
            return sb.toString();
        }
    }
    

      

  • 相关阅读:
    游泳池 (Standard IO)
    Antimonotonicity (Standard IO)
    开花 (Standard IO)
    Wild Number (Standard IO)
    数码问题 (Standard IO)
    输油管道 (Standard IO)
    猴子摘桃 (Standard IO)
    二叉树 (Standard IO)
    iis运行asp.net页面提示“服务器应用程序不可用”的解决办法_.NET.
    SVN安装配置与使用
  • 原文地址:https://www.cnblogs.com/412013cl/p/11008499.html
Copyright © 2020-2023  润新知