用链表和数组实现队列

队列的原则是先进先出,主要有offer,poll,peek,isEmpty和size方法.

用链表实现:

package Test;

class Node{
    int var;
    Node next;

    Node(int var, Node next){
       this.var = var;
       this.next = next;
   }

    Node(int var){
       this(var,null);
   }
}

public class QueueTest {
    private Node head = null;
    private Node tail = null;
    private int size = 0;

    //元素进队
    public void offer(int var){
        Node node = new Node(var);
        //处理队为空的情况
        if (tail == null){
            head = node;
        }else{
            tail.next = node;
        }
        tail = node;
        size++;
    }

    //元素出队
    public int poll(){
        if (size == 0){
            throw new RuntimeException("队列为空");
        }

        Node oldHead = head;
        head = head.next;
        //若head指向为空
        if (head == null){
            tail = null;
        }
        size--;
        return oldHead.var;
    }

    //返回队首元素的值
    public int peek(){
        if (size == 0){
            throw new RuntimeException("队列为空");
        }
        return head.var;
    }

    //判定队是否为空
    public boolean isEmpty(){
        return size == 0;
    }

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

    public static void main(String[] args){
        QueueTest queue = new QueueTest();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        while (!queue.isEmpty()){
            System.out.println("队首元素:");
            int ret = queue.peek();
            System.out.println(ret);
            System.out.println("出队元素:");
            System.out.println(queue.poll());
        }
    }
}

用数组实现:

package Test;

public class QueueTest2 {
    private int[] data = new int[100];
    private int head = 0;  //队首元素下标
    private int tail = 0;  //队尾元素下标
    private int size = 0;  //队列大小

    //队尾插入元素
    public boolean offer(int val){
        //若队列已满
        if (size == data.length){
            return false;
        }

        data[tail] = val;  //插入数据
        tail++;
        //处理tail走到队尾,但队不满的情况
        if (tail == data.length){
            tail = 0;
        }
        size++;  //实际已存数据加1
        return true;
    }

    //元素出队方法
    public Integer poll(){
        //处理队列为空的情况
        if (size == 0){
            return null;
        }

        //记录队首元素
        Integer ret = data[head];
        head++;
        //若head走到队尾,但队不满的情况
        if (head == data.length){
            head = 0;
        }
        size--;  //实际已存数据减1
        return ret;
    }

    //获取队首元素
    public Integer peek(){
        //处理队列为空的情况
        if (size == 0){
            return null;
        }
        //记录队首元素
        Integer ret = data[head];
        return ret;
    }

    //判定队是否为空
    public boolean isEmpty(){
        return size == 0;
    }

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

    public static void main(String[] agrs) {
        QueueTest2 queue = new QueueTest2();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        while (!queue.isEmpty()){
            System.out.println("队首元素:");
            Integer ret = queue.peek();
            System.out.println(ret);
            System.out.println("出队元素:");
            System.out.println(queue.poll());
        }
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值