数据结构-队列-顺序队列(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
----------查看队列长度操作:结束----------
----------清除队列操作:开始----------
----------清除队列操作:结束----------
发布了84 篇原创文章 · 获赞 39 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/Abit_Go/article/details/104137898