JAVA 数据结构与算法之————队列

JAVA 数据结构与算法之————队列

直接上代码:
1,线性表作为队列

package stackandqueue;

import java.util.ArrayList;

public class LineQueue<E> {
    private ArrayList<E> data = new ArrayList<>();
    private int front = 0;  // 队头指针 
    private int rear = 0;  // 队尾指针

    LineQueue() {}

    //    获取队列长度
    public int length() {
        return rear - front;
    }

    //    判空
    public boolean isEmpty() {
        if (this.front == this.rear) {
            return true;
        } else {
            return false;
        }
    }

    //    进队
    public boolean add(E e) {

        data.add(e);
        this.rear++;
        return true;


    }

    //    出队
    public E poll() {
        if (isEmpty()) {
            throw new RuntimeException("队列已空");
        } else {
            return (E) data.get(front++);
        }
    }

    //    查看队头元素,但不删除
    public E peek() {
        if (isEmpty()) {
            throw new RuntimeException("队列已空");
        } else {
            return (E) data.get(front);
        }
    }

// 测试
    public static void main(String[] args) {
        LineQueue<Integer> queue = new LineQueue<>();

        queue.add(1);
        queue.add(2);

        System.out.println("length: " + queue.length());
        System.out.println("poll:  " + queue.poll());
        System.out.println("peek: " + queue.peek());
        System.out.println("isEmpty: " + queue.isEmpty());


    }
}

2,链表作为队列

package stackandqueue;

public class LinkQueue<E> {

    private int size;
    private Node front;
    private Node rear;

    private class Node<E> {
        E e;
        Node next;

        Node() {
        }

        ;

        Node(E e) {
            this.e = e;
            this.next = null;
        }
    }

    LinkQueue() {
        this.size = 0;
        this.front = this.rear = null;
    }

    //    获取Node的元素值
    private E getE(Node n) {
        return (E) n.e;
    }

    //    获取队的大小
    public int length() {
        return size;
    }

    //    判空
    public boolean isEmpty() {
        if (this.size == 0) {
            return true;
        } else {
            return false;
        }
    }

    //    进队
    public boolean add(E e) {
        if (isEmpty()) {
            Node<E> n = new Node(e);
            this.front = this.rear = n;
            size++;
            return true;
        } else {
            Node<E> n = new Node(e);
            this.rear.next = n;
            this.rear = n;
            size++;
            return true;
        }
    }

    //    出队
    public E poll() {
        if (isEmpty()) {
            throw new RuntimeException("队列已空");
        } else {
            Node<E> value = this.front;
            this.front = this.front.next;
            value.next = null;
            size--;
            return getE(value);
        }
    }

    //    查看队首元素
    public E peek() {
        if (isEmpty()) {
            throw new RuntimeException("队列已空");
        } else {
            return getE(this.front);
        }
    }

    //    测试
    public static void main(String[] args) {
        LinkQueue<String> queue = new LinkQueue<>();

        queue.add("a");
        queue.add("b");

        System.out.println("length: " + queue.length());
        System.out.println("poll:  " + queue.poll());
        System.out.println("peek: " + queue.peek());
        System.out.println("isEmpty: " + queue.isEmpty());

    }
}

3.用LInkedList实现队列更简单

package stackandqueue;

import java.util.LinkedList;


public class LinkedQueue<E> {
    LinkedList<E> linkedList = new LinkedList<>();

    //    获取队列元素个数
    public int length() {
        return linkedList.size();
    }

    //    判空
    public boolean isEmpty() {
        return linkedList.isEmpty();
    }

    //    进队
    public boolean add(E e) {
        return linkedList.add(e);
    }

    //    出队
    public E poll() {
        return linkedList.removeFirst();
    }

    //    查看队首元素
    public E peek() {
        return linkedList.getFirst();
    }
    
//    测试

    public static void main(String[] args) {
        LinkedQueue<Integer> queue = new LinkedQueue<>();

        queue.add(1);
        queue.add(2);

        System.out.println("length: " + queue.length());
        System.out.println("poll:  " + queue.poll());
        System.out.println("peek: " + queue.peek());
        System.out.println("isEmpty: " + queue.isEmpty());


    }
}

4,还有更简单的实现方法,比如:LinkedBlockingQueue 和 ConcurrentLinkedQueue,这两个的区别可以看(https://blog.csdn.net/qq_33591903/article/details/82693558),这里实现一下ConcurrentLinkedQueue:

public static void main(String[] args) {
        ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();

//        判空
        boolean empty = queue.isEmpty();
        System.out.println(empty);

//        进队
        queue.add(1);

//        查看队首元素,但不出队
        int valuePeek = queue.peek();
        System.out.println(valuePeek);

//        出队
        int valuePoll = queue.poll();
        System.out.println(valuePoll);
//
    }

5, 循环队列

package stackandqueue;

import java.util.ArrayList;

public class LoopLineQueue<E> {

    private ArrayList<E> data = null;
    private int maxSize;
    private int front;
    private int rear;
    private int length;

    LoopLineQueue(){
        this.length = 0;
        this.front = this.rear = 0;
        this.data = new ArrayList<>(10);
        this.maxSize = 10;
    }
    LoopLineQueue(int initialSize){
        if(initialSize < 0){
            throw new RuntimeException("初始化队列的大小不能小于0" + initialSize);
        }else{
            this.length = 0;
            this.maxSize = initialSize;
            data = new ArrayList<>(initialSize);
            this.front = this.rear = 0;
        }
    }
    //    获取队列长度
    public int length(){

        return this.length;
    }
    //    判空
    public boolean isEmpty(){
        if(length == 0){
            return true;
        }else{
            return false;
        }
    }
    //    进队
    public boolean add(E e){
        if(this.length == this.maxSize){
            throw new RuntimeException("队列已满");
        }else{
            data.add(e);
            this.rear = (this.rear + 1) % this.maxSize; //这里与非循环队列不同
            length++;
            return true;
        }
    }
    //    出队
    public E poll(){
        if(isEmpty()){
            throw new RuntimeException("队列已空");
        }else{
            E value = (E)data.get(front);
            this.front = (this.front + 1) % this.maxSize; //这里与非循环队列不同
            length--;
            return value;
        }
    }
    //    查看队头元素,但不删除
    public E peek(){
        if(isEmpty()){
            throw new RuntimeException("队列已空");
        }else{
            return (E)data.get(front);
        }
    }
}

代码拙劣,望大家多提意见,共同进步。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值