Writing a Java Queue class

Source: Internet
Author: User

Based on these characteristics, the following six actions are defined for the queue:

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

DEQ () deletes an element from the queue;

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

Empty () to determine whether the queue is empty, empty returns true;

Clear () empty queue;

Search (x) finds the position of the element closest to the first team, and returns-1 if it does not exist.

The vector class is a class in Java that specializes in orderly storage and arbitrary additions and deletions of object elements, so vector

You can quickly implement a Java queue class.

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, causing emptyqueueexception abnormal.

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 by JDK1.1.5

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.