日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php环行队列实现,java数组实现队列及环形队列实现过程解析

發布時間:2025/3/15 php 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php环行队列实现,java数组实现队列及环形队列实现过程解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這篇文章主要介紹了java數組實現隊列及環形隊列實現過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

代碼內容

ArrayQueue---用數組實現隊列

package com.structure;

import java.util.Scanner;

/**

* @auther::9527

* @Description: 數組模擬隊列

* @program: jstl2

* @create: 2019-10-05 08:58

*/

public class ArrayQueueDemo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

//測試

ArrayQueue queue = new ArrayQueue(3);

char key = ' '; //接受用戶輸入

boolean loop = true; //循環終止判斷器

while (loop) {

System.out.println("s(show):顯示隊列");

System.out.println("(e(exit)):退出程序");

System.out.println("a(add):添加數據到隊列");

System.out.println("g(get):從隊列取出數據");

System.out.println("h(head):查看隊列頭部的數據");

System.out.println("按提示輸入......");

key = scanner.next().charAt(0); //接收數據

switch (key) {

case 's':

queue.showQueue();

break;

case 'a':

System.out.println("請輸入一個數");

int value = scanner.nextInt();

queue.addQueue(value);

break;

case 'g':

try {

int res = queue.getQueue();

System.out.printf("取出的數是%d\n", res);

} catch (Exception e) {

System.out.println(e.getMessage());

}

break;

case 'h':

try {

int res = queue.headQueue();

System.out.printf("隊列頭部的數是%d\n", res);

} catch (Exception e) {

System.out.println(e.getMessage());

}

break;

case 'e':

scanner.close();

loop = false;

break;

default:

System.out.println("...輸入不合法,請重新輸入...");

break;

}

}

System.out.println("程序退出....");

}

}

//使用數組模擬隊列--編寫一個ArrayQueue

class ArrayQueue {

private int maxSize; //數組的最大容量

private int front; //隊列頭

private int rear; //隊列尾

private int[] arr; //存放數據的數組,模擬隊列.

//創建隊列的構造器

public ArrayQueue(int arrMaxSize) {

maxSize = arrMaxSize;

arr = new int[maxSize];

front = -1;

rear = -1;

}

//判斷隊列是否已經滿了

public boolean isFull() {

return rear == maxSize - 1;

}

//判斷隊列是否為空

public boolean isEmpty() {

return rear == front;

}

//添加數據到隊列

public void addQueue(int n) {

//判斷隊列是否已滿

if (isFull()) {

System.out.println("隊列已滿,不能添加");

return;

}

rear++; //指針后移

arr[rear] = n;

}

//數據出隊列

public int getQueue() {

//判斷隊列是否為空

if (isEmpty()) {

throw new RuntimeException("隊列為空,不能取數據");

}

front++; //頭部指針后移

return arr[front];

}

//顯示隊列的所有數據

public void showQueue() {

if (isEmpty()) {

System.out.println("隊列為空,沒有數據");

return;

}

for (int i = 0; i < arr.length; i++) {

System.out.printf("arr[%d]=%d\n", i, arr[i]);

}

}

//顯示隊列的頭部數據,這個不是取出數據

public int headQueue() {

//判斷是否為空

if (isEmpty()) {

System.out.println("隊列為空,沒有數據....");

throw new RuntimeException("隊列為空,沒有數據....");

}

return arr[front + 1];

}

}

改進版---環形隊列:

環形隊列代碼

package com.structure;

import java.util.Scanner;

/**

* @auther::9527

* @Description: 環形隊列

* @program: jstl2

* @create: 2019-10-05 09:53

*/

public class CircleArrayQueueDemo {

public static void main(String[] args) {

//創建一個環形隊列 這里輸入最大數是4,其實有效的是3

CircleArray queue = new CircleArray(4);

char key = ' '; // 接收用戶輸入

Scanner scanner = new Scanner(System.in);//

boolean loop = true;

// 輸出一個菜單

while (loop) {

System.out.println("s(show): 顯示隊列");

System.out.println("e(exit): 退出程序");

System.out.println("a(add): 添加數據到隊列");

System.out.println("g(get): 從隊列取出數據");

System.out.println("h(head): 查看隊列頭的數據");

key = scanner.next().charAt(0);// 接收一個字符

switch (key) {

case 's':

queue.showQueue();

break;

case 'a':

System.out.println("輸出一個數");

int value = scanner.nextInt();

queue.addQueue(value);

break;

case 'g': // 取出數據

try {

int res = queue.getQueue();

System.out.printf("取出的數據是%d\n", res);

} catch (Exception e) {

// TODO: handle exception

System.out.println(e.getMessage());

}

break;

case 'h': // 查看隊列頭的數據

try {

int res = queue.headQueue();

System.out.printf("隊列頭的數據是%d\n", res);

} catch (Exception e) {

// TODO: handle exception

System.out.println(e.getMessage());

}

break;

case 'e': // 退出

scanner.close();

loop = false;

break;

default:

break;

}

}

System.out.println("程序退出~~");

}

}

class CircleArray {

private int maxSize; //數組的最大容量

//front 變量調整:front就指向隊列的第一個元素,即:arr[front]=arr[0]

private int front;

//rear 變量調整:rear 初始值為0 指向隊列的最后一個元素的位置,因為需要空出一個位置作約束

private int rear;

private int[] arr;

//構造器

public CircleArray(int arrMaxSize) {

maxSize = arrMaxSize;

arr = new int[maxSize];

}

//判斷隊列是否已滿

public boolean isFull() {

return (rear + 1) % maxSize == front;

}

//判斷隊列是否為空

public boolean isEmpty() {

return rear == front;

}

//添加數據到隊列

public void addQueue(int n) {

//判斷隊列是否已滿

if (isFull()) {

System.out.println("隊列已滿,不能添加數據");

return;

}

//直接加入數據

arr[rear] = n;

//rear 后移,后移的時候,要通過取模來實現環形后移

rear = (rear + 1) % maxSize;

}

//獲取隊列的數據,出隊列

public int getQueue() {

if (isEmpty()) {

throw new RuntimeException("隊列為空,不能取數據");

}

//由于front是指向隊列的第一個元素,所以需要

// 1、front的值先保存到臨時變量

//2、將front后移一位,通過取模實現環形后移,

// 3、將臨時變量返回

int temp = arr[front];

front = (front + 1) % maxSize;

return temp;

}

//顯示隊列的所有數據

public void showQueue() {

if (isEmpty()) {

System.out.println("隊列為空,沒有數據~");

return;

}

//遍歷環形隊列....這里有個小技巧,需要記住

for (int i = front; i < front + size(); i++) {

System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);

}

}

//求出當前隊列有效數據的個數

public int size() {

return (rear + maxSize - front) % maxSize;

}

//顯示隊列的頭部數據

public int headQueue(){

//判斷是否為空

if (isEmpty()){

throw new RuntimeException("隊列為空,沒有數據");

}

return arr[front];

}

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的php环行队列实现,java数组实现队列及环形队列实现过程解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。