单链表实现队列

class Node{
    int data;
    Node next;
    public Node(int data){
        this.data = data;
    }
}

public class MyQueue {
    public Node head;
    public Node tail;
    int useSize;

    public void offer(int data){
        if(head == null){
            this.head = new Node(data);
            this.tail = head;
            useSize++;
        }
        else {
            this.tail.next = new Node(data);
            this.tail = this.tail.next;
            useSize++;
        }

    }

    public int poll(){
        if(this.head == null){
            return -1;
        }
        int oldData = head.data;
        this.head = this.head.next;
        useSize--;
        return oldData;
    }

    public int peek(){
        if(this.head == null){
            return -1;
        }
        return this.head.data;
    }

    public int size(){
        return useSize;
    }

}
发布了28 篇原创文章 · 获赞 3 · 访问量 736

猜你喜欢

转载自blog.csdn.net/XDtobaby/article/details/103056607
今日推荐