Java写数据结构:队列

1.概念:

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾(Tail/Rear) 出队列:进行删除操作的一端称为队头Head/Front)

2.队列的方法:

接下来模拟实现上述方法:

双向链表实现:

先创建最基本的成员变量和构造方法,内部类

复制代码
public class MyQueue {
    //内部类
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
     public ListNode first;
    public ListNode last;
    public int useSize;

}

实现offer方法

复制代码
 public void offer(int val){
        ListNode node = new ListNode(val);
        if(isEmpty()) {
            first = last = node;
        }else{
            last.next = node;
            node.prev = last;
            last = last.next;
        }
        useSize++;
    }
public boolean isEmpty(){
        return useSize == 0;
    }

实现poll方法

复制代码
public int poll(){
        if(isEmpty()){
            return -1;
        }else {
            int val = first.val;
            first = first.next;
            if(first != null){
                first.prev = null;
            }
            useSize--;
            return val;
        }
    }
public boolean isEmpty(){
        return useSize == 0;
    }

实现peek方法

复制代码
public int peek(){
        if(isEmpty()){
            return -1;
        }else {
            int val = first.val;
            return val;
        }
    }

    public boolean isEmpty(){
        return useSize == 0;
    }

测试:

复制代码
public class Test {
 //MyQueue
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);
        myQueue.offer(2);
        myQueue.offer(3);
        myQueue.offer(4);

        System.out.println(myQueue.useSize);
        System.out.println(myQueue.peek());
        System.out.println(myQueue.poll());
        System.out.println(myQueue.peek());
        System.out.println(myQueue.useSize);
    }
}

用循环数组实现队列:

先创建最基本的成员变量和构造方法

复制代码
public class MyCircleQueue {
 public int front;
    public int rear;
    public int[] elem;

    public MyCircleQueue(int k) {
        elem = new int[k + 1];
    }
}

实现入队方法

复制代码
 public boolean enQueue(int val){
        if(isFull()){
            return false;
        }else {
            elem[rear] = val;
            rear = (rear + 1) % elem.length;
            return true;
        }
    }
public boolean isFull(){
        return front == (rear + 1) % elem.length;
    }

实现出队方法

复制代码
public boolean deQueue(){
    if(isEmpty()){
        return false;
    }else {
        front = (front + 1) % elem.length;
        return true;
    }
}
public boolean isEmpty(){
        return front == rear;
    }

获得队头元素

复制代码
//获得队列的头元素
    public int Front(){
        if(isEmpty()){
            return -1;
        }else{
            return elem[front];
        }
    }
 public boolean isEmpty(){
        return front == rear;
    }

测试:

复制代码
public class Test {
//循环数组实现队列
    public static void main(String[] args) {
        MyCircleQueue myCircleQueue = new MyCircleQueue(10);
        myCircleQueue.enQueue(1);
        myCircleQueue.enQueue(2);
        myCircleQueue.enQueue(3);
        myCircleQueue.enQueue(4);

        System.out.println(myCircleQueue.Front());
        System.out.println(myCircleQueue.deQueue());
        System.out.println(myCircleQueue.Front());
        System.out.println(myCircleQueue.rear);
    }
}
相关推荐
毕业设计制作和分享2 小时前
springboot150基于springboot的贸易行业crm系统
java·vue.js·spring boot·后端·毕业设计·mybatis
he___H4 小时前
数据结构-移位
数据结构
电子_咸鱼5 小时前
LeetCode——Hot 100【电话号码的字母组合】
数据结构·算法·leetcode·链表·职场和发展·贪心算法·深度优先
仰泳的熊猫5 小时前
LeetCode:785. 判断二分图
数据结构·c++·算法·leetcode
haoly19897 小时前
数据结构和算法篇-归并排序的两个视角-迭代和递归
数据结构·算法·归并排序
小梁努力敲代码7 小时前
java数据结构--List的介绍
java·开发语言·数据结构
摸鱼的老谭7 小时前
构建Agent该选Python还是Java ?
java·python·agent
lang201509288 小时前
Spring Boot 官方文档精解:构建与依赖管理
java·spring boot·后端
夫唯不争,故无尤也8 小时前
Tomcat 启动后只显示 index.jsp,没有进入你的 Servlet 逻辑
java·servlet·tomcat
zz-zjx8 小时前
Tomcat核心组件全解析
java·tomcat