数据结构-队列-顺序队列(Java语言)

详细的代码可见github:

https://github.com/AbitGo/myClassWork/tree/master/workspace_ds

队列一般分为顺序队列以及链队列,本文主要讲述顺序队列。

所需要实现的接口功能。

package com.company.ch3.queue;

public interface IQueue {
    public void clear();
    public boolean isEmpty();
    public int length();
    public Object peek();
    public void offer(Object x);
    public Object poll();
}

顺序队列主要实现代码:

package com.company.ch3.queue;

public class CircleSqQueue implements IQueue {
    private Object[] queueElem;
    private int front;
    private int rear;
    private int maxSize;

    public CircleSqQueue(int maxSize) {
        this.maxSize = maxSize;
        front = rear = 0;
        queueElem = new Object[maxSize];
    }

    public Object[] getQueueElem() {
        return queueElem;
    }

    public void setQueueElem(Object[] queueElem) {
        this.queueElem = queueElem;
    }

    public int getFront() {
        return front;
    }

    public void setFront(int front) {
        this.front = front;
    }

    public int getRear() {
        return rear;
    }

    public void setRear(int rear) {
        this.rear = rear;
    }

    public int getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    @Override
    public void clear() {
        //将头与尾设置为0即可
        front = rear = 0;
    }

    @Override
    public boolean isEmpty() {
        if (front == rear) {
            return true;
        }
        return false;
    }

    @Override
    public int length() {
        return (rear - front + queueElem.length) % queueElem.length;
    }

    @Override
    public Object peek() {
        if (isEmpty()) {
            return null;
        } else
            return queueElem[this.getFront()];
    }

    @Override
    public void offer(Object x) {
        //入队

        //当队列已满的时候
        if ((rear + 1) % queueElem.length == front) {
            System.out.println("the Sqack is full");
            return;
        } else {
            queueElem[rear] = x;
        }

        //修改队尾指针
        rear = (rear + 1) % queueElem.length;

    }

    @Override
    public Object poll() {
        //出队

        //为空栈的时候则返回空指针
        if (isEmpty()) {
            return null;
        } else {
            Object t = queueElem[front];
            front = (front + 1) % queueElem.length;
            return t;
        }
    }
}

测试类:

package com.company.ch3.queue;

public class CircleSqQueueTest {
    public static void main(String[] args){
        CircleSqQueue sqStack = new CircleSqQueue(20);
        System.out.println("----------插入操作:开始----------");
        sqStack.offer(1);
        sqStack.offer(2);
        sqStack.offer(3);
        System.out.println("----------插入操作:结束----------");

        System.out.println("----------查看队列操作:开始----------");
        System.out.println(sqStack.peek());
        System.out.println("弹出栈顶元素:"+sqStack.poll());
        System.out.println(sqStack.peek());
        System.out.println("弹出栈顶元素:"+sqStack.poll());
        System.out.println("----------查看队列操作:结束----------");

        System.out.println("----------查看队列长度操作:开始----------");
        System.out.println("长度为:"+sqStack.length());
        System.out.println("----------查看队列长度操作:结束----------");

        System.out.println("----------清除队列操作:开始----------");
        sqStack.clear();
        System.out.println("----------清除队列操作:结束----------");
    }
}

测试结果:

----------插入操作:开始----------
----------插入操作:结束----------
----------查看队列操作:开始----------
1
弹出栈顶元素:1
2
弹出栈顶元素:2
----------查看队列操作:结束----------
----------查看队列长度操作:开始----------
长度为:1
----------查看队列长度操作:结束----------
----------清除队列操作:开始----------
----------清除队列操作:结束----------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值