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