Array mock queue

Array mock queue

Today, learn array simulation queue . Queues are often used in all aspects of life . For example, bank call queue . It's actually a queue . Everyone draws numbers and lines up . Those who go first draw numbers first . So the number ahead will be called first and then out of the team . The back will move forward . That is, the order in the team will change .

In fact, it's easy to simulate queues with arrays .

Several elements of the queue . The queue length (MaxSize) The previous position of the queue head front, Tail of the queue rear, And our array arr[]. We need to create Queue Class to simulate a queue .

package com.joseph.sparseArray;
public class ArrayQueue {
public static void main(String[] args) {
Queue queue = new Queue(3);
System.out.println(" Array simulation queue length is :" + queue.arr.length + ",MaxSize = " + queue.MaxSize);
queue.addQueue(10);
queue.List();
System.out.println("rear:" + queue.rear);
System.out.println("front" + queue.front);
System.out.println(" Header data :" + queue.arr[queue.front + 1]);
System.out.println(" Tail data :" + queue.arr[queue.rear]);
System.out.println("---------------------------------------------------------------------");
queue.addQueue(20);
queue.List();
System.out.println("rear:" + queue.rear);
System.out.println("front" + queue.front);
System.out.println(" Header data :" + queue.arr[queue.front + 1]);
System.out.println(" Tail data :" + queue.arr[queue.rear]);
System.out.println("---------------------------------------------------------------------");
queue.getQueue();
queue.List();
System.out.println("rear:" + queue.rear);
System.out.println("front" + queue.front);
System.out.println(" Header data :" + queue.arr[queue.front + 1]);
System.out.println(" Tail data :" + queue.arr[queue.rear]);
System.out.println("---------------------------------------------------------------------");
queue.addQueue(40);
queue.List();
System.out.println("rear:" + queue.rear);
System.out.println("front" + queue.front);
System.out.println(" Header data :" + queue.arr[queue.front + 1]);
System.out.println(" Tail data :" + queue.arr[queue.rear]);
}
}
class Queue{
int MaxSize ;
int rear ;
int front ;
int arr[] ;
public Queue(int MaxSize) {
this.MaxSize = MaxSize ;
arr = new int[MaxSize];
this.rear = -1;
this.front = -1;
}
public boolean isFull(){
return rear == MaxSize -1 ;
}
public boolean isEmpty(){
return rear == front ;
}
public void addQueue(int key){
// Judge whether it is full
if(isFull()){
System.out.println("QUEUE IS FULL!");
return ;
}
// Not full
this.rear ++ ;
arr[rear] = key ;
if(arr[rear] == key){
System.out.println(" Add success !");
}else{
System.out.println(" Add failure !");
}
}
public int getQueue(){
// The judgment is empty
if(isEmpty()){
throw new RuntimeException("QUEUE IS EMPTY");
}
// Not empty
this.front ++ ;
return arr[front]; }
public void List(){
// Judge
if(isEmpty()){
System.out.println("QUEUE IS EMPTY!");
return ;
}
for(int i = 0 ; i < arr.length ; i ++){
System.out.printf("arr[%d] = %d\n",i,arr[i]);
}
}
}
/**
* @author JosephWang
* @date 2021/8/9 17:18
*/

Be careful :

My dear friends have found our flaws . The array simulation queue has been completed . But arrays are one-time . We find that when the array is full or not . The head and tail of the queue use our rear and front To point to . The subscript after taking out the data will be discarded . Lead to waste . We will optimize this waste in the next issue .

