亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java隊列實(shí)現(xiàn)方法(順序隊列,鏈?zhǔn)疥犃?循環(huán)隊列)

 更新時間:2017年12月18日 15:00:52   作者:jiutianhe  
下面小編就為大家分享一篇java隊列實(shí)現(xiàn)方法(順序隊列,鏈?zhǔn)疥犃?循環(huán)隊列),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

雙向順序隊列ArrayDeque和雙向鏈?zhǔn)疥犃蠰inkedList,JDK已經(jīng)包含,在此略。ArrayDeque包括順序棧和順序隊列,LinkedList包含鏈?zhǔn)綏:玩準(zhǔn)疥犃?。ArrayDeque和LinkedList都是線程不安全的。PriorityQueue優(yōu)先隊列也在JDK。

1.順序隊列的實(shí)現(xiàn)

package lang;
import java.io.Serializable;
import java.util.Arrays;
/**
 * @ClassName: ArrayQueue
 * @Description: 順序隊列
 * @date 2014年1月20日 下午3:46:19
 * @param <T>
 */
public class ArrayQueue<T> implements Serializable{
 /**
  * @Fields serialVersionUID : TODO
  */
 private static final long serialVersionUID = 7333344126529379197L;
 private int DEFAULT_SIZE = 10;
 private int capacity;//保存數(shù)組的長度
 private Object[] elementData;//定義一個數(shù)組用于保存順序隊列的元素
 private int front = 0;//隊頭
 private int rear = 0;//隊尾
 //以默認(rèn)數(shù)組長度創(chuàng)建空順序隊列
 public ArrayQueue() {
  capacity = DEFAULT_SIZE;
  elementData = new Object[capacity];
 }
 //以一個初始化元素來創(chuàng)建順序隊列
 public ArrayQueue(T element) {
  this();
  elementData[0] = element;
  rear++;
 }
 
 public ArrayQueue(int initSize) {
  elementData = new Object[initSize];
 }
 /**
  * 以指定長度的數(shù)組來創(chuàng)建順序隊列
  * @param element 指定順序隊列中第一個元素
  * @param initSize 指定順序隊列底層數(shù)組的長度
  */
 public ArrayQueue(T element, int initSize) {
  this.capacity = initSize;
  elementData = new Object[capacity];
  elementData[0] = element;
  rear++;
 }
 /**
  * @Title: size   
  * @Description: 獲取順序隊列的大小  
  * @return
  */
 public int size() {
  return rear - front;
 }
 /**
  * @Title: offer   
  * @Description: 入隊  
  * @param element
  */
 public void offer(T element) {
  ensureCapacity(rear + 1);
  elementData[rear++] = element;
 }
 
 private void ensureCapacity(int minCapacity) {
  //如果數(shù)組的原有長度小于目前所需的長度
  int oldCapacity = elementData.length;
  if (minCapacity > oldCapacity) {
   int newCapacity = (oldCapacity * 3) / 2 + 1;
   if (newCapacity < minCapacity)
    newCapacity = minCapacity;
   // minCapacity is usually close to size, so this is a win:
   elementData = Arrays.copyOf(elementData, newCapacity);
  }
 }
 /**
  * @Title: poll   
  * @Description: 出隊  
  * @return
  */
 public T poll() {
  if (isEmpty()) {
   throw new IndexOutOfBoundsException("空隊列異常");
  }
  //保留隊列的front端的元素的值
  T oldValue = (T) elementData[front];
  //釋放隊列的front端的元素
  elementData[front++] = null;
  return oldValue;
 }
 /**
  * @Title: peek   
  * @Description: 返回隊列頂元素,但不刪除隊列頂元素  
  * @return
  */
 public T peek() {
  if (isEmpty()) {
   throw new IndexOutOfBoundsException("空隊列異常");
  }
  return (T) elementData[front];
 }
 /**
  * @Title: isEmpty   
  * @Description: 判斷順序隊列是否為空隊列  
  * @return
  */
 public boolean isEmpty() {
  return rear == front;
 }
 /**
  * @Title: clear   
  * @Description: 清空順序隊列
  */
 public void clear() {
  //將底層數(shù)組所有元素賦為null
  Arrays.fill(elementData, null);
  front = 0;
  rear = 0;
 }
 public String toString() {
  if (isEmpty()) {
   return "[]";
  } else {
   StringBuilder sb = new StringBuilder("[");
   for (int i = front; i < rear; i++) {
    sb.append(elementData[i].toString() + ", ");
   }
   int len = sb.length();
   return sb.delete(len - 2, len).append("]").toString();
  }
 }
}

2. 鏈?zhǔn)疥犃械膶?shí)現(xiàn)

package lang;
import java.io.Serializable;
/**
 * @ClassName: LinkQueue
 * @Description: 鏈?zhǔn)疥犃?
 * @date 2014年1月21日 下午3:24:38
 * @param <T>
 */
public class LinkQueue<T> implements Serializable{
 /**
  * @Fields serialVersionUID : TODO
  */
 private static final long serialVersionUID = -6726728595616312615L;
 //定義一個內(nèi)部類Node,Node實(shí)例代表鏈隊列的節(jié)點(diǎn)。
 private class Node {
  
  private T data;//保存節(jié)點(diǎn)的數(shù)據(jù)
  
  private Node next;//指向下個節(jié)點(diǎn)的引用
  //無參數(shù)的構(gòu)造器
  public Node() {
  }
  //初始化全部屬性的構(gòu)造器
  public Node(T data, Node next) {
   this.data = data;
   this.next = next;
  }
 }
 
 private Node front;//保存該鏈隊列的頭節(jié)點(diǎn)
 
 private Node rear;//保存該鏈隊列的尾節(jié)點(diǎn)
 private int size;//保存該鏈隊列中已包含的節(jié)點(diǎn)數(shù)
 /**
  * <p>Title: LinkQueue </p>   
  * <p>Description: 創(chuàng)建空鏈隊列 </p> 
  */
 public LinkQueue() {
  //空鏈隊列,front和rear都是null
  front = null;
  rear = null;
 }
 /**
  * <p>Title: LinkQueue </p>  
  * <p>Description: 以指定數(shù)據(jù)元素來創(chuàng)建鏈隊列,該鏈隊列只有一個元素</p> 
  */
 public LinkQueue(T element) {
  front = new Node(element, null);
  //只有一個節(jié)點(diǎn),front、rear都指向該節(jié)點(diǎn)
  rear = front;
  size++;
 }
 /**
  * @Title: size   
  * @Description: 獲取順序隊列的大小  
  * @return
  */
 public int size() {
  return size;
 }
 /**
  * @Title: offer   
  * @Description: 入隊  
  * @param element
  */
 public void offer(T element) {
  //如果該鏈隊列還是空鏈隊列
  if (front == null) {
   front = new Node(element, null);   
   rear = front;//只有一個節(jié)點(diǎn),front、rear都指向該節(jié)點(diǎn)
  } else {   
   Node newNode = new Node(element, null);//創(chuàng)建新節(jié)點(diǎn)   
   rear.next = newNode;//讓尾節(jié)點(diǎn)的next指向新增的節(jié)點(diǎn)   
   rear = newNode;//以新節(jié)點(diǎn)作為新的尾節(jié)點(diǎn)
  }
  size++;
 }
 /**
  * @Title: poll   
  * @Description: 出隊  
  * @return
  */
 public T poll() {
  Node oldFront = front;
  front = front.next;
  oldFront.next = null;
  size--;
  return oldFront.data;
 }
 /**
  * @Title: peek   
  * @Description: 返回隊列頂元素,但不刪除隊列頂元素  
  * @return
  */
 public T peek() {
  return rear.data;
 }
 /**
  * @Title: isEmpty   
  * @Description: 判斷順序隊列是否為空隊列  
  * @return
  */
 public boolean isEmpty() {
  return size == 0;
 }
 /**
  * @Title: clear   
  * @Description: 清空順序隊列
  */
 public void clear() {
  //將front、rear兩個節(jié)點(diǎn)賦為null
  front = null;
  rear = null;
  size = 0;
 }
 public String toString() {
  //鏈隊列為空鏈隊列時
  if (isEmpty()) {
   return "[]";
  } else {
   StringBuilder sb = new StringBuilder("[");
   for (Node current = front; current != null; current = current.next) {
    sb.append(current.data.toString() + ", ");
   }
   int len = sb.length();
   return sb.delete(len - 2, len).append("]").toString();
  }
 }
 public static void main(String[] args) {
  LinkQueue<String> queue = new LinkQueue<String>("aaaa");
  //添加兩個元素
  queue.offer("bbbb");
  queue.offer("cccc");
  System.out.println(queue);
  //刪除一個元素后
  queue.poll();
  System.out.println("刪除一個元素后的隊列:" + queue);
  //再次添加一個元素
  queue.offer("dddd");
  System.out.println("再次添加元素后的隊列:" + queue);
  //刪除一個元素后,隊列可以再多加一個元素
  queue.poll();
  //再次加入一個元素
  queue.offer("eeee");
  System.out.println(queue);
 }
}

3. 循環(huán)隊列的實(shí)現(xiàn)

package lang;
import java.io.Serializable;
import java.util.Arrays;
/**
 * @ClassName: LoopQueue
 * @Description: 循環(huán)隊列
 * @date 2014年1月20日 下午3:47:14
 */
public class LoopQueue<T> implements Serializable{
 /**
  * @Fields serialVersionUID : TODO
  */
 private static final long serialVersionUID = -3670496550272478781L;
 private int DEFAULT_SIZE = 10;
 private int capacity;//保存數(shù)組的長度
 private Object[] elementData;//定義一個數(shù)組用于保存循環(huán)隊列的元素
 private int front = 0;//隊頭
 private int rear = 0;//隊尾
 //以默認(rèn)數(shù)組長度創(chuàng)建空循環(huán)隊列
 public LoopQueue() {
  capacity = DEFAULT_SIZE;
  elementData = new Object[capacity];
 }
 //以一個初始化元素來創(chuàng)建循環(huán)隊列
 public LoopQueue(T element) {
  this();
  elementData[0] = element;
  rear++;
 }
 /**
  * 以指定長度的數(shù)組來創(chuàng)建循環(huán)隊列
  * @param element 指定循環(huán)隊列中第一個元素
  * @param initSize 指定循環(huán)隊列底層數(shù)組的長度
  */
 public LoopQueue(T element, int initSize) {
  this.capacity = initSize;
  elementData = new Object[capacity];
  elementData[0] = element;
  rear++;
 }
 //獲取循環(huán)隊列的大小
 public int size() {
  if (isEmpty()) {
   return 0;
  }
  return rear > front ? rear - front : capacity - (front - rear);
 }
 //插入隊列
 public void add(T element) {
  if (rear == front && elementData[front] != null) {
   throw new IndexOutOfBoundsException("隊列已滿的異常");
  }
  elementData[rear++] = element;
  //如果rear已經(jīng)到頭,那就轉(zhuǎn)頭
  rear = rear == capacity ? 0 : rear;
 }
 //移除隊列
 public T remove() {
  if (isEmpty()) {
   throw new IndexOutOfBoundsException("空隊列異常");
  }
  //保留隊列的rear端的元素的值
  T oldValue = (T) elementData[front];
  //釋放隊列的rear端的元素
  elementData[front++] = null;
  //如果front已經(jīng)到頭,那就轉(zhuǎn)頭
  front = front == capacity ? 0 : front;
  return oldValue;
 }
 //返回隊列頂元素,但不刪除隊列頂元素
 public T element() {
  if (isEmpty()) {
   throw new IndexOutOfBoundsException("空隊列異常");
  }
  return (T) elementData[front];
 }
 //判斷循環(huán)隊列是否為空隊列
 public boolean isEmpty() {
  //rear==front且rear處的元素為null
  return rear == front && elementData[rear] == null;
 }
 //清空循環(huán)隊列
 public void clear() {
  //將底層數(shù)組所有元素賦為null
  Arrays.fill(elementData, null);
  front = 0;
  rear = 0;
 }
 public String toString() {
  if (isEmpty()) {
   return "[]";
  } else {
   //如果front < rear,有效元素就是front到rear之間的元素
   if (front < rear) {
    StringBuilder sb = new StringBuilder("[");
    for (int i = front; i < rear; i++) {
     sb.append(elementData[i].toString() + ", ");
    }
    int len = sb.length();
    return sb.delete(len - 2, len).append("]").toString();
   }
   //如果front >= rear,有效元素為front->capacity之間、0->front之間的
   else {
    StringBuilder sb = new StringBuilder("[");
    for (int i = front; i < capacity; i++) {
     sb.append(elementData[i].toString() + ", ");
    }
    for (int i = 0; i < rear; i++) {
     sb.append(elementData[i].toString() + ", ");
    }
    int len = sb.length();
    return sb.delete(len - 2, len).append("]").toString();
   }
  }
 }
 public static void main(String[] args) {
  LoopQueue<String> queue = new LoopQueue<String>("aaaa", 3);
  //添加兩個元素
  queue.add("bbbb");
  queue.add("cccc");
  //此時隊列已滿
  System.out.println(queue);
  //刪除一個元素后,隊列可以再多加一個元素
  queue.remove();
  System.out.println("刪除一個元素后的隊列:" + queue);
  //再次添加一個元素,此時隊列又滿
  queue.add("dddd");
  System.out.println(queue);
  System.out.println("隊列滿時的長度:" + queue.size());
  //刪除一個元素后,隊列可以再多加一個元素
  queue.remove();
  //再次加入一個元素,此時隊列又滿
  queue.add("eeee");
  System.out.println(queue);
 }
}

以上這篇java隊列實(shí)現(xiàn)方法(順序隊列,鏈?zhǔn)疥犃?循環(huán)隊列)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 通過Java實(shí)現(xiàn)zip文件與rar文件解壓縮的詳細(xì)步驟

    通過Java實(shí)現(xiàn)zip文件與rar文件解壓縮的詳細(xì)步驟

    這篇文章主要給大家介紹了如何通過?Java?來完成?zip?文件與?rar?文件的解壓縮,文中通過代碼示例講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-07-07
  • SpringBoot中實(shí)現(xiàn)接收文件和對象

    SpringBoot中實(shí)現(xiàn)接收文件和對象

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)接收文件和對象,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • SpringBoot異步處理的四種實(shí)現(xiàn)方式

    SpringBoot異步處理的四種實(shí)現(xiàn)方式

    本篇文章我們以SpringBoot中異步的使用(包括:異步調(diào)用和異步方法兩個維度)來進(jìn)行講解,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • IDEA配置Maven的超詳細(xì)步驟

    IDEA配置Maven的超詳細(xì)步驟

    Maven是一個能使我們的java程序開發(fā)節(jié)省時間和精力,是開發(fā)變得相對簡單,還能使開發(fā)規(guī)范化的工具,下面這篇文章主要給大家介紹了關(guān)于IDEA配置Maven的超詳細(xì)步驟,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • 詳解MyBatis延遲加載是如何實(shí)現(xiàn)的

    詳解MyBatis延遲加載是如何實(shí)現(xiàn)的

    MyBatis 的延遲加載(懶加載)特性允許在需要使用關(guān)聯(lián)對象數(shù)據(jù)時才進(jìn)行加載,而不是在執(zhí)行主查詢時就加載所有相關(guān)數(shù)據(jù),我們將通過以下幾個方面來深入了解MyBatis的延遲加載實(shí)現(xiàn)機(jī)制,需要的朋友可以參考下
    2024-07-07
  • Java實(shí)現(xiàn)小程序簡單五子棋

    Java實(shí)現(xiàn)小程序簡單五子棋

    這篇文章主要介紹了利用Java實(shí)現(xiàn)小程序簡單五子棋,本程序適用于java初學(xué)者鞏固類與對象、事件響應(yīng)、awt包中各種工具的相關(guān)概念以及對邏輯能力的鍛煉,下面來看具體實(shí)現(xiàn)吧
    2021-12-12
  • 通過實(shí)例解析spring bean之間的關(guān)系

    通過實(shí)例解析spring bean之間的關(guān)系

    這篇文章主要介紹了通過實(shí)例解析spring bean之間的關(guān)系,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • spring boot系列之集成測試(推薦)

    spring boot系列之集成測試(推薦)

    這篇文章主要介紹了spring boot系列集成測試,需要的朋友可以參考下
    2018-03-03
  • 用攔截器修改返回response,對特定的返回進(jìn)行修改操作

    用攔截器修改返回response,對特定的返回進(jìn)行修改操作

    這篇文章主要介紹了用攔截器修改返回response,對特定的返回進(jìn)行修改操作。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Springboot整合PageOffice 實(shí)現(xiàn)word在線編輯保存功能

    Springboot整合PageOffice 實(shí)現(xiàn)word在線編輯保存功能

    這篇文章主要介紹了Springboot整合PageOffice 實(shí)現(xiàn)word在線編輯保存,本文以Samples5 為示例文件結(jié)合示例代碼給大家詳細(xì)介紹,需要的朋友可以參考下
    2021-08-08

最新評論