zoukankan      html  css  js  c++  java
  • [数据结构]-链表实现队列

    [数据结构]-链表实现队列

    如有问题,请指出呀。感谢。

    package com.cn.jichu.day09;
    
    public class LinkedQueue<E> {
    
        /**
         * 首节点
         */
        private Node head;
        /**
         * 尾节点
         */
        private Node tail;
        /**
         * 当前栈中元素个数
         */
        private int size;
    
        public LinkedQueue(){
            this.size = 0;
        }
    
        /**
         * 入列
         * @param e
         */
        public void enqueue(E e){
            Node node = new Node(e);
            if(head == null && tail == null){
                head = tail = node;
                size ++;
                return;
            }
            tail.next = node;
            tail = node;
            size ++;
        }
    
        /**
         * 出列
         * @return
         */
        public E dequeue(){
            if(head==null){
                throw  new IllegalArgumentException("当前队列无元素");
            }
    
            if(head.equals(tail)){
                System.out.println("head.equals(tail)");
                E e = head.data;
                head = tail = null;
                size --;
                return e;
            }
    
            E e = head.data;
            head = head.next;
            size --;
            return e;
        }
    
        /**
         * 获取队列中的元素个数
         * @return
         */
        public int getSize(){
            return size;
        }
    
    
    
    
        private class Node{
            private Node next;
            private E data;
    
            public Node(E data) {
                this.data = data;
            }
        }
    
        @Override
        public String toString(){
            StringBuilder ret = new StringBuilder();
            ret.append("queue  head[");
            Node node = head;
            while (node!=null){
                ret.append(node.data + "-->");
                node = node.next;
            }
            ret.append("null ] tail");
            return ret.toString();
        }
    
    }
  • 相关阅读:
    Jquery
    JavaScript
    poj--2115 C Looooops
    poj--3970 party
    poj 1061 青蛙的约会
    hdu1250--Hat's Fibonacci
    2318--TOYS
    扩展欧几里得--让你一次刷个够
    关于大数加法的解法
    有关环形数组的约瑟夫问题
  • 原文地址:https://www.cnblogs.com/anny0404/p/10655090.html
Copyright © 2011-2022 走看看