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,至强铂金8270,提供自助防火墙管理,超大内存满足你各种需求

官方网站:点击访问亚洲云官网618活动方案:618特价活动(6.18-6.30)全站首月活动月底结束!地区:浙江高防BGPCPU:至强铂金8270主频7 默频3.61 睿频4.0核心:8核(最高支持64核)内存:8G(最高支持128G)DDR4 3200硬盘:40G系统盘+80G数据盘带宽:上行:20Mbps/下行:1000Mbps防御:100G(可加至300G)防火墙:提供自助 天机盾+金盾 管...

轻云互联(19元)香港高防云服务器 ,美国云服务器

轻云互联成立于2018年的国人商家,广州轻云互联网络科技有限公司旗下品牌,主要从事VPS、虚拟主机等云计算产品业务,适合建站、新手上车的值得选择,香港三网直连(电信CN2GIA联通移动CN2直连);美国圣何塞(回程三网CN2GIA)线路,所有产品均采用KVM虚拟技术架构,高效售后保障,稳定多年,高性能可用,网络优质,为您的业务保驾护航。活动规则:用户购买任意全区域云服务器月付以上享受免费更换IP服...

腾讯云2核4GB内存8M带宽 年74元

一般大厂都是通过首年才有可以享受爆款活动,然后吸引我们注册他们商家达到持续续费和购买的目的。一般只有大厂才能有这样的魄力和能力首年亏本,但是对于一般的公司和个人厂家确实难过,这几年确实看到不少的同类商家难以生存。这里我们可以看到有对应的套餐方案。不过这两个套餐都是100%CPU独享的,不是有某云商家限制CPU的。但是轻量服务器有个不好的就是带宽是较大且流量是限制的额,分别是1GB和1.2TB月流量...

java队列为你推荐
教育城域网关于学校局域网文件下载小项目奥运会一共有几个大项目小项目?光纤是什么什么是光纤.是什么材料做的?soap是什么意思肥皂剧是什么意思?相册网知不知道怎么把一个人的照片放到网上去?java变量设置java的环境变量设置约束是什么意思cad软件里“推断约束是什么意思”分销渠道案例王老吉分销渠道案例分析警告本网站内容我的网页一打开,就出安全警告,内容是:当前网页正试图打开您的受信任列表中站点。 该怎么去掉啊????fshow瑜伽有什么好处,快三十的人啦,练瑜伽可以吗
过期域名查询 过期已备案域名 budgetvm 本网站服务器在美国维护 樊云 idc测评网 seovip 2017年黑色星期五 申请个人网页 asp免费空间申请 hinet 免费美国空间 空间技术网 卡巴斯基是免费的吗 吉林铁通 常州联通宽带 in域名 新睿云 闪讯官网 创建邮箱 更多