package com.my;
/**
 * 链式队列
 * @author wanjn
 *
 */
public class LinkedQueue {
        private Node head;
        private int size;
        private Node last;
        //入队列
        public void add(Object value){
            if (head ==null) {
                head =last = new Node(null, value);
            }else {
                last.next = new Node(null, value);
                last = last.next;
            }
        }
        //出队列
        public Object poll(){
            Object oldValue =null;
            if (head!=null) {
                oldValue = head.value; 
                head = head.next;
            }
            return oldValue;
            
        }
        @Override
        public String toString() {
            Node temp = head;
            String result= "";
            while (temp!=null) {
                result +=temp.value+" ,";
                temp = temp.next;
            }
            return result;
        }
        private class Node{
            Node next;
            Object value;
            Node(Node next,Object value){
                this.next = next;
                this.value = value;
            }
        }
}

 

 posted on 2018-01-26 17:04  改变一下  阅读(161)  评论(0编辑  收藏  举报