Java Data Structure Series--Queue (1): Sequential storage structure of queue and its implementation

Source: Internet
Author: User

Package queue.sequencequeue;/** * Queue definition: A queue is a linear table that allows only one end of an insert operation, while a delete operation at the other end is a line-in, first-in-A-class, FIFO, short-form, Allow one end of the insertion * to be called the tail of the queue, allowing the deleted end to be called the team header * @author WL * */public class Sequencequeue {private object[] elementdata;//use an array to save the elements of the queues private int front;//queue's head pointer private int rear;//queue tail pointer//parameterless constructor public Sequencequeue () {elementdata=new object[10];front=0;rear=0 ;} With the parameter constructor, specify the initialization length of the array public sequencequeue (int capacity) {elementdata=new object[capacity];front=0;rear=0;} The size of the queue public int size () {return rear-front;} Determines whether the queue is empty public boolean isEmpty () {int size=size (); return size==0;} Determines whether the queue is full public boolean isfull () {int size=size (); return size==elementdata.length;} into queue public void enQueue (int data) {if (Isfull ()) {throw new Indexoutofboundsexception ("queue full");} Elementdata[rear++]=data;} Out of queue public Object DeQueue () {if (IsEmpty ()) {throw new Indexoutofboundsexception ("Queue is Empty");} Get the opponent Element Object data=elementdata[front];//release the head element Elementdata[front++]=null;return data;} Get the opponent element, do not delete public Object Getfront () {if (IsEmpty ()) {RetuRN null;} Else{return Elementdata[front];}} The element in the print queue public void traverse () {if (IsEmpty ()) {System.out.println ("null");} else{for (int i=front;i<rear;i++) {System.out.print (elementdata[i]+ "");}} System.out.println ();}}

Java Data Structure Series--Queue (1): Sequential storage structure of queue and its implementation

Related Article

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.