Вы находитесь на странице: 1из 3

/* * Queue.

java * * Created on July 28, 2006, 8:42 AM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package mazesolving; /** * Implements a queue abstract data type. A queue is a first in first out (FIFO) * type data structure. Each queue has a front and a rear associated with it. * Entries are added to the rear of the queue and removed from the front of the * queue. * * @author Alan Scrivner * @version 1 */ public class Queue { private Node head; private Node rear; Queue() { head = null; rear = head; } /** * Determine if the queue is empty * * @return true if the queue is empty, else false */ public boolean isEmpty() { return null == head; } /** * Remove all elements from the queue * */ public void makeEmpty() { head = null; } /** * Returns the first entry in the queue * * @return Object or null if queue is empty */ public Object getFront() { if (!isEmpty()) return head.element; else return null; }

/** * Adds the first entry in the queue * * @param element * or null if queue is empty */ public void addFirst(Object element) { if (isEmpty()) { head = new Node(element); rear = head; } else { head = new Node(element, head); } } /** * Enqueue allows the user to add an item to the queue. * * @param element * The object to be added to the queue */ public void enqueue(Object element) { if (isEmpty()) { head = new Node(element); rear = head; } else { rear.next = new Node(element); rear = rear.next; } } /** * Dequeue allows the user to remove the first element from the queue * * @return The first object in the queue */ public Object dequeue() { Object temp = null; if (!isEmpty()) { temp = head.element; head = head.next; } return temp; } /** * Determines if the object is already in the queue * * @param element * the object one is searching for * @return true if the object is in the queue, else false */ public boolean inQueue(Object element) { Node tmp = head; while (null != tmp) { if (tmp.element.equals(element)) return true; tmp = tmp.next; } return false;

} }

Вам также может понравиться