Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The process Analysis of realizing queue and Ring queue in java Array

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "java array implementation queue and ring queue process parsing". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Code content

ArrayQueue--- implements queues with arrays

Package com.structure;import java.util.Scanner;/** * @ auther::9527 * @ Description: array Simulation queue * @ program: jstl2 * @ create: 2019-10-05 08:58 * / public class ArrayQueueDemo {public static void main (String [] args) {Scanner scanner = new Scanner (System.in); / / Test ArrayQueue queue = new ArrayQueue (3); char key =''; / / accept user input boolean loop = true / / Loop termination decision while (loop) {System.out.println ("s (show): display queue"); System.out.println ("(e (exit)): exit the program"); System.out.println ("a (add): add data to the queue"); System.out.println ("g (get): fetching data from the queue") System.out.println ("h (head): view the data in the queue header"); System.out.println ("enter as prompted."); key = scanner.next (). CharAt (0); / / receive data switch (key) {case's data: queue.showQueue (); break Case'anumbers: System.out.println ("Please enter a number"); int value = scanner.nextInt (); queue.addQueue (value); break; case'gears: try {int res = queue.getQueue (); System.out.printf ("the number taken out is% d\ n", res) } catch (Exception e) {System.out.println (e.getMessage ());} break; case'hackers: try {int res = queue.headQueue (); System.out.printf ("the number of queue headers is% d\ n", res) } catch (Exception e) {System.out.println (e.getMessage ());} break; case'ebacks: scanner.close (); loop = false; break; default: System.out.println ("... Invalid input, please re-enter. "); break;}} System.out.println (" Program exit. ");}} / / use array simulation queue-- write a maximum capacity private int front; / / queue header private int rear; / / queue tail private int of an ArrayQueueclass ArrayQueue {private int maxSize; / / array [] arr / / an array of data and an analog queue. / / the constructor public ArrayQueue (int arrMaxSize) {maxSize = arrMaxSize; arr = new int [maxSize]; front =-1; rear =-1;} / / determines whether the queue is full of public boolean isFull () {return rear = = maxSize-1;} / / determines whether the queue is empty public boolean isEmpty () {return rear = = front } / / add data to the queue public void addQueue (int n) {/ / determine whether the queue is full (isFull ()) {System.out.println ("queue is full and cannot be added"); return;} rear++; / / pointer moves back to arr [rear] = n } / / data out of queue public int getQueue () {/ / determine whether the queue is empty if (isEmpty ()) {throw new RuntimeException ("queue is empty, data cannot be fetched");} front++; / / move the header pointer back to return arr [front] } / / display all data of the queue public void showQueue () {if (isEmpty ()) {System.out.println ("queue is empty, no data"); return;} for (int I = 0; I < arr.length; idata +) {System.out.printf ("arr [% d] =% d\ n", I, arr [I]) }} / / displays the header data of the queue, which is not public int headQueue () {/ / determine whether the data is empty if (isEmpty ()) {System.out.println ("queue is empty, no data...."); throw new RuntimeException ("queue is empty, no data....");} return arr [front + 1];}}

Improved version-ring queue:

Ring queue code

Package com.structure;import java.util.Scanner;/** * @ auther::9527 * @ Description: ring queue * @ program: jstl2 * @ create: 2019-10-05 09:53 * / public class CircleArrayQueueDemo {public static void main (String [] args) {/ / create a ring queue where the maximum number entered is 4, but what is actually valid is 3 CircleArray queue = new CircleArray (4); char key ='' / / receive user input Scanner scanner = new Scanner (System.in); / / boolean loop = true; / / output a menu while (loop) {System.out.println ("s (show): display queue"); System.out.println ("e (exit): exit the program"); System.out.println ("a (add): add data to the queue") System.out.println ("g (get): fetch data from the queue"); System.out.println ("h (head): view the data in the queue header"); key = scanner.next () .charAt (0); / / receive a character switch (key) {case's fetch: queue.showQueue (); break Case'anumbers: System.out.println ("output a number"); int value = scanner.nextInt (); queue.addQueue (value); break; case'gathers: / / fetch data try {int res = queue.getQueue (); System.out.printf ("fetched data is% d\ n", res) } catch (Exception e) {/ / TODO: handle exception System.out.println (e.getMessage ());} break; case'hackers: / / View the data of the queue header try {int res = queue.headQueue (); System.out.printf ("the data of the queue header is% d\ n", res) } catch (Exception e) {/ / TODO: handle exception System.out.println (e.getMessage ());} break; case'ebacks: / / exit scanner.close (); loop = false; break; default: break }} System.out.println ("Program exit ~");}} class CircleArray {private int maxSize; / / array maximum capacity / / front variable adjustment: front points to the first element of the queue, that is, arr [front] = arr [0] private int front; / / rear variable adjustment: the initial value of rear is 0 pointing to the position of the last element of the queue, because a position needs to be vacated as a constraint private int rear Private int [] arr; / / constructor public CircleArray (int arrMaxSize) {maxSize = arrMaxSize; arr = new int [maxSize];} / / determine whether the queue is full public boolean isFull () {return (rear + 1)% maxSize = = front;} / / determine whether the queue is empty public boolean isEmpty () {return rear = = front } / / add data to the queue public void addQueue (int n) {/ / determine whether the queue is full (isFull ()) {System.out.println ("queue is full, data cannot be added"); return;} / / directly add data arr [rear] = n / / rear moves backward, when moving backward, you need to take the module to achieve circular backward moving rear = (rear + 1)% maxSize;} / / to get the data of the queue, and public int getQueue () {if (isEmpty ()) {throw new RuntimeException ("the queue is empty, data cannot be taken") } / / because front is the first element pointing to the queue, it is necessary to save the value of / / 1 and front to the temporary variable / / 2, move the front back one bit, move the front backward by taking the module, / / 3, return the temporary variable int temp = arr [front]; front = (front + 1)% maxSize; return temp } / / displays all data of the queue public void showQueue () {if (isEmpty ()) {System.out.println ("queue is empty, no data ~"); return;} / / traverses the ring queue. Here is a little trick to remember for (int I = front; I < front + size (); iTunes +) {System.out.printf ("arr [% d] =% d\ n", I% maxSize, arr [I% maxSize]);}} / / find the number of valid data in the current queue public int size () {return (rear + maxSize-front)% maxSize } / / display the header data of the queue public int headQueue () {/ / to determine whether the queue is empty if (isEmpty ()) {throw new RuntimeException ("queue is empty, no data");} return arr [front];}} "java array to achieve queue and ring queue process parsing" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report