java队列java 队列

java队列  时间:2021-09-02  阅读:()

用java实现循环队列?

class Element{ int id; String name; Element(int a,String n){ id=a;name=n; } } class SeqQueue{ int first,last,maxsize; Element queue[]; SeqQueue(int i){ maxsize=i; first=last=-1; queue=new Element[i]; } public void clear(){//置空 first=last=-1; } public boolean isEmpty(){//判空 if(first==-1)return true; else return false; } public Element getFirst(){//取队列头元素 if(first==-1)return null; else return queue[first+1]; } public boolean isFull(){//判满 if((last+1)%maxsize==first)return true; else return false; } public boolean enQueue(Element e){//入队 if(this.isFull())return false; if(this.isEmpty()) first=last=0; else last=(last+1)%maxsize; queue[last]=e; return true; } public Element deQueue(){//出队 Element t=queue[first]; if(this.isEmpty())return null; if(first==last){ queue[first]=null; this.clear(); return t; } queue[first]=null; first=(first+1)%maxsize; return t; } public int getLength(){//队列长度 if(last>=first)return last-first+1; else return maxsize-(first-last)+1; } public void display(){//打印所有元素 int i,j; for (i=first,j=0;j在java中,什么是队列?java中没有队列这个东西吧...队列这个是出现在数据结构里面的吧.. 然后 队列的结构是 先进先出..有队头对尾.数据从尾进...从头读出来 比如 1 2 3 4 5 这么一个队列....那么java读这个数据.是从1的队头开始读.....读到1 把1拿出来..然后队列剩下 2 3 4 5 ....如果要添加进去.那么就是 在对尾 就是5 的后面加一个 如3 ..那么就是 2 3 4 5 3 .这样的结构.. 跟栈不同.栈是先进后出..这个你自己翻书吧..或者网上搜 数据结构 栈..就有了. 补充一下...java是有提供队列的类的.这个我就不说自己学会看api吧.

怎么编写一个简单的java队列?

展开全部 import java.util.*; public class MyQueue { private LinkedList list = new LinkedList(); public void addLast(T v) { list.addLast(v); //队尾插入 } public T getFirst() { return list.getFirst(); //取得队受元素 } public void remove() { list.removeFirst(); //移除队首元素 } //类似功能自己扩展下 public static void main(String[] args) { MyQueue mq = new MyQueue(); mq.addLast("hello world"); mq.addLast("hello world2"); System.out.println(mq.getFirst()); mq.remove(); System.out.println(mq.getFirst()); } }

java 队列

//通过LinkedList实现队列 package 队列和堆栈; import java.util.*; public class LinkedListQueueTest { //字段 private LinkedList list; //无参数构造 public LinkedListQueueTest() { list=new LinkedList(); } //队列元素的个数 public int size() { return list.size(); } //进入队列 public void enqueue(Object obj) { list.addLast(obj); } //对头出来 public Object dequeue() { return list.removeFirst(); } //浏览对头元素 public Object front() { //return list.getFirst(); return list.peekFirst(); } //判断队列是否为空 public boolean isEmpty() { return list.isEmpty(); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub LinkedListQueueTest llq=new LinkedListQueueTest(); System.out.println(llq.isEmpty()); llq.enqueue("147"); llq.enqueue("258"); llq.enqueue("369"); System.out.println(llq.size()); System.out.println("移除队列头元素:"+llq.dequeue()); System.out.println(llq.size()); llq.enqueue("abc"); llq.enqueue("def"); System.out.println(llq.size()); System.out.println("查看队列的头元素:"+llq.front()); System.out.println(llq.size()); System.out.println(llq.isEmpty()); } } 通过数组实现 package 队列和堆栈; import java.util.NoSuchElementException; //通过数组来实现队列 public class ArrayQueue { //字段 public static Object[] data; //队列的元素个数 protected int size ; //队列头 protected int head; //队列尾 public static int tail; /** * */ //无参数构造函数 public ArrayQueue() { final int INITIAL_LENGTH=3; data=new Object[INITIAL_LENGTH]; size=0; head=0; tail=-1; } //队列元素个数方法 public int size() { return size; } public boolean isEmpty() { return size==0; } //得到队列头元素 public Object front() { if(size==0) throw new NoSuchElementException(); return data[head]; } //进入队列enqueue()方法 public void enqueue(Object obj) { //此时队列已经满 if(size==data.length){ Object[] oldData=data; data=new Object[data.length*2]; //if(head==0) System.arraycopy(oldData, head, data, 0, oldData.length-head); if(head>0) System.arraycopy(oldData, 0, data, head+1, tail+1); head=0; tail=oldData.length-1; } tail=(tail+1)%data.length; size++; data[tail]=obj; } //队列的元素出队 public Object dequeue() { if(size==0) throw new NoSuchElementException(); Object ele=data[head]; //循环队列 head=(head+1)%data.length; size--; return ele; } @Override public String toString() { // TODO Auto-generated method stub return super.toString(); } } 通过向量实现: //通过向量实现栈 package 队列和堆栈; import java.util.*; public class VectorStackTest { //字段 Vector v; //构造函数 public VectorStackTest() { v=new Vector(); } //元素的个数 public int size() { return v.size(); } //是否为空 public boolean isEmpty() { return size()==0; } //进栈 public Object Push(Object obj) { v.addElement(obj); return obj; } //出栈方法 public Object Pop() { int len=size(); Object obj=Peek(); v.removeElementAt(len-1); return obj; } //查看栈顶元素 public Object Peek() { int len = size(); if (len == 0) throw new EmptyStackException(); return v.elementAt(len - 1); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub VectorStackTest vst=new VectorStackTest(); System.out.println("大小:"+vst.size()); vst.Push("123"); vst.Push("456"); vst.Push("789"); vst.Push("abc"); System.out.println("大小:"+vst.size()); System.out.println("栈顶:"+vst.Peek()); System.out.println("出栈:"+vst.Pop()); vst.Push("def"); vst.Push("147"); System.out.println("大小:"+vst.size()); System.out.println("栈顶:"+vst.Peek()); System.out.println("出栈:"+vst.Pop()); System.out.println(vst.Peek()); vst.Push("def"); vst.Push("147"); System.out.println(vst.Pop()); System.out.println(vst.Pop()); System.out.println(vst.Peek()); System.out.println(vst.Pop()); System.out.println(vst.Pop()); vst.Push("1aadf"); vst.Push("2dafad"); vst.Push("123789"); System.out.println(vst.Pop()); System.out.println(vst.Peek()); System.out.println(vst.Pop()); System.out.println(vst.Peek()); System.out.println("------------------end------------"); VectorStackTest llst=new VectorStackTest(); llst.Push("123"); llst.Push("456"); System.out.println("栈顶:"+llst.Peek()); System.out.println("出栈:"+llst.Pop()); System.out.println(llst.Peek()); llst.Push("789"); llst.Push("abc"); System.out.println("栈顶:"+llst.Peek()); System.out.println("出栈:"+llst.Pop()); System.out.println(llst.size()); System.out.println("栈顶:"+llst.Peek()); } } 推荐:都看API文档。

有疑问可以问我,QQ:285479197

java 自行编写程序实现队列的效果

class myqueue { ArrayList<Integer> list = new ArrayList<Integer>(); public boolean add(Integer value) { return list.add(value); } public boolean offer(Integer value) { return list.add(value); } /** * function: 获取但不移除对象的头 为空时抛出异常 * @return */ public Integer element() { if(list.size() == 0) throw new NoSuchElementException(); return list.get(0); } /** * function: 获取但不移除对象的头 为空时 返回null * @return */ public Integer peek() { if(list.size() == 0) return null; return list.get(0); } /** * function: 获取并移除对象的头 为空时未null * @return */ public Integer poll() { if(list.size() == 0) return null; Integer i = list.get(0); list.remove(0); return i; } /** * function:获取并移除对象的头 为空时抛出异常 * @return */ public Integer remove() { if(list.size() == 0) throw new NoSuchElementException(); Integer i = list.get(0); list.remove(0); return i; } }

java 队列

/sunzhenxing19860608/archive/2011/02/24/1964283.html 队列是一种重要的数据结构,在排队论和算法设计中有很重要的应用,其实队列也是一种链表,它只允许在表的始端出表(dequeue),在表的末端入表(enqueue),下边是队列的java实现。

//队列是一种重要的数据结构,主要应用是资源的排队(例如打印机),需要注意的是要利用循环数据来存储数据class Queue {private int front;private int back;private int size;private Object[] data;public Queue(){data=new Object[10];}public int getSize(){return size;}public Object dequeue(){Object o=null;if(size>0){size--;o=data[front];data[front]=null;front=(front>data.length-1)?0:front+1;}return o;}} ......

无忧云-河南洛阳BGP,CEPH集群分布式存储,数据安全可靠,活动期间月付大优惠!

 无忧云怎么样?无忧云服务器好不好?无忧云值不值得购买?无忧云是一家成立于2017年的老牌商家旗下的服务器销售品牌,现由深圳市云上无忧网络科技有限公司运营,是正规持证IDC/ISP/IRCS商家,主要销售国内、中国香港、国外服务器产品,线路有腾讯云国外线路、自营香港CN2线路等,都是中国大陆直连线路,非常适合免备案建站业务需求和各种负载较高的项目,同时国内服务器也有多个BGP以及高防节点...

tmhhost:全场VPS低至6.4折,香港BGP200M日本软银美国cn2 gia 200G高防美国三网cn2 gia韩国CN2

tmhhost放出了2021年的端午佳节+618年中大促的优惠活动:日本软银、洛杉矶200G高防cn2 gia、洛杉矶三网cn2 gia、香港200M直连BGP、韩国cn2,全都是高端优化线路,所有这些VPS直接8折,部分已经做了季付8折然后再在此基础上继续8折(也就是6.4折)。 官方网站:https://www.tmhhost.com 香港BGP线路VPS ,200M带宽 200M带...

Budgetvm12核心 16G 500 GB SSD 或者 2 TB SATA 10GB  20 TB  99美金

Budgetvm(原EZ机房),2005年成立的美国老品牌机房,主打美国4个机房(洛杉矶、芝加哥、达拉斯、迈阿密)和日本东京机房的独立服务器和VPS业务,而且不限制流量,默认提供免费的1800G DDoS防御服务,支持IPv6和IPMI,多种免费中文操作系统可供选择,独立服务器主打大硬盘,多硬盘,大内存,用户可以在后台自行安装系统等管理操作!内存可定制升级到1536G,多块硬盘随时加,14TBSA...

java队列为你推荐
kongjianming求空间超长的名字!信件格式书信格式是什么起英文名根据中文名取英文名nvidia官方网站NVIDIA显卡驱动小项目如何搞小工程antiarp360防火墙:antiarp.exe文件损坏.运行chkdsk是什么意思?怎么处理?jsp源码在网上下的jsp源码怎么运行?有数据库的活跃网络移动大V网是什么意思?0x800ccc0f您的服务器意外终止了连接。其可能原因包括服务器出错、网络出错或长时间处于非活动状态。 0x800CCC0F快照优化快照跟不上优化节奏优化方法出问题?
国内ip代理 域名备案 提供香港vps 云网数据 hostmaster 美国主机推荐 sugarsync 好看的留言 本网站服务器在美国 宁波服务器 怎么测试下载速度 服务器维护方案 网站木马检测工具 域名评估 1g内存 isp服务商 免费申请网站 超级服务器 联通网站 湖南idc 更多