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

Tudcloud(月付7.2美元),香港VPS,可选大带宽或不限流量

Tudcloud是一家新开的主机商,提供VPS和独立服务器租用,数据中心在中国香港(VPS和独立服务器)和美国洛杉矶(独立服务器),商家VPS基于KVM架构,开设在香港机房,可以选择限制流量大带宽或者限制带宽不限流量套餐。目前提供8折优惠码,优惠后最低每月7.2美元起。虽然主机商网站为英文界面,但是支付方式仅支付宝和Stripe,可能是国人商家。下面列出部分VPS主机套餐配置信息。CPU:1cor...

ftlcloud9元/月,美国云服务器,1G内存/1核/20g硬盘/10M带宽不限/10G防御

ftlcloud(超云)目前正在搞暑假促销,美国圣何塞数据中心的云服务器低至9元/月,系统盘与数据盘分离,支持Windows和Linux,免费防御CC攻击,自带10Gbps的DDoS防御。FTL-超云服务器的主要特色:稳定、安全、弹性、高性能的云端计算服务,快速部署,并且可根据业务需要扩展计算能力,按需付费,节约成本,提高资源的有效利用率。活动地址:https://www.ftlcloud.com...

ZJI-全场八折优惠,香港服务器 600元起,还有日本/美国/韩国服务器

月付/年付优惠码:zji  下物理服务器/VDS/虚拟主机空间订单八折终身优惠(长期有效)一、ZJI官网点击直达ZJI官方网站二、特惠香港日本服务器香港大埔:http://hkdb.speedtest.zji.net/香港葵湾:http://hkkw.speedtest.zji.net/日本大阪:http://jpsk.speedtest.zji.net/日本大阪一型 ...

java队列为你推荐
推信现在大二怎样准备北大金融研究生eofexceptionjava中dis.readutf报错java.io.EOFException全球随机视频网全球随机视频网的发展方向摇一摇周边公众号怎么用微信摇一摇周边功能soap是什么意思rbq是什么意思?相册网怎样才能把我的照片传到网上去??主板说明书精英主板中文说明书海淀区公司注册在北京海淀区注册的有限责任公司,要增加自然人股东,需要准备哪些材料?安全工程师待遇注册安全工程师待遇怎样?分销渠道案例企业分销渠道成功的案例分析
cn域名 云南服务器租用 vps代购 什么是域名地址 zpanel 免费ftp空间申请 panel1 最好的空间 我爱水煮鱼 速度云 免费活动 息壤代理 天翼云盘 免费网页申请 多线空间 优酷黄金会员账号共享 环聊 路由跟踪 服务器论坛 lamp兄弟连 更多