队列演示

队列

队列是先进先出,与堆栈不一样

数组实现

数组实现主要采用双指针方式

package com.example.data;

/**
 * *@ClassName QueueList
 * *@Author yyc
 * *@Date 2020/6/15 15:15
 **/
public class QueueList {

    //头指针
    private int Head;
    //尾指针
    private int Tail;

    private int[] result;

    private int len;

    //初始化队列数组
    public QueueList(int length)
    {
        this.len=length;
        this.result=new int[length];
        this.Head=0;
        this.Tail=0;
    }

    //往队列中压入数据
    public void Enqueue(int value)
    {
        if(Tail<len)
        {
            result[Tail++] = value;
        }
    }

    public int Dequeue() throws Exception {
        if(Head<=Tail)
        {
            return result[Head++];
        }
        else
           throw  new Exception("超出范围");
    }


}

在这里插入图片描述

链表队列

package com.example.data;

/**
 * *@ClassName QueueLinked
 * *@Author yyc
 * *@Date 2020/6/15 16:17
 * 链表队列
 **/
public class QueueLinked {

    //头链表
    public Node head;
    //尾链表
    public Node Tail;

    //链表长度
    private  int N;

    public QueueLinked(){
        head=new Node(null,null);
        Tail=null;
        N=0;
    }

    /**
     * 判断是否为空链表
     * @return
     */
    public boolean isEmpty(){
        return N==0;
    }


    public void push(Integer value)
    {
       if(Tail==null)
       {
           Tail=new Node(value,null);
           head.next=Tail;
       }
       else
       {
            Node oldLast=Tail;
            Tail=new Node(value,null);
            oldLast.next=Tail;
       }
       N++;
    }

    public Integer pop(){
        if(isEmpty()) {
            return null;
        }

        Node oldFirst=head.next;

        head.next=oldFirst.next;
        N--;
        if(isEmpty())
        {
            Tail=null;
        }

        return (Integer) oldFirst.item;
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值