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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构和算法之数组模拟队列

發布時間:2024/10/5 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构和算法之数组模拟队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

特點:先入先出(類似于銀行排隊問題)


一、數組模擬隊列

package com.company;import java.util.Scanner;/*** @author:抱著魚睡覺的喵喵* @date:2021/1/31* @description: the array simulate the queue*/ public class ArrayQueue {//test array simulate queuepublic static void main(String[] args) {ArraySimulateQueue arraySimulateQueue = new ArraySimulateQueue(3);char key=' ';Scanner scanner = new Scanner(System.in);boolean flag=true;while (flag){System.out.println("s:show queue");System.out.println("a:add data to the queue");System.out.println("g:the data obtained from the queue");System.out.println("h:view queue header data");System.out.println("e:exit the program");key=scanner.next().charAt(0);switch (key){case 's':arraySimulateQueue.showQueue();break;case 'a':System.out.println("please entry a data!");int data=scanner.nextInt();arraySimulateQueue.enQueue(data);break;case 'g':try {int result=arraySimulateQueue.deQueue();System.out.println("the data obtained from the queue is:"+result);}catch (Exception e){System.out.println(e.getMessage());}break;case 'h':try {int frontData=arraySimulateQueue.getFront();System.out.println("the head of the queue data is:"+frontData);}catch (Exception e){System.out.println(e.getMessage());}break;case 'e':scanner.close();flag=false;return;default:System.out.println("the characters you entered are wrong!");}}System.out.println("program has exited!");} } /*** use array to simulate queue*/ class ArraySimulateQueue{private int maxSize;private int front;private int rear;private int[] arr;public ArraySimulateQueue(int currentMaxSize){this.maxSize=currentMaxSize;this.front=-1;this.rear=-1;arr=new int[maxSize];}//determine whether the queue is fullpublic boolean isFull(){return rear == maxSize-1;}//determine whether the queue is emptypublic boolean isEmpty(){return rear == front;}//enqueuepublic void enQueue(int data){if (isFull()){System.out.println("the queue is full and cannot add data!");return;}else{this.rear++;arr[rear]=data;System.out.println("successful data entry!");}}//dequeuepublic int deQueue(){if (isEmpty()){throw new RuntimeException("the queue is empty and cannot get data!");}else{this.front++;int result=arr[front];arr[front]=0;return result;}}//show datapublic void showQueue(){for (int i = 0; i < arr.length; i++) {System.out.printf("arr[%d]=%d",i,arr[i]);;System.out.println();}}public int getFront(){if (isEmpty()){throw new RuntimeException("the queue is empty and cannot get header data!");}else{return arr[front+1];}} }

分析缺點:

由圖可以看出使用過的內存單元將不能夠繼續使用,所以使用環形隊列可以解決內存資源浪費的問題

環形隊列

實現方式1

package com.company;import java.util.Scanner;/*** @author:抱著魚睡覺的喵喵* @date:2021/2/1* @description: the array simulate the circular queue*/ public class CircleArrayQueue {public static void main(String[] args) {CircleQueue circleQueue = new CircleQueue(3);boolean flag = true;Scanner scanner = new Scanner(System.in);char key = ' ';while (flag) {System.out.println("s:show queue");System.out.println("a:add data to the queue");System.out.println("g:get data from the queue");System.out.println("h:view queue header data");System.out.println("e:exit the program");key = scanner.next().charAt(0);switch (key) {case 's':circleQueue.showData();break;case 'a':System.out.println("please enter a data:");int result = scanner.nextInt();circleQueue.enQueue(result);break;case 'g':try {System.out.printf("the data obtained from queue is:%d", circleQueue.dequeue());System.out.println();} catch (Exception e) {System.out.println(e.getMessage());}break;case 'h':try {System.out.printf("header data is:%d", circleQueue.getFront());System.out.println();} catch (Exception e) {System.out.println(e.getMessage());}break;case 'e':scanner.close();flag = false;}}} }/*** Circular simulate queue*/ class CircleQueue {private int maxSize;private int front;private int rear;private int[] arr;private int num;public CircleQueue(int currentMaxSize) {this.maxSize = currentMaxSize;this.front = -1;this.rear = -1;arr = new int[maxSize];this.num = 0; //record the number of array units and used to determine whether the circular queue is full or empty}//determine whether the circular queue is fullpublic boolean isFull() {return num == maxSize;}//determine whether the circular queue is emptypublic boolean isEmpty() {return num == 0;}//enqueuepublic void enQueue(int data) {if (isFull()) {System.out.println("the circular is full,unable to add data!");} else if ((rear + 1 == maxSize) && (!isFull())) {rear = 0;arr[rear] = data;num++;} else {rear++;arr[rear] = data;num++;}}//dequeuepublic int dequeue() {if (isEmpty()) {throw new RuntimeException("the circular queue is empty,unable to leave the queue!");} else if ((front + 1 == maxSize) && (!isEmpty())) {front = 0;int result = arr[front];arr[front] = 0;num--;return result;} else {front++;int result = arr[front];arr[front] = 0;num--;return result;}}//get the circular queue header datapublic int getFront() {if (isEmpty()) {throw new RuntimeException("the circular queue is empty cannot get header data!");} else if (front +1 == maxSize){return arr[0];} else {return arr[front+1];}}//show datapublic void showData() {for (int i = 0; i < arr.length; i++) {System.out.printf("arr[%d]=%d", i, arr[i]);System.out.println();}} }

實現方式2

package com.company;import java.util.Scanner;/*** @author:抱著魚睡覺的喵喵* @date:2021/2/2* @description:*/ public class CircleArrayQueue2 {public static void main(String[] args) {CircleQueue2 circleQueue = new CircleQueue2(3);boolean flag = true;Scanner scanner = new Scanner(System.in);char key = ' ';while (flag) {System.out.println("s:show queue");System.out.println("a:add data to the queue");System.out.println("g:get data from the queue");System.out.println("h:view queue header data");System.out.println("e:exit the program");key = scanner.next().charAt(0);switch (key) {case 's':circleQueue.showData();break;case 'a':System.out.println("please enter a data:");int result = scanner.nextInt();circleQueue.enQueue(result);break;case 'g':try {System.out.printf("the data obtained from queue is:%d", circleQueue.dequeue());System.out.println();} catch (Exception e) {System.out.println(e.getMessage());}break;case 'h':try {System.out.printf("header data is:%d", circleQueue.getFront());System.out.println();} catch (Exception e) {System.out.println(e.getMessage());}break;case 'e':scanner.close();flag = false;}}} }/*** Circular simulate queue*/ class CircleQueue2 {private int maxSize;private int front;private int rear;private int[] arr;public CircleQueue2(int currentMaxSize) {this.maxSize = currentMaxSize;this.front = 0;this.rear = 0;arr = new int[maxSize];}//determine whether the circular queue is fullpublic boolean isFull() {return (rear + 1) % maxSize == front;}//determine whether the circular queue is emptypublic boolean isEmpty() {return rear == front;}//enqueuepublic void enQueue(int data) {if (isFull()) {System.out.println("the circular is full,unable to add data!");} else {arr[rear] = data;rear = (rear + 1) % maxSize;}}//dequeuepublic int dequeue() {if (isEmpty()) {throw new RuntimeException("the circular queue is empty,unable to leave the queue!");} else {int result = arr[front];arr[front] = 0;front = (front + 1) % maxSize;return result;}}//get the circular queue header datapublic int getFront() {if (isEmpty()) {throw new RuntimeException("the circular queue is empty cannot get header data!");} else {return arr[front % maxSize];}}//show datapublic void showData() {for (int i = 0; i < arr.length; i++) {System.out.printf("arr[%d]=%d", i, arr[i]);System.out.println();}} }

總結

以上是生活随笔為你收集整理的数据结构和算法之数组模拟队列的全部內容,希望文章能夠幫你解決所遇到的問題。

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