Queue for data structure algorithm learning ( Array simulation java Realization ) More articles about

  1. java Stack and queue simulation --java

    Definition of stack : Stack is a special table, which only inserts and deletes in the header . therefore , The header has a special meaning for the stack , It's called the top of the stack . Accordingly , The end of the table is called the bottom of the stack . A stack without any elements is called an empty stack . The logical structure of the stack : Suppose a stack S The element in is an,a ...

  2. data structure —— The realization of the stack ( Array 、Java)

    Consolidate the data structure Stack is a restricted linear table You can only operate on the footer package com.shine.test.datastruct; import java.util.Arrays; public clas ...

  3. python The first day of self-study of data structure algorithm 【 Data structure and algorithm introduction 】

    1. Algorithm is introduced into : #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ from Queue import Queue import time que = Queu ...

  4. Algorithm notes _224: The probability of winning the championship is simulated (Java)

    Catalog 1 Problem description 2 Solution   1 Problem description Football matches have a certain degree of contingency , The weak team can also beat the strong team . Suppose there is a . B . C . Four teams . Based on their past results , Get the probability table of each team winning against another team ...

  5. data structure (3):java Use an array to simulate a stack

    Stack principle : Array simulation stack : // Array simulation stack class ArrayStack{ // To the top of the stack private int top = -1; private int maxSize; private int[ ...

  6. Data structure and algorithm series 2 The linear table Use java Implementation of dynamic array +ArrayList Source details

    Data structure and algorithm series 2 The linear table Use java Implementation of dynamic array +ArrayList Source details If you don't know about arrays, you can read another article of mine first , That article has a lot of detailed parsing of arrays , This article focuses on dynamic arrays , Link to another article ...

  7. Java Array mock queue + Optimize

    Queue introduction A queue is a sequence table , It can be realized by array or linked list . Follow the first in, first out principle . namely : Data stored in the queue first , You have to take it out first . After the deposit to take out after Sketch Map :( Use arrays to simulate queue diagrams )   Array mock queue The queue itself has a sequence table ...

  8. java Learning notes ( The basic chapter )— Array simulation implementation stack

    Stack concept First in last out strategy (LIFO) Is a basic data structure There are two types of stacks :1. Static stack ( Array implementation ) 2. Dynamic stack ( Linked list implementation ) The model diagram of stack is as follows : Demand analysis Before writing code , I'm used to analyzing the requirements of the program to be implemented first , ...

  9. Array simulates a circular queue (java Realization )

    1.front The meaning of variables :front Point to the first element of the queue , in other words arr[front] It's the first element of the queue front The initial value of the =0. 2.rear The meaning of variables :rear Point to the last position of the last element in the queue ...

  10. java Data structure and algorithm value priority queue

    One . Priority queue What is priority queue : Priority queue is a data structure more commonly used than stack and queue . In the priority queue , Data items are ordered according to the values of keywords . When a data item is inserted into the queue , It will be inserted into the appropriate position in order , Used to ensure the order of the queue . raw ...

Random recommendation

  1. Involved in software testing Linux Order to organize

    Linux File system commands 1. Install Firefox , The first thing to know is Linux System is 64 It's still 32 Bit uname -a 2. Move the downloaded Firefox compressed package to the specified directory (/user/local) Next sudo mv ...

  2. jquery Slide animation

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <head> ...

  3. Java Experiment link

    The first 1 Experiments The content of classroom experiment :Java introduction +Eclipse+PTA+Git+ Blog Experiment assignment book : The first 01 trials ( install JDK. Editor . Write the first Java Program ).pdf Eclipse A concise tutorial (by Zheng Rubin ).p ...

  4. wget Definition of common parameters

    wget Dafa is good , I don't say much nonsense , Let's start with wget Take a trip . Download speed limit wget The command has a built-in option to first load the maximum bandwidth occupied by the task , So as to ensure the smooth operation of other applications . Specific use --limit-rate Count ...

  5. springboot Integrate h2 And Visualization

    1. New projects

  6. navicat mysql Derived data In the form of batch insertion

    This is about mysql Advanced settings for data transfer of the same server type Select the database and right-click “ dump SQL file ” By default, the exported record format is one by one , It's using ” Complete insert statement ”, The format is as follows '); '); '); This format ensures that ...

  7. Mysql Custom function functions Times wrong

    delimiter && [ Function creation statement .....] [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or ...

  8. Learn in simple language Hibernate frame ( Two ):JDBC Basic operation

    Last blog < Learn in simple language Hibernate frame ( One ): Start with examples and get to know Hibernate frame > A brief introduction Hibernate frame , And an example is given to understand Hibernate. This blog will introduce JD ...

  9. How to write a SQL Injection tool

    0x01   Preface I've been thinking about how to write an automatic injection tool , This tool doesn't have to be too complicated , But you can use the simplest . The most direct way to obtain database information , Constructed according to custom payload To bypass the protection , In this way, we can . 0x02 SQL notes ...

  10. 【 Attached source file 】 Diary class App Prototyping sharing -Grid Diary

    Grid Diary It is a recording application loved by literary and artistic youth , It's simple in design , The content is very rich . It is no longer a monotonous written record , The design of the interface is very different , It is made up of many lattices , There is also a question in each grid , Very interesting . Mention the grid ...