The Java array simulates an instance of a priority queue data structure

  • 2020-05-09 18:32:29
  • OfStack

Priority queue
If we assign a number to each element to indicate its priority, we might as well set the smaller number to a higher priority so that we can access the highest-priority element in a collection and find and delete it. Thus, we introduce the data structure of priority queues. The priority queue (priority queue) is a collection of 0 or more elements, each of which has a priority. The operations performed on the priority queue are (1) find (2) insert a new element (3) delete (1). In general, the search operation is used to search for the element with the highest priority, and the delete operation is used to delete the element. For elements with the same priority, it may be processed in first-in, first-out order or at any priority.

The Java array simulates the queue
A queue is a special linear table that allows deletion at the front end of the table and insertion at the back end of the table. The end of the insert operation is called the tail of the queue, and the end of the delete operation is called the head of the queue. This is often referred to as the first in first out principle (FIFO). The List in Java can be used as a queue, the list.add method is used for adding elements to the end of the queue, and the list.remove method is used for removing elements from the head of the queue.

Java array simulates the priority queue structure example:


package datastruct; 
 
import java.util.Arrays; 
import java.util.Comparator; 
 
/** 
 *  Simulate with arrays   Priority queue   Priority first, first out   Linear tabular structure  
 *  Similar to the comparator  TreeSet , TreeMap 
 */ 
public class SimulatePriorityQueue { 
   
  public static void main(String[] args) { 
    SimulatePriorityQueue queue = new SimulatePriorityQueue(4); 
//   SimulateQueue queue = new SimulateQueue(); 
//   System.out.println(" Extract elements: " + queue.remove()); 
    queue.insert(1); 
    queue.insert(3); 
    queue.insert(2); 
    queue.insert(5); 
    queue.insert(4); 
    System.out.println("size:" + queue.size()); 
    System.out.println("peek:" + queue.peek()); 
    System.out.println(" Extract elements: " + queue.remove()); 
    System.out.println(" Extract elements: " + queue.remove()); 
    System.out.println(" Extract elements: " + queue.remove()); 
    System.out.println(" Extract elements: " + queue.remove()); 
//   System.out.println(" Extract elements: " + queue.remove()); 
    System.out.println("size:" + queue.size()); 
    System.out.println(); 
  } 
 
  private int mSize = 3;     // The size of the  
  private int[] mArray;    // An array of  
  private int mNextItem;     // Under the 1 A position, can also be regarded as   The current number of elements  
   
  public SimulatePriorityQueue() { 
    mArray = new int[mSize]; 
    mNextItem = 0; 
  } 
  public SimulatePriorityQueue(int size) { 
    this.mSize = size; 
    mArray = new int[mSize]; 
    mNextItem = 0; 
  } 
  /** 
   *  Insert elements  
   * @param item 
   */ 
  public void insert(int item) { 
    if (!isFull()) { 
      mArray[mNextItem++] = item; 
      for (int i = 0; i < mNextItem; i++) {// Bubble sort  
        for (int j = 0; j < mNextItem - 1; j++) { 
          if (mArray[j] > mArray[j + 1]) { 
            mArray[j] = mArray[j + 1] + 0 * (mArray[j + 1] = mArray[j]); 
            System.out.println(Arrays.toString(mArray)); 
          } 
        } 
      } 
      System.out.println(Arrays.toString(mArray)); 
    } else { 
      System.out.println("---- Cannot insert element, the queue is full ----"); 
    } 
  } 
  /** 
   *  Remove elements   First in first out  
   * @return 
   */ 
  public int remove() { 
    if (!isEmpty()) { 
      return mArray[--mNextItem]; 
    } else { 
      throw new IllegalArgumentException(" There are no more elements to pull out "); 
    } 
  } 
  /** 
   *  Look at the previous element  
   * @return 
   */ 
  public int peek() { 
    return mArray[mNextItem - 1]; 
  } 
  /** 
   *  Whether is empty  
   * @return 
   */ 
  public boolean isEmpty() { 
    return mNextItem == 0; 
  } 
  /** 
   *  Whether the full  
   * @return 
   */ 
  public boolean isFull() { 
    return mNextItem == mSize; 
  } 
  /** 
   * size 
   * @return 
   */ 
  public int size() { 
    return mNextItem; 
  } 
   
} 

Output results:


[1, 0, 0, 0]
[1, 3, 0, 0]
[1, 2, 3, 0]
[1, 2, 3, 0]
[1, 2, 3, 5]
---- Cannot insert element, the queue is full ----
size:4
peek:5
 Extract elements: 5
 Extract elements: 3
 Extract elements: 2
 Extract elements: 1
size:0


Related articles: