In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
It is believed that many inexperienced people are at a loss about how to realize the queue in Java programming. therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Basic introduction
A queue is an ordered list, which can be implemented with an array or a linked list
Follow the first-in, first-out principle, that is, the data stored in the queue should be taken out first. After the deposit, take it out later.
Array analog queue
The queue itself is an ordered list. If the structure of the array is used to store the data of the queue, the declaration of the queue array is shown in the following figure, where maxSize is the maximum capacity of the queue.
Because the input / output of the queue is processed from the front and back end respectively, two variables front and rear are needed to record the subscript of the front and back end of the queue respectively. The front changes with the data output, while the rear changes with the data input.
Code case
Package com.structures.queue; import java.util.Scanner; public class ArrayQueueDemo {public static void main (String [] args) {ArrayQueue arrayQueue = new ArrayQueue (3); char key =''; / / accept 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 of the queue header"); key = scanner.next (). CharAt (0); switch (key) {case's header: arrayQueue.showQueue (); break Case'asides: System.out.println ("enter an integer"); int value = scanner.nextInt (); arrayQueue.addQueue (value); break Case'gathers: try {int queue = arrayQueue.getQueue (); System.out.printf ("data fetched is% d", queue);} catch (Exception e) {System.out.println (e.getMessage ()) } break; case'ebacks: scanner.close (); loop = false; break; case'hackers: try {int head = arrayQueue.headQueue () System.out.printf ("fetch queue header data is% d", head);} catch (Exception e) {System.out.println (e.getMessage ());} default: break }} System.out.println ("program exit");}} / / use array simulation queue-write an ArrayQueue class class ArrayQueue {/ / represents array maximum capacity private int maxSize; / / queue header private int front; / / queue tail private int rear; / / for storing data, simulation queue private int [] arr / / create the queue constructor public ArrayQueue (int arrMaxSize) {maxSize = arrMaxSize; arr = new int [maxSize]; front =-1 return rear / pointing to the previous position of the queue header / data pointing to the end of the queue, that is, the last data of the queue} / / to determine whether the queue is full or not () {return rear = = maxSize-1 } / / determine whether the queue is empty public boolean isEmpty () {return rear = = front;} / / add data to the queue public void addQueue (int n) {if (isFull ()) {System.out.println ("queue cannot join data"); return;} rear++ / / Let rear move arr [rear] = n;} / / get the queue data, and leave the queue public int getQueue () {if (isEmpty ()) {throw new RuntimeException ("queue is empty, cannot fetch data");} front++; return arr [front] } / / display all data of the queue public void showQueue () {if (isEmpty ()) {System.out.println ("queue is empty, no data");} for (int I = 0; I < this.arr.length; idata +) {System.out.printf ("arr [% d] =% d\ n", I, arr [I]) }} / / displays the header data of the queue. Note that public int headQueue () {if (isEmpty ()) {throw new RuntimeException ("queue is empty and no data");} return arr [front + 1];}}
Analysis of problems
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
At present, this array cannot be used once and does not achieve the effect of reuse.
Use the algorithm to improve this array into a circular queue: modulo%
Analysis of the train of thought of improving into a circular queue
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
The meaning of the front variable is adjusted: front points to the first element of the queue, that is, arr [front] is the first element of the queue, and the initial value of front = 0
The meaning of the rear variable is adjusted: rear points to the next location of the last element of the queue because you want to free up a space as the convention .rear initial value = 0
When the queue is full, the condition is (rear+1)% maxSize = front.
When the queue is listed as a null condition, rear = = front is empty.
When we analyze it this way, the number of valid data in the queue = (rear+maxSize-front)% maxSize.
Case of Ring queue Code
Package com.structures.queue; import java.util.Scanner; public class CircleArrayQueue {public static void main (String [] args) {CircleArray arrayQueue = new CircleArray (4); / / set 4 here, the maximum valid data of its queue is 3 char key =''; / / accept 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 of the queue header"); key = scanner.next (). CharAt (0); switch (key) {case's header: arrayQueue.showQueue (); break Case'asides: System.out.println ("enter an integer"); int value = scanner.nextInt (); arrayQueue.addQueue (value); break Case'gathers: try {int queue = arrayQueue.getQueue (); System.out.printf ("data fetched is% d", queue);} catch (Exception e) {System.out.println (e.getMessage ()) } break; case'ebacks: scanner.close (); loop = false; break; case'hackers: try {int head = arrayQueue.headQueue () System.out.printf ("fetch queue header data is% d", head);} catch (Exception e) {System.out.println (e.getMessage ());} default: break }} System.out.println ("program exit");}} class CircleArray {/ / indicates the maximum capacity of the array private int maxSize; / / front variable makes an adjustment: front points to the first element of the queue, that is, arr [front] is the first element of the queue, and the initial value of front = 0 private int front The meaning of the / / rear variable is adjusted: rear points to the last element of the queue, because you want to free up a space as the convention .rear initial value = 0 private int rear; / / for storing data, simulate the queue private int [] arr; 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 queue public void addQueue (int n) {if (isFull ()) {System.out.println ("queue full, queue cannot join data"); return;} / / directly add data to arr [rear] = n / / move the rear back. Here you must consider taking the module rear = (rear + 1)% maxSize;} / / to get the queue data, and leaving the queue public int getQueue () {if (isEmpty ()) {throw new RuntimeException ("queue is empty, data cannot be fetched") } / / We need to analyze that front is the first element pointing to the queue, / / 1. First save the value corresponding to front to a temporary variable, / / 2. Move the front back and consider taking the module / / 3. Return the temporarily saved variable int value = arr [front]; front = (front + 1)% maxSize; return value;} / / display all data in the queue public void showQueue () {if (isEmpty ()) {System.out.println ("queue is empty, no data") } / / traverse for from front (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. Note that you are not taking the data public int headQueue () {if (isEmpty ()) {throw new RuntimeException ("queue is empty, no data");} return arr [front];}} after reading the above, have you mastered how to implement the queue in Java programming? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.