您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Java中循環隊列與非循環隊列的區別有哪些,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
判滿:(rear+1) % maxsize == front
判空:front == rear
隊列元素個數:rear = (rear + maxsize - front) % maxsize
front指針移動方式:front = (front + 1) % maxsize
rear指針移動方式:rear= (rear+ 1) % maxsize
import java.awt.Font; import java.util.Scanner; import javax.management.RuntimeErrorException; public class CircleArrayQueueDemo { public static void main(String[] args) { //創建隊列 CircleArrayQueue circleArrayQueue = new CircleArrayQueue(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': circleArrayQueue.showQueue(); break; case 'e': circleArrayQueue.showQueue(); break; case 'a': System.out.println("輸入一個數"); int value = scanner.nextInt(); circleArrayQueue.addQueue(value); break; case 'g': try { int res = circleArrayQueue.getQueue(); System.out.printf("取出的數據是%d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { int res = circleArrayQueue.headQueue(); System.out.printf("隊列頭的數據是%d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; default: scanner.close(); loop = false; break; } } System.out.println("程序退出"); } } //隊列 class CircleArrayQueue{ private int maxsize; private int front; private int rear; private int[] arr; //構造器 public CircleArrayQueue(int arrmaxsize) { maxsize = arrmaxsize; front = 0; rear = 0; 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("隊列為空,不可取出元素!"); } //取值 int value = arr[front]; //front后移 front = (front + 1)%maxsize; return value; } //遍歷 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]; }
結果示意圖
判滿:(rear+1) % maxsize == front
判空:front == rear
隊列元素個數:rear = (rear + maxsize - front) % maxsize
front 指針移動方式:front = (front + 1) % maxsizer
ear指針移動方式:rear= (rear+ 1) % maxsize
import java.awt.Font; import java.util.Scanner; import javax.management.RuntimeErrorException; public class CircleArrayQueueDemo { public static void main(String[] args) { //創建隊列 CircleArrayQueue circleArrayQueue = new CircleArrayQueue(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': circleArrayQueue.showQueue(); break; case 'e': circleArrayQueue.showQueue(); break; case 'a': System.out.println("輸入一個數"); int value = scanner.nextInt(); circleArrayQueue.addQueue(value); break; case 'g': try { int res = circleArrayQueue.getQueue(); System.out.printf("取出的數據是%d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { int res = circleArrayQueue.headQueue(); System.out.printf("隊列頭的數據是%d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; default: scanner.close(); loop = false; break; } } System.out.println("程序退出"); } } //隊列 class CircleArrayQueue{ private int maxsize; private int front; private int rear; private int[] arr; //構造器 public CircleArrayQueue(int arrmaxsize) { maxsize = arrmaxsize; front = 0; rear = 0; 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("隊列為空,不可取出元素!"); } //取值 int value = arr[front]; //front后移 front = (front + 1)%maxsize; return value; } //遍歷 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]; } }
結果示意圖
關于“Java中循環隊列與非循環隊列的區別有哪些”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。