博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA数据结构--队列
阅读量:6977 次
发布时间:2019-06-27

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

JAVA中的队列实现:

1 // Queue.java 2 // demonstrates queue 3 // to run this program: C>java QueueApp 4  5 class Queue 6    { 7    private int maxSize; 8    private long[] queArray; 9    private int front;10    private int rear;11    private int nItems;12 //--------------------------------------------------------------13    public Queue(int s)          // constructor14       {15       maxSize = s;16       queArray = new long[maxSize];17       front = 0;18       rear = -1;19       nItems = 0;20       }21 //--------------------------------------------------------------22    public void insert(long j)   // put item at rear of queue23       {24       if(rear == maxSize-1)         // deal with wraparound25          rear = -1;26       queArray[++rear] = j;         // increment rear and insert27       nItems++;                     // one more item28       }29 //--------------------------------------------------------------30    public long remove()         // take item from front of queue31       {32       long temp = queArray[front++]; // get value and incr front33       if(front == maxSize)           // deal with wraparound34          front = 0;35       nItems--;                      // one less item36       return temp;37       }38 //--------------------------------------------------------------39    public long peekFront()      // peek at front of queue40       {41       return queArray[front];42       }43 //--------------------------------------------------------------44    public boolean isEmpty()    // true if queue is empty45       {46       return (nItems==0);47       }48 //--------------------------------------------------------------49    public boolean isFull()     // true if queue is full50       {51       return (nItems==maxSize);52       }53 //--------------------------------------------------------------54    public int size()           // number of items in queue55       {56       return nItems;57       }58 //--------------------------------------------------------------59    }  // end class Queue60 61 class QueueApp62    {63    public static void main(String[] args)64       {65       Queue theQueue = new Queue(5);  // queue holds 5 items66 67       theQueue.insert(10);            // insert 4 items68       theQueue.insert(20);69       theQueue.insert(30);70       theQueue.insert(40);71 72       theQueue.remove();              // remove 3 items73       theQueue.remove();              //    (10, 20, 30)74       theQueue.remove();75 76       theQueue.insert(50);            // insert 4 more items77       theQueue.insert(60);            //    (wraps around)78       theQueue.insert(70);79       theQueue.insert(80);80 81       while( !theQueue.isEmpty() )    // remove and display82          {                            //    all items83          long n = theQueue.remove();  // (40, 50, 60, 70, 80)84          System.out.print(n);85          System.out.print(" ");86          }87       System.out.println("");88       }  // end main()89    }  // end class QueueApp90

 

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

你可能感兴趣的文章
CentOS6.3编译安装Nginx1.4.7 + MySQL5.5.25a + PHP5.3.28
查看>>
PHP下载/采集远程图片到本地
查看>>
关于PHP程序员技术职业生涯规划
查看>>
python requests返回的json对象用json.loads()时转为字典时编码变为了unicode
查看>>
各大浏览器 CSS3 和 HTML5 兼容速查表
查看>>
使用可信证书为windows RDP服务提供加密
查看>>
报错 org.springframework.beans.factory.BeanCreationException
查看>>
IOS分享扩展使用JS脚本
查看>>
Hibernate 的 session.load()使用方法
查看>>
$httpprovider指令中拦截器interceptors的使用介绍
查看>>
gulp插件之browser-sync安装报错
查看>>
mongodb 连接和备份
查看>>
Velocity文档(3)
查看>>
SQL中的case when then else end用法
查看>>
通过data:image/png;base64把图片直接写在src里
查看>>
Android TextView的一些小知识
查看>>
css :after或:before写小三角形
查看>>
VMware Tools手动下载
查看>>
干货:排名前 16 的 Java 工具类!
查看>>
Solr 4.x定时、实时增量索引 - 修改、删除和新增索引
查看>>