博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义线性及非线性存储的队列实现
阅读量:7146 次
发布时间:2019-06-29

本文共 5598 字,大约阅读时间需要 18 分钟。

在平时的编程中,队列可以应用于很多方面。

在生活中我们同样可以随处见到它的身影,比如我们排队,先排的人先得到服务,后进来的人后接受服务。这就是队列。
说白了,就是FIFO原则(First in First out, 先进先出)。
队列的实现是基于存储结构不同而不同的,通常会有两种方式存储。线性存储或者非线行存储。

1、线性存储。是基于数组等长度固定的存储方式来实现的。我这里是先定义了一个接口,然后基于接口来实现的。

下面请看代码,

package simple;interface queue {    boolean isEmpty();    void in(Object value);    Object top();    void out();    boolean isFull();    int getSize();}public class ArrayBase implements queue {
private static final int MAXSIZE = 100; int front; int rear; int number; Object[] data; ArrayBase() { front = rear = -1; number = 0; data = new Object[MAXSIZE]; } @Override public void in(Object value) { // TODO Auto-generated method stub if (this.isFull()) { System.out.println("Sorry, the queue is full!"); } else { this.rear = (this.rear + 1) % MAXSIZE; number++; this.data[this.rear] = value; } } @Override public Object top() { // TODO Auto-generated method stub if (this.isEmpty()) { System.out.println("Sorry, the queue is empty!"); } int p = (this.front + 1) % MAXSIZE; return data[p]; } @Override public void out() { // TODO Auto-generated method stub if (this.isEmpty()) { System.out.println("Sorry, the queue is empty!"); } this.front = (this.front - 1) % MAXSIZE; number--; } @Override public boolean isFull() { // TODO Auto-generated method stub if (this.rear == MAXSIZE - 1) { return true; } return false; } public boolean isEmpty() { if (number == 0) { return true; } return false; } @Override public int getSize() { // TODO Auto-generated method stub return this.number; }}

接下来是测试类:

package simple;public class MyQueueTest {    public static void main(String []args){        ArrayBase myQueue=new ArrayBase();        System.out.println(((ArrayBase) myQueue).isEmpty());        myQueue.in(false);        myQueue.in(1);        myQueue.in('T');        myQueue.in("Biao");        System.out.println("Now there are "+myQueue.getSize()+"  items!");        System.out.println("Top Item is: "+ myQueue.top());        myQueue.out();        System.out.println("After the out operation,there are "                            +myQueue.getSize()+" items in this queue!");    }}

然后就是我们的测试结果了:

trueNow there are 4  items!Top Item is: falseAfter the out operation,there are 3 items in this queue!

2、当然也可以使用非线性的存储结构来实现队列,好处是可以避免“假溢出”的现象,充分的利用到空间。

下面是代码实现:

package simple;interface queueListBase {    boolean isEmpty();    void in(Object value);    Object top();    Object out();    int getSize();}class Node{    Object data;    Node next;    Node(){        data=null;        next=null;    }}public class ListBase implements queueListBase{
int number; Node front; Node rear; public ListBase(){ number=0; front=rear=null; } @Override public boolean isEmpty() { // TODO Auto-generated method stub if(this.front==this.rear){ return true; } return false; } /* * (non-Javadoc)此方法应该尤其要注意的是对初始时的分析 * 我就在此处忘记了front的存在,结果就会导致程序一直抱NullPointerException的错误! * @see simple.queueListBase#in(java.lang.Object) */ @Override public void in(Object value) { // TODO Auto-generated method stub if (rear == null && front == null) { rear = new Node(); rear.data=value; front = rear; } else { Node node = new Node(); node.data=value; rear.next = node; rear = node; number++; } } @Override public Object top() { // TODO Auto-generated method stub Node temp=new Node(); temp.data=this.front.data; return temp; } @Override public Object out() { // TODO Auto-generated method stub Object temp=null; if(this.isEmpty()){ System.out.println("Sorry,the queue is Empty!"); return false; }else{ temp=this.front.data; this.front=this.front.next; if(front.next==null){ this.rear=this.front; } number--; } return temp; } @Override public int getSize() { // TODO Auto-generated method stub return number; }}

下面是我的测试代码:

package simple;public class MyQueueTest {    public static void main(String []args){//      ArrayBase myQueue=new ArrayBase();        ListBase myQueue=new ListBase();        System.out.println(((ListBase) myQueue).isEmpty());        myQueue.in(false);        myQueue.in(1);        myQueue.in('T');        myQueue.in("Biao");        System.out.println(myQueue.out());        System.out.println(myQueue.out());        System.out.println(myQueue.out());        System.out.println(myQueue.out());        System.out.println("Now there are "+myQueue.getSize()+"  items!");        System.out.println("Top Item is: "+ myQueue.top());        myQueue.out();        System.out.println("After the out operation,there are "                            +myQueue.getSize()+" items in this queue!");    }}

下面是测试的结果:

truefalse1TSorry,the queue is Empty!falseNow there are 0  items!Top Item is: simple.Node@659e0bfdSorry,the queue is Empty!After the out operation,there are 0 items in this queue!

结果分析:

不难看出,在链式队列内存储值时,字符串类型并没有成功存储进去。但这并不代表队列无法存储字符串类型的数据,所以我们应该注意这一点!否则将会有可能导致今后软件的出错!下面是一个更加完整的非线性的队列的实现代码,也是我入队操作失败后得到纠正的一篇博客,与君共勉,共同进步吧!

转载地址:http://luzgl.baihongyu.com/

你可能感兴趣的文章
MES之殇和工业IOT之春
查看>>
阿里云网络漏洞扫描系统AVDS(商业化)发布
查看>>
python splinter 小坑说明
查看>>
控制input输入格式
查看>>
一次XEN启动中的错误捕获
查看>>
esxi嵌套华为Fusioncomputer安装VRM几个关键步骤。
查看>>
DNS设置引起的登录延迟
查看>>
saltstack之SLS文件
查看>>
JAVA构建缓存
查看>>
解决:Loading kernel module CAP_SYS_MODULE CAP_NET_ADMIN alias netdev-eth0 instead
查看>>
wav2letter-基于深度学习的语音识别
查看>>
Java class.forname()和newinstance
查看>>
学习计划书
查看>>
[iOS Animation]-CALayer 视觉效果
查看>>
wps的ppt放映时不能完全全屏的解决方法
查看>>
我的友情链接
查看>>
本地存储
查看>>
react-native环境配置入坑指南.
查看>>
使用qemu
查看>>
小试下新博客,一个列传行的SQL
查看>>