数据结构----队列

2018-08-21  本文已影响7人  pgydbh

结构

先进先出
需要size()----大小
需要push()----压入
需要pop()----弹出
采用链表不需要扩展

代码

public class Queue<T> {

    private Node<T> head;
    private Node<T> poi;
    private int size;

    public int size(){
        return size;
    }

    public void push(T t){
        if (head == null){
            head = new Node<>();
            poi = head;
        } else {
            poi = poi.next = new Node<>();
        }
        poi.t = t;
        size++;
    }

    public T pop(){
        if (size > 0){
            T t = head.t;
            head = head.next;
            return t;
        } else {
            return null;
        }
    }


    public static class Node<T>{
        public T t;
        public Node<T> next;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读