# Java Data Structures and algorithms (v)--queues

Source: Internet
Author: User

Queues, QUEQE, are the real-life queues.

1. Simple queue:

`public class Queqe {private int array[];    private int front;    private int end;    private int number;        private int Max;        Private Queqe (int maxsize) {array = new int[maxsize];        max = maxsize;        Front = 0;        end = 0;    Number = 0;            } private void Insert (int item) {if (Isfull ()) {System.out.println ("is Full,can not insert");        Return        } array[end++] = Item;    number++;            } private int Remove () {if (IsEmpty ()) {System.out.println ("is Empty,can not remove");        return-1;        } number--;    return array[front++];    } private Boolean Isfull () {return number = = max;    } private Boolean IsEmpty () {return number = = 0;    } private int size () {return number;    } private int Peekfront () {return array[front];        } public static void Main (string[] args) {QUEQE q = new Queqe (4); SyStem.out.println ("Queqe is empty:" +q.isempty ());        Q.insert (1);        Q.insert (2);        Q.insert (3);        Q.insert (4);                Q.insert (5);        System.out.println (Q.size ());                System.out.println ("Front is" +q.peekfront ());    while (!q.isempty ()) System.out.println (Q.remove ()); }}RESULT:QUEQE is Empty:trueis Full,can isn't Insert4front is 11234`

or through an array, but unlike the stack, the queue has the head of the team, the team head points to the header of the queue, and the tail of the queue points to the tail, number of the queue count, whenever a new element is inserted, the end of the tail shift, whenever a new element is removed, the first team to move back, Insert and remove are inserted and removed depending on whether the queue is empty or full。

Advanced elements in the front, then remove the time is to remove the team head, to achieve first-out effect, • Insertion and removal is not used to move the entire array, just change the team head and queue point, so the time complexity and the stack is the same as O (1).

2, do not change the method after number

The number of elements in the front used to count the queue, in fact, do not use this variable, a single team head and tail can also be implemented queue.

`    Private Boolean Isfull () {        return end = = max;    }    Private Boolean IsEmpty () {        return (front ==0&&end ==0) | | Front = = max;    }        private int size () {        return end-front;    }`

Team tail minus team head is the number of elements, because once inserted, end is increased by 1.

And judging whether it's a little more simple, the tail of the queue moves to the end of the array, which is full.

Empty words will be more complicated, one is the beginning front to zero, but there are elements inserted, at this time end is not zero, if the two are zero that is empty, and there is no stop remove, front moved to the end, proof that the elements before all removed, this is also empty.

The author is based on the change of front and end, then the values of front and end are changed directly in insert and remove, in IsEmpty and isfull, for example in Isfull ():

`    Private Boolean Isfull () {        return end+2 = = Front| | (front+max-2==end);    }`

The end of the author is set to-1 and then once the end exceeds Max will become-1, so there is a very complex positional relationship.

This is more complicated. Because this is similar to the loop queue, it goes out and starts to insert again.

In fact, in IsEmpty and isfull directly to judge on it. Then really out of the time when you print the error directly after the return.

3. Priority queue

Bank business of course to line up, but, the bank's VIP users do not need, because he deposited more money than you, the bank attaches importance to this customer, the priority is high, so although he came to the bank later than you, but the automatic arranging when the number than before you.

In that case, the priority queue is like a queue that is prioritized in the first order.

`private void Insert (int item) {    if (Isfull ()) {        System.out.println ("is Full,can not insert");        return;    }    if (end = = 0) {        array[end++] = Item;    } else{        int i;        for (i = number-1; I >=0; i--) {            if (item > Array[i]) {                array[i+1] = Array[i];            }        }        array[i+1]= item;        end++;    }    `

now we see the value as high priority, the first insert directly into the first bit of the array, after the beginning of the insertion of the comparison, if it is smaller than the previous number, then placed in the end of the queue, if large, the entire queue is pushed back, and then find the appropriate location to insert, and the insertion sort is similar. In this way, the elements are inserted in the order of precedence from large to small (ex: prefix expression: infix expression after: postfix expression).

4. Parsing of arithmetic expressions

1+2+3=?

Since the education we received from the beginning is from left to right, there are parentheses to calculate the parentheses inside, first multiplication and then add and subtract such ideas, and then see the above formula when the result is suddenly, this is infix expression. That's how we calculate formulas in our brains.

From left to right, 1+2 look at the back, is the + number, that can be directly calculated 1+2 the value of 3,3+3 after IS = number, direct calculation 3+3=6.

1+2*3=?

From left to right, 1+2 look at the back, is the * number, that can not directly calculate the value of 1+2, first calculate the 2*3=6, and then look at the back as =, so the calculation 1+6=7.

1* (2+3) =?

From left to right, 1 is followed by a *, so it is calculated first, but the following is (so look at the parentheses, the parentheses inside is 2+3=5 =, so the direct calculation of 1*5=5.

But computers are not as intelligent as the human brain. A glance at the past can be basically mental arithmetic. It is easier for the computer to recognize the suffix expression, which is as good for us if we accept the suffix expression from a small age.

Suffix expression is indicated by the symbol A,B,C, because the number of digits, written 1242132 +, do not know that the two numbers added, then there will be separators, but undoubtedly increase the difficulty.

a+b-c Turn suffix:

Read a, read +, continue to read B, written in AB, and then read-the number can be placed after AB, that is, ab+, read C, the end of the ab+c-.

A+b*c Turn suffix:

Read a, read +, read B, placed as AB, and then read the back, * priority is greater than + number, temporarily can not put +, and then read c,c back end, so you can put in *, in Put +, that is abc*+.

A * (b+c) turn suffix:

Read a, read *, later found (can not be placed *, (B+C * Priority High, read, followed by), can be changed to bc+, and a * together, that is abc+*.

This is a queue, but the structure to use is the stack, because it comes in. The arithmetic here does not have to be pressed into the stack, directly output.

a+b-c Turn suffix:

Direct output A, read +, stack empty press into the stack, then output B, stack non-empty, pop +, read to-,+ priority >=-, output +, pressure-into the stack, output C, end, pop-.

Final result: ab+c-.

A+b*c Turn suffix:

Direct output A, read +, stack empty press into the stack, then output B, stack non-empty, pop +, read to *,+ priority <-, so first + press the stack, then press *, Output C, end, Pop *, then play +.

Final result: abc*+.

A * (b+c) turn suffix:

Direct output A, read *, stack empty press into the stack, read (pressure (into the stack, output B, read +, press the stack, output C, read), found that the end of the back can be ejected + and output, pop up (not output, pop-up *.

final Result: abc+*.

After looking at this analysis process, we use the stack to push the stack and eject the operator.

If you write a calculator purely in a high-level language, you can use infix expressions directly, but to the bottom of the compiler, you need to implement the suffix expression, but also to consider the number of in-place and other operators, you can think of code far more complex than the above.

The whole idea is this, the implementation of code is through the stack and conditional judgment into the stack and out of the stack.

Java Data Structures and algorithms (v)--queues

Related Keywords:

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.