上篇文章,小编已经带大家一起认识了队列,并且对队列的方法进行调用测试,下面我们将模拟实现一个队列,话不多说,上正文~
1.队列的模拟实现
队列的实现方法和链表的实现方式一模一样,这里我们首选双链表,前驱,后继,参数,头,尾,构造方法,这里就不多做赘述了,直接上代码~
public class MyQueue {
    static class ListNode{
        private int val;
        private ListNode prev;
        private ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
    private ListNode front;
    private ListNode rear;
   public int usedSize ;
}
2.插入数据
这里我们要用到的是头插法,前面提到。我们自己模拟实现的队列底层是一个双向链表,那么双向链表的头插我们前面也提到过,这里我们再复述一遍~
这里我们先定义一个 node节点,如图

接下来就是改节点,和具体代码

然而,只是这样,我们还忘记了一个重要细节,如果head为空,那么node就是整个链表的头,也是整个链表的尾,如图

代码如下
 public void offer(int x){
        ListNode node = new ListNode(x);
        if(front == null) {
            front = rear = node;
        }else {
            node.next = front;
            front.prev = node;
            front = node;
        }
        usedSize++;
    }
}调用测试
 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();
    }运行截图
注意,由于我们是头插法,实际存储数据为:4,3,2,1
3.弹出数据
1.删第一个数据(此方法比较不合理,不常用,大家了解即可,下面的删除头节点才是重点)
同样的,双向链表一样,考虑没有节点的情况(直接返回或抛出异常),只有一个节点的情况(将头,尾全部置空),和普通情况(front向后走,front前驱置为空)
代码如下
 public int pop(){
        //没有节点
        if (front == null){
            return -1;
        }
        int ret = front.val;//存下数据
        //只有一个节点
        if (front == rear){
            front= null;
            rear = null;
            usedSize--;
        }
        //至少有两个节点
        front=front.next;
        front.prev = null;
        usedSize--;
        return ret;
    }调用测试
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();
        System.out.println(myQueue.pop());
    }运行截图

2.删除最后一个元素 (这个才是重点)
同上,没有元素直接返回即可,只有一个元素(全部置空),普通情况(rear等于前驱,rear置为空),此时存下的数据也要改变
代码如下
 public int pop(){
        //没有节点
        if (front == null){
            return -1;
        }
        int ret = rear.val;//存下数据
        //只有一个节点
        if (front == rear){
            front= null;
            rear = null;
            usedSize--;
        }
        //至少有两个节点
        rear = rear.prev;
        rear.next = null;
        usedSize--;
        return ret;
    }调用测试
public class test {
    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();
        System.out.println(myQueue.pop());
    }
}
运行截图

4.获取队头元素
首先判空,如果为空,返回-1,如果不为空,返回front的值即可
代码如下
 public int peek(){
        if (front == null){
            return -1;
        }
        return front.val;
    }调用测试
public class test {
    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();
     
        System.out.println(myQueue.peek());
    }
}
运行截图

5.返回元数个数,判空
两种方法不复杂,一个直接返回有效元素个数,一共看usedSize是否为零,代码如下
  public int getUsedSize() {
        return usedSize;
    }
    public boolean isEmpty(){
        return usedSize == 0;
    }调用测试
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();
     
        System.out.println(myQueue.getUsedSize());
        System.out.println(myQueue.isEmpty());
    }运行截图

那么到此为止,本篇文章就到此结束啦~下一篇文章我们将继续学习队列的相关知识,敬请期待叭~觉得小编讲的还可以的可以留个关注支持一下,谢谢观看~




















