Example of Java array simulation priority queue data structure _java

Source: Internet
Author: User
Tags arrays int size

Priority queues
If we assign a number to each element to mark its priority, we might want to set a smaller number with a higher priority so that we can access the highest-priority element in a collection and find and delete it. In this way, we introduce the priority queue this kind of data structure. A priority queue (priority queue) is a collection of 0 or more elements. Each element has a priority, and the actions performed on the priority queue are (1) lookup (2) Insert a new element (3) Delete in general, a lookup operation is used to search for the element with the highest priority, and deletion is used to delete the element. For elements of the same priority, they can be processed in FIFO order or by any priority.

Java Array Simulation queue
a queue is a special linear table that allows deletions only at the front of the table and inserts at the back end of the table. The end of the insert operation is called the team tail, and the end of the delete operation is called the team head. This is what we usually use to speak of the advanced first Out principle (FIFO). The list in Java can be used as a queue, adding elements at the end of the queue using the List.add method, and the List.remove method is used to remove elements from the queue header.

Java Array Impersonation Priority queue Structure instance:

Package datastruct; 
Import Java.util.Arrays; 
 
Import Java.util.Comparator; /** * Use array to simulate precedence queue priority high precedence, first-Line table structure * Similar to the comparator TreeSet, TREEMAP/public class Simulatepriorityqueue {Publi 
c static void Main (string[] args) {simulatepriorityqueue queue = new Simulatepriorityqueue (4); 
Simulatequeue queue = new Simulatequeue (); 
    SYSTEM.OUT.PRINTLN ("remove element:" + 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 ("remove element:" + queue.remove ()); 
    SYSTEM.OUT.PRINTLN ("remove element:" + queue.remove ()); 
    SYSTEM.OUT.PRINTLN ("remove element:" + queue.remove ()); 
SYSTEM.OUT.PRINTLN ("remove element:" + queue.remove ()); 
    SYSTEM.OUT.PRINTLN ("remove element:" + queue.remove ()); 
    SYSTEM.OUT.PRINTLN ("Size:" + queue.size ()); 
  System.out.println ();     private int msize = 3; Size Private INt[] Marray;     array private int mnextitem; 
    The next position can also be treated 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 Element * @param item */public void Insert (int item) {if (!isfull ()) {Marray[mnextit 
      em++] = 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, queue is full----"); }/** * Move out element advanced First Out * @return/public int Remove () {if (!isempty ()) {return marray[-- 
Mnextitem];    else {throw new IllegalArgumentException ("No elements can be taken out"); 
  }/** * View the preceding element * @return * * * public int peek () {return marray[mnextitem-1]; 
  /** * is null * @return/public Boolean isempty () {return mnextitem = 0; 
  /** * is 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 an element, the queue is full----size:4 peek:5 remove element: 5 remove element: 3 remove element:
2
remove element: 1
size:0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.