本文目錄一覽:
java 用什麼實現 FIFO隊列?
java使用數據結構來實現FIFO先進先出的隊列,實例如下:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package linkedlisttest;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
/**
*
* @author Vicky.H
* @email eclipser@163.com
*/
public class FIFOTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
FIFOA fifo = new FIFOImplA(5);
for (int i = 0; i 20; i++) {
A a = new A(“A:” + i);
A head = fifo.addLastSafe(a);
System.out.println(i + “\thead:” + head + “\tsize:” + fifo.size());
}
System.out.println(“—————“);
System.out.println(“彈出數據”);
ListA polls = fifo.setMaxSize(3);
for (A a : polls) {
System.out.println(“\thead:” + a);
}
System.out.println(“剩餘數據”);
for (A a : fifo) {
System.out.println(“\thead:” + a);
}
System.out.println(fifo.size());
}
}
interface FIFOT extends ListT, DequeT, Cloneable, java.io.Serializable {
/**
* 向最後添加一個新的,如果長度超過允許的最大值,則彈出一個 *
*/
T addLastSafe(T addLast);
/**
* 彈出head,如果Size = 0返回null。而不同於pop拋出異常
* @return
*/
T pollSafe();
/**
* 獲得最大保存
*
* @return
*/
int getMaxSize();
/**
* 設置最大存儲範圍
*
* @return 返回的是,因為改變了隊列大小,導致彈出的head
*/
ListT setMaxSize(int maxSize);
}
class FIFOImplT extends LinkedListT implements FIFOT {
private int maxSize = Integer.MAX_VALUE;
private final Object synObj = new Object();
public FIFOImpl() {
super();
}
public FIFOImpl(int maxSize) {
super();
this.maxSize = maxSize;
}
@Override
public T addLastSafe(T addLast) {
synchronized (synObj) {
T head = null;
while (size() = maxSize) {
head = poll();
}
addLast(addLast);
return head;
}
}
@Override
public T pollSafe() {
synchronized (synObj) {
return poll();
}
}
@Override
public ListT setMaxSize(int maxSize) {
ListT list = null;
if (maxSize this.maxSize) {
list = new ArrayListT();
synchronized (synObj) {
while (size() maxSize) {
list.add(poll());
}
}
}
this.maxSize = maxSize;
return list;
}
@Override
public int getMaxSize() {
return this.maxSize;
}
}
class A {
private String name;
public A() {
}
public A(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return “A{” + “name=” + name + ‘}’;
}
}
java中怎麼實現隊列
public class QueueE {
private Object[] data=null;
private int maxSize; //隊列容量
private int front; //隊列頭,允許刪除
private int rear; //隊列尾,允許插入
//構造函數
public Queue(){
this(10);
}
public Queue(int initialSize){
if(initialSize =0){
this.maxSize = initialSize;
data = new Object[initialSize];
front = rear =0;
}else{
throw new RuntimeException(“初始化大小不能小於0:” + initialSize);
}
}
//判空
public boolean empty(){
return rear==front?true:false;
}
//插入
public boolean add(E e){
if(rear== maxSize){
throw new RuntimeException(“隊列已滿,無法插入新的元素!”);
}else{
data[rear++]=e;
return true;
}
}
//返回隊首元素,但不刪除
public E peek(){
if(empty()){
throw new RuntimeException(“空隊列異常!”);
}else{
return (E) data[front];
}
}
//出隊
public E poll(){
if(empty()){
throw new RuntimeException(“空隊列異常!”);
}else{
E value = (E) data[front]; //保留隊列的front端的元素的值
data[front++] = null; //釋放隊列的front端的元素
return value;
}
}
//隊列長度
public int length(){
return rear-front;
}
}
用java編一個隊列
自己寫了個簡單的實現
class QueueE{
private Object[] integerQueue;//用來當隊列
public int tail;//隊尾
public int size;//隊的長度,也可以設置一個默認值,溢出時從新申請
public Queue(int size){
integerQueue=new Object[size];
this.size=size;
tail=-1;
}
/**
* 將元素插入隊列
* @return 如果該元素已添加到此隊列,則返回 true;否則返回 false
*/
public boolean offer(E e){
if(tail size-1){
tail++;
this.integerQueue[tail]=e;
return true;
}else{
return false;
}
}
/**
* 獲取並移除此隊列的頭,如果此隊列為空,則返回 null。
*/
public E poll(){
Object tmp;
if(tail=0){
tmp=this.integerQueue[tail];
tail–;
return (E)tmp;
}else{
return null;
}
}
}
java中的隊列用什麼實現
隊列的實現單純的是數據結構的問題,既可以用鏈表結構實現隊列,也可以用數組實現。這和語言不是緊密關係,java可以這樣實現,C、C++ 也可以。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/198638.html