算法通关村第五关|青铜|基于链表实现队列

基于链表实现队列

public class LinkQueue {
    // front的next指向首部结点
    private Node front;
    // rear记录尾部结点
    private Node rear;
    private int size;
    public LinkQueue() {
        this.front = new Node(0);
        this.rear = new Node(0);
    }

    // 入队
	public void push(int value) {
        Node newNode = new Node(value);
        Node temp = front;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = newNode;
        rear = newNode;
        size++;
    }

    // 出队
	public int pull() {
        if (front.next == null) {
            System.out.println("队列为空");
        }
        Node firstNode = front.next;
        front.next = firstNode.next;
        size--;
        return firstNode.data;
    }

    // 遍历
    public void traverse() {
        Node temp = front.next;
        while (temp != null) {
            System.out.println(temp.data + "\t");
            temp = temp.next;
        }
    }

    // 结点结构
    static class Node {
        public int data;
        public Node next;
        public Node(int data) {
            this.data = data;
        }
    }
}

如果对您有帮助,请点赞关注支持我,谢谢!❤
如有错误或者不足之处,敬请指正!❤
个人主页:星不易 ♥
算法通关村专栏:不易|算法通关村 ♥

你可能感兴趣的:(不易,算法通关村,算法,java,算法通关村)