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;}} ......

friendhosting:(优惠55%)大促销,全场VPS降价55%,9个机房,不限流量

每年的7月的最后一个周五是全球性质的“系统管理员日”,据说是为了感谢系统管理员的辛苦工作....friendhosting决定从现在开始一直到9月8日对其全球9个数据中心的VPS进行4.5折(优惠55%)大促销。所有VPS基于KVM虚拟,给100M带宽,不限制流量,允许自定义上传ISO...官方网站:https://friendhosting.net比特币、信用卡、PayPal、支付宝、微信、we...

819云互联 香港 日本 美国 2核4G 18元 8核8G 39元 免费空间 免费CDN 香港 E3 16G 20M 230元/月

819云互联是海外领先的互联网业务平台服务提供商。专注为用户提供低价高性能云计算产品,致力于云计算应用的易用性开发,并引导云计算在国内普及。目前平台研发以及运营云服务基础设施服务平台(IaaS),面向全球客户提供基于云计算的IT解决方案与客户服务,拥有丰富的海外资源、香港,日本,美国等各国优质的IDC资源。官方网站:https://www.819yun.com香港特价物理服务器:地区CPU内存带宽...

RAKsmart(年79元),云服务器年付套餐汇总 - 香港 美国 日本云服务器

RAKsmart 商家从原本只有专注于独立服务器后看到产品线比较单薄,后来陆续有增加站群服务器、高防服务器、VPS主机,以及现在也有在新增云服务器、裸机云服务器等等。机房也有增加到拥有洛杉矶、圣何塞、日本、韩国、中国香港等多个机房。在年前也有介绍到RAKsmart商家有提供年付129元的云服务器套餐,年后我们看到居然再次刷新年付云服务器低价格。我们看到云服务器低至年79元,如果有需要便宜云服务器的...

java队列为你推荐
java队列怎样用java代码实现一个队列eofexceptionjava中dis.readutf报错java.io.EOFException容灾备份容灾备份的容灾分类nvidia官方网站怎么下载英伟达显卡驱动官方nvidia官方网站N卡的官网是什么?小项目如何搞小工程售后软件有没有什么软件可以接单手机维修互动电视互动电视和有线电视的数字电视有什么区别电商网站设计电商网站设计需要注意哪些海淀区公司注册北京海淀培训公司注册如何办理?
太原域名注册 域名查询工具 域名备案批量查询 免费cn域名 高防dns enom host1plus godaddy主机 服务器日志分析 http500内部服务器错误 建站代码 韩国网名大全 警告本网站美国保护 老左正传 域名和空间 cn3 美国在线代理服务器 昆明蜗牛家 双线机房 1元域名 更多