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