Compile a JAVA queue class

Source: Internet
Author: User

Compile a JAVA queue class

Based on these features, the following six operations are defined for the queue:

Enq (x) inserts an element with the value of x into the queue;

Deq () deletes an element from the queue;

Front () reads an element from the queue, but the queue remains unchanged;

Empty () is used to determine whether the queue is empty. If it is null, true is returned;

Clear () clears the queue;

Search (x) searches for the position of the element closest to the first of the team. If it does not exist,-1 is returned.

The Vector class is a JAVA-specific class responsible for processing the ordered storage of object elements and arbitrary addition and deletion. Therefore, the Vector

JAVA queue classes can be quickly implemented.

Public class Queue extends java

Public synchronized void enq (ob ject x ){

Super. addElement (x );

}

Public synchronized ob ject deq (){

/* If the queue is empty, an EmptyQueueException exception is thrown */

If (this. empty ())

Throw new EmptyQueueException ();

Ob ject x = super. elementAt (0 );

Super. removeElementAt (0 );

Return x;

}

Public synchronized ob ject front (){

If (this. empty ())

Throw new EmptyQueueException ();

Return super. elementAt (0 );

}

Public boolean empty (){

Return super. isEmpty ();

}

Public synchronized void clear (){

Super. removeAllElements ();

}

Public int search (ob ject x ){

Return super. indexOf (x );

}

}

Public class EmptyQueueException extends java

}

The above program is compiled in JDK1.1.5

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.