数据结构——循环队列

  这里所编写的就是一个循环队列,具体的说明在程序的注释里就已经写明了,并配有相应的测试代码。

Queue

package com.lyc.linearTable.queue;

import com.lyc.util.ObjectHolder;

public interface Queue {

    /**
     * 队列的初始化
     * @param size
     */
    void initQueue(int size);

    /**
     * 销毁队列
     */
    void destroyQueue();

    /**
     * 队列判空
     * @return
     */
    boolean queueEmpty();

    /**
     * 队列判满
     * @return
     */
    boolean queueFull();

    /**
     * 队列长度
     * @return
     */
    int queueLenth();

    /**
     * 插入队列
     * @param ele
     * @return
     */
    boolean enQueue(Object ele);

    /**
     * 删除队列
     * @param ele
     * @return
     */
    boolean deQueue(ObjectHolder ele);

    /**
     * 遍历队中的元素
     */
    void queueTraverse();

}

QueueImpl

package com.lyc.linearTable.queue;

import com.lyc.util.ObjectHolder;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class QueueImpl implements Queue {

    private int m_queueLenth = 0;              //队列元素个数
    private int m_head = 0;                    //队头
    private int m_tail = 0;                    //队尾
    private Object[] m_pList = null;           //队列
    private int m_queueCapacity = 0;           //队列容量

    /**
     * 队列的初始化
     * @param size
     */
    public void initQueue(int size) {
        this.m_pList = new Object[size];
        this.m_head = 0;
        this.m_tail = 0;
        this.m_queueLenth = 0;
        this.m_queueCapacity = size;
    }

    /**
     * 销毁队列
     */
    public void destroyQueue() {
        this.m_pList = null;
        this.m_head = 0;
        this.m_tail = 0;
        this.m_queueLenth = 0;
        this.m_queueCapacity = 0;
    }

    /**
     * 队列判空
     * @return
     */
    public boolean queueEmpty() {
        if(this.m_queueLenth == 0){
            return true;
        }
        return false;
    }

    /**
     * 队列判满
     * @return
     */
    public boolean queueFull() {
        if(this.m_queueLenth == this.m_queueCapacity){
            return true;
        }
        return false;
    }

    /**
     * 队列长度
     * @return
     */
    public int queueLenth() {
        return this.m_queueLenth;
    }

    /**
     * 插入队列
     * @param ele
     * @return
     */
    public boolean enQueue(Object ele) {
        //如果对非满,则在队中插入元素
        if(!this.queueFull()){
            //将元素插入队尾
            this.m_pList[this.m_tail] = ele;
            //队尾指针向后移动1个单位长度
            this.m_tail ++;
            //对队尾求余,因为在循环队列中,队尾指针不可能指向队外的地址
            this.m_tail = this.m_tail % this.m_queueCapacity;
            //队长+1
            this.m_queueLenth ++;
            return true;
        }
        return false;
    }

    /**
     * 删除队列
     * @param ele
     * @return
     */
    public boolean deQueue(ObjectHolder ele) {
        //如果队列非空,则继续删除元素
        if(!this.queueEmpty()){
            //对头出列,将其赋值给ObjectHolder容器,由其负责带出
            ele.object = this.m_pList[this.m_head];
            //队头指针后移+1
            this.m_head ++;
            //由于对头指针不可以指向队外的地址,所以说需要对其取余
            this.m_head = this.m_head % this.m_queueCapacity;
            //队头元素出队后,元素的长度-1
            this.m_queueLenth --;
            return true;
        }
        return false;
    }

    /**
     * 遍历队中的元素
     */
    public void queueTraverse() {
        //将队头指针赋给head
        int head = this.m_head;
        //控制循环次数
        for(int i = 0;i < this.m_queueLenth;i ++){
            //打印队头
            log.info(this.m_pList[head].toString());
            //打印完成后head向后位移1个单位长度
            head ++;
            //head对头指针不能出队,所以说得对其取余
            head = head % this.m_queueCapacity;
        }
    }
}

QueueTest

package com.lyc.linearTable.queue;

import com.lyc.linearTable.entity.Person;
import com.lyc.util.ObjectHolder;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;

@Slf4j
public class QueueTest {

    private Queue queue = null;

    /**
     * 元素入队测试
     */
    @Before
    public void InsertQueue(){
        //初始化队
        queue = new QueueImpl();
        //初始化队长
        queue.initQueue(10);
        Person person1 = Person.builder()
                .id(1)
                .name("zhangsan")
                .age(11)
                .build();
        Person person2 = Person.builder()
                .id(2)
                .name("lisi")
                .age(22)
                .build();
        Person person3 = Person.builder()
                .id(3)
                .name("wangwu")
                .age(33)
                .build();
        //将上述元素添加进队中
        queue.enQueue(person1);
        queue.enQueue(person2);
        queue.enQueue(person3);
    }

    /**
     * 元素出队测试
     */
    @Test
    public void deQueue(){
        //元素出队
        while(!queue.queueEmpty()){
            ObjectHolder objectHolder = new ObjectHolder();
            queue.deQueue(objectHolder);
            Person person = (Person) objectHolder.object;
            log.info(person.toString());
        }
    }

    /**
     * 队列内的元素遍历测试
     */
    @Test
    public void traversQueue(){
        queue.queueTraverse();
    }

}

  QueueTest.deQueue()的测试结果为:

2018-04-08 16:06:48  INFO main com.lyc.linearTable.queue.QueueTest.deQueue(QueueTest.java:54) - Person(id=1, name=zhangsan, age=11)
2018-04-08 16:06:48  INFO main com.lyc.linearTable.queue.QueueTest.deQueue(QueueTest.java:54) - Person(id=2, name=lisi, age=22)
2018-04-08 16:06:48  INFO main com.lyc.linearTable.queue.QueueTest.deQueue(QueueTest.java:54) - Person(id=3, name=wangwu, age=33)

  QueueImpl.queueTraverse()的测试结果为:

2018-04-08 16:07:25  INFO main com.lyc.linearTable.queue.QueueImpl.queueTraverse(QueueImpl.java:119) - Person(id=1, name=zhangsan, age=11)
2018-04-08 16:07:25  INFO main com.lyc.linearTable.queue.QueueImpl.queueTraverse(QueueImpl.java:119) - Person(id=2, name=lisi, age=22)
2018-04-08 16:07:25  INFO main com.lyc.linearTable.queue.QueueImpl.queueTraverse(QueueImpl.java:119) - Person(id=3, name=wangwu, age=33)

猜你喜欢

转载自blog.csdn.net/zzy1078689276/article/details/79853428