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

A400互联(49元/月)洛杉矶CN2 GIA+BGP、1Gbps带宽,全场独服永久5折优惠

a400互联是一家成立于2020年商家,主营美国机房的产品,包括BGP线路、CN2 GIA线路的云服务器、独立服务器、高防服务器,接入线路优质,延迟低,稳定性高,额外也还有香港云服务器业务。当前,全场服务器5折,香港VPS7折,洛杉矶VPS5折,限时促销!A400互联官网:https://a400.net/优惠活动全场独服永久5折优惠(续费同价):0722香港VPS七折优惠:0711洛杉矶VPS五...

旅途云(¥48 / 月),雅安高防4核4G、洛阳BGP 2核2G

公司成立于2007年,是国内领先的互联网业务平台服务提供商。公司专注为用户提供低价高性能云计算产品,致力于云计算应用的易用性开发,并引导云计算在国内普及。目前,旅途云公司研发以及运营云服务基础设施服务平台(IaaS),面向全球客户提供基于云计算的IT解决方案与客户服务,拥有丰富的国内BGP、双线高防、香港等优质的IDC资源。点击进入:旅途云官方网商家LOGO优惠方案:CPU内存硬盘带宽/流量/防御...

HostYun 新上美国CN2 GIA VPS 月15元

HostYun 商家以前是玩具主机商,这两年好像发展还挺迅速的,有点在要做点事情的味道。在前面也有多次介绍到HostYun商家新增的多款机房方案,价格相对还是比较便宜的。到目前为止,我们可以看到商家提供的VPS主机包括KVM和XEN架构,数据中心可选日本、韩国、香港和美国的多个地区机房,电信双程CN2 GIA线路,香港和日本机房,均为国内直连线路。近期,HostYun上线低价版美国CN2 GIA ...

java队列为你推荐
融360融360贷款是假的是骗人的,大家注意了阿里校园招聘阿里巴巴校园招聘结束后还能继续面试实习生吗?文件下载在电脑上下载文件怎么下载项目质量管理项目质量管理的名词解释局域网监控软件求一个破解版局域网监控软件路由器映射路由器映射设置硬盘分区格式化硬盘分区后怎么格式化网络购物的发展网购发展史黑屏操作常见黑屏故障的处理方法有哪些呢?gps简介GPS是什么
二级域名申请 最便宜虚拟主机 互联网域名管理办法 阿里云搜索 冰山互联 raksmart wordpress技巧 ssh帐号 网站实时监控 权嘉云 php空间推荐 股票老左 hinet 中国电信测网速 如何用qq邮箱发邮件 ftp免费空间 广东主机托管 国外免费云空间 广东服务器托管 服务器托管价格 更多