Java Basics (2)--Queue operations

Source: Internet
Author: User

A queue is a linear table that allows only one end of the table to be inserted and deleted at the other end.

One end of the allowable insertion is called the tail of the queue (rear), and the one end that allows deletion is called the team Head (front);

The following is an array of Java to simulate the operation of the queue:

/**2015-07-26 18:49:08
* Use array elements to demonstrate the internal operation of the queue
* @author lihaiming
*email:[email protected]
*/
Import Java.util.Scanner;

Public class Queue {
//attribute variables
private int[] A;
private int head=0,tail=0;
/**
* Non-parametric construction method
* The default array length is 4;
*/
Public Queue () {
a = new int[4];
}
/**
* Parametric construction method
* @param m Array length
*/
Public Queue (int m) {
a = new int[m];
}
/**
* Ways to put elements in the queue
* @param i element value
*/
Public void push (int i) {
a[++tail]=i;
}
/**
* Remove element method from queue
*/
public void Pop () {
head++;
}
/**
* The queue element is output sequentially
*/
Public void Display () {
for (int i=head+1;i<=tail;i++) {
System.out.println (A[i]);
}
}
/**
* Returns the number of elements in the queue
* @return
*/
public int Size () {
return tail-head;
}
Public static void Main (string[] args) {
//TODO auto-generated method stub
Scanner input = new Scanner (system.in);
System.out.println ("Please enter the length of the array:");
int m = Input.nextint ();
Queue q = new Queue (m);

System.out.println ("Enter the elements of the queue in turn:");
System.out.println ("Press 1 to End");
int C;
While ((C=input.nextint ())!=-1) {
Q.push (c);
}
System.out.println ("Order of entry into queue:");
Q.display ();

System.out.print ("There are: in the queue");
System.out.println (q.size () + "elements");

System.out.println ("Order taken out in turn:");
While (q.head! = q.tail) {
System.out.println (q.a[q.head+1]);
q.head++;
}

}

}

Program results:

Please enter the length of the array:
6
Enter the elements of the queue, in turn:
Please press-1 to end
1
4
3
6
-1
Enter the order of the queues sequentially:
1
4
3
6
in the queue are: 4 elements
order to take out in turn:
1
4
3
6

First determine the length of an array, here I first determine a length of 6, followed by adding the values 1, 4, 3, 6;

-1 Input End symbol

The order of values taken out is still 1, 4, 3, 6

Queue is FIFO;

Java Basics (2)--Queue operations

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.