Write a Java queue class

zhaozj2021-02-16  73

The queue is a data structure that is commonly used in the design program. It is similar to the queuing phenomenon in our daily life, using a storage structure called "advanced first out" (FIFO). Data elements can only be entered from the tail and take out from the team. In the queue, the data element can be arbitrarily increased, but the order of the data elements will not change. Whenever a data element is removed from the queue, the rear data element is moved forward. So, from the queue from the queue is the headed data. According to these features, the queue defines the following operations: ENQ (X) inserts 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 not Change; EMPTY () determines whether the queue is empty, the air is returned; clear () empties; Search (x) finds the location of the most recent elements from the lead, if there is no existence, return -1. The Vector Class is a class specifically responsible for handling object elements in order storage and arbitrarily deletion of object elements. Therefore, Java's queue classes can be quickly implemented. public class Queue extends java public synchronized void enq (Object x) {super.addElement (x);} public synchronized Object deq () {/ * If the queue is empty, triggering exception EmptyQueueException * / if (this.empty ()) throw new EmptyQueueException (); Object x = super.elementAt (0); super.removeElementAt (0); return x;} public synchronized Object 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 (Object x) {Return Super.indexof (x); }}} public class emptyqueueexception Extends Java} Compiled by JDK1.1.5 by JDK1.1.5

转载请注明原文地址:https://www.9cbs.com/read-16511.html

New Post(0)