您好,登录后才能下订单哦!
这篇文章将为大家详细讲解有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。