Java数组实现循环队列、Java链表实现队列

Java实现队列的两种方式(链表,循环数组)

数组实现循环队列

1、需要一个theSize变量,来判断队列是否填满了数组,或者判断队列是否为空

代码如下

public class cycleQueue<T>
{
    private int front ;//队头
    private int back;  //队尾
    private int  theSize;  //队伍长度
    private  T[] theQueue=(T[]) new Object[10]; //数组

    /*
     * 方法名:enqueue
     * 作用:入队列
     */
    public boolean enqueue(T x)
    {
        if (theSize==theQueue.length) //判断队列是否已满
            return false;

        theQueue[back]=x;//在队列尾部添加新成员
        back=(1+back)%theQueue.length;
        theSize++;
        return true;
    }
    /*
     * 方法名:dequeue
     * 作用:出队列
     */
    public T dequeue()
    {
        if (theSize==0) //判断队列是否为空
            return null;

        T dequeueVal=theQueue[front];//不为空,返回队列的对头
        theSize--;
        front=(front+1)%theQueue.length;
        return dequeueVal;
    }

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

        //测试无误 
        for(int i=1;i<=10;i++)
            test.enqueue(i);
        for(int i=1;i<=5;i++)
            System.out.println(test.dequeue());
        System.out.println();
        for(int i=11;i<=15;i++)
            test.enqueue(i);
        for(int i=1;i<=10;i++)
            System.out.println(test.dequeue());     
    }

}

链表实现队列

1、使用单项链表

2、可以使用两种方式判断队列是否为空 1是theSize的大小,2是front.next是否等于null。下面的代码两种判断方式都使用了

public class QueueOfList<E> 
{
    private Node front=null; //队头
    private Node back=null;  //对尾
    private int size;        //队伍大小

    /*
     *    节点定义成内部类
     */
    private class Node//链表节点类
    {
        Node next; //指向下一节点
        E data;//存放数据
        public Node(Node next, E data) //构造方法
        {
            super();
            this.next = next;
            this.data = data;
        }

    }
    /*
     *   方法:isEmpty
     *   作用:判断队列是否为空
     */
    public boolean isEmpty()//队列是否为空
    {
        return front==null;
    }
    /*
     *   方法:enQueue
     *   作用:入列
     */
    public void enQueue(E newVal)//入队
    {
        Node newNode=new Node(null, newVal);
        if (front==null)//分成队伍为空,与不空两种情况
        {
            front=newNode;
            back=newNode;
        }
        else
        {
            back.next=newNode;
            back=newNode;
        }
    }
    /*
     *   方法:deQueue
     *   作用:出列,空列返回null
     */
    public E deQueue()
    {

        if (!isEmpty()) //队伍不为空
        {
            Node put=front;
            front=front.next;
            return put.data;
        }
        else//队伍为空,返回null
            return null;
    }
    public static void main(String[] args)
    {
        QueueOfList<String> test=new QueueOfList<>();
        //测试
        test.enQueue("111");
        test.enQueue("222");
        test.enQueue("333");
        while(!test.isEmpty())
            System.out.println(test.deQueue());
        test.enQueue("111");
        test.enQueue("111");
        test.enQueue("111");
        System.out.println(test.deQueue());

        System.out.println(test.deQueue());
        System.out.println(test.deQueue());
    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值