// Copyright (C) 2002 -- See your tutor for details // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // /** * Circularly Linked implementation of a Queue ADT. * @author Andrew H. Hoefel ahhoefel@student.math.uwaterloo.ca * @version v0.5 -- Jan 5, 2002 */ public class QueueCL extends Object implements Queue { private Node tail = null; private int count = 0; public QueueCL() { super(); } public void enqueue(Object item) { if(tail == null) { this.tail = new Node(item); this.tail.setNext(tail); }else { Node tempNode = new Node(item); tempNode.setNext(this.tail.getNext()); this.tail.setNext(tempNode); this.tail = this.tail.getNext(); } this.count++; } public Object dequeue() { Object item = this.tail.getNext().getItem(); if(tail.getNext() == tail) { tail = null; }else { this.tail.setNext(tail.getNext().getNext()); } this.count--; return item; } public Object peek() { return this.tail.getNext().getItem(); } public boolean isEmpty() { return tail == null; } public int size() { return count; } }