#java数据结构#_队列_FIFO_链表实现

package schwinn20201026;

import java.util.Iterator;

public class Queue<Sch> implements Iterable<Sch> {
    public Node first;//最先插入之元素
    public Node last;//最后插入之元素
    public int N;//元素数量
    private class Node{
        Node next;//下指针
        Sch sch;//本例元素
    }
    public int size(){
        return N;
    }
    public boolean isEmpty(){
        return first==null;
    }
    public void enqueue(Sch s){
        Node oldlast=last;
        last=new Node();
        last.next=null;
        last.sch=s;
        if(isEmpty()){
            first=last;
        }
        else{
            oldlast.next=last;
        }
        N++;
    }
    public Sch dequeue(){
            if(isEmpty()) return null;
            Sch sch=first.sch;
            first=first.next;
            if(isEmpty()){
                last=null;
            }
            N--;
            return sch;
    }
    public Iterator<Sch> iterator(){
        return new QueueIterator();
    }
    private class QueueIterator implements Iterator<Sch>{
        private Node current=first;
        public boolean hasNext(){
            return current!=null;
        }
        public void remove(){
        }
        public Sch next(){
            Sch s=current.sch;
            current=current.next;
            return s;
        }
    }

}

 

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页