Java实现队列

一.创建队列接口

public interface Queue {
    void offer(Object data);

    Object peek();

    void display();

    boolean isEmpty();

    int size();

    void clear();
}

二.数组实现循环队列

public class ArrayQueue implements Queue {

    private final Object[] elements;
    private int front;
    private int rear;
    private int initCapacity = 8;

    ArrayQueue() {
        elements = new Object[initCapacity];
        front = 0;
        rear = 0;
    }


    @Override
    public void offer(Object data) {
        if ((rear + 1) % initCapacity == front) {
            System.out.println("Queue is overflow");
        } else {
            elements[rear] = data;
            rear = rear + 1;
        }
    }

    @Override
    public Object peek() {
        if (isEmpty()) {
            System.out.println("Queue is Empty");
            throw new RuntimeException("Queue is Empty");
        } else {
            Object data = elements[front];
            front = (front + 1)%initCapacity;
            return data;
        }
    }

    @Override
    public void display() {
        if (front != rear ) {
            for (int i = front; i < front+ size(); i++) {
                System.out.println(elements[i%initCapacity]);
            }
        }
    }

    @Override
    public boolean isEmpty() {
        return front == rear;
    }

    @Override
    public int size() {
        return (rear - front + initCapacity)%initCapacity;
    }

    @Override
    public void clear() {

    }
}

三.链表实现队列

public class LinkQueue implements  Queue{
    private  Node frontNode;
    private  Node rear;
    private int size;

    LinkQueue(){
         frontNode = null;
         rear = null;
    }

    @Override
    public void offer(Object data) {
        if (size() == 0) {
            rear = new Node(data);
            frontNode = rear;
            size++;
        }else {
            Node node = new Node(data);
            rear.next = node;
            rear = node;
            size++;
        }


    }

    @Override
    public Object peek() {
        if (size() == 0) {
            throw new RuntimeException("queue is empty");
        }else {
            frontNode = frontNode.next;
            size--;
            return frontNode.data;
        }
    }

    @Override
    public void display() {
        if (frontNode != null) {
            System.out.println( frontNode.data.toString());
            Node curNode = frontNode;
            while (curNode.next != null) {
                curNode = curNode.next;
                System.out.println( curNode.data.toString());
            }
        }

    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void clear() {

    }


    class Node{
        Object data;
        Node next;

        Node() {

        }

        public Node(Object data) {
            this.data = data;
        }
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值