Download Queue Samples

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
// Queue Interface
package DataStructures;
import Exceptions.*;
//
//
//
//
//
//
//
//
//
//
Queue interface
******************PUBLIC OPERATIONS*********************
void enqueue( x )
--> Insert x
Object getFront( )
--> Return least recently inserted item
Object dequeue( )
--> Return and remove least recent item
boolean isEmpty( )
--> Return true if empty; else false
void makeEmpty( )
--> Remove all items
******************ERRORS********************************
getFront or dequeue on empty queue
/**
* Protocol for queues.
* @author Mark Allen Weiss
*/
public interface Queue
{
/**
* Test if the queue is logically empty.
* @return true if empty, false otherwise.
*/
boolean isEmpty( );
/**
* Get the least recently inserted item in the queue.
* Does not alter the queue.
* @return the least recently inserted item in the queue.
* @exception Underflow if the queue is empty.
*/
Object getFront( ) throws Underflow;
/**
* Return and remove the least recently inserted item
* from the queue.
* @return the least recently inserted item in the queue.
* @exception Underflow if the queue is empty.
*/
Object dequeue( ) throws Underflow;
/**
* Insert a new item into the queue.
* @param X the item to insert.
*/
void enqueue( Object X );
/**
* Make the queue logically empty.
*/
void makeEmpty( );
}
// Queue Class – Sample 1
// Fig. 17.13: Queue.java
// Class Queue.
package com.deitel.jhtp6.ch17;
public class Queue
{
private List queueList;
// no-argument constructor
public Queue()
{
queueList = new List( "queue" );
} // end Queue no-argument constructor
// add object to queue
public void enqueue( Object object )
{
queueList.insertAtBack( object );
} // end method enqueue
// remove object from queue
public Object dequeue() throws EmptyListException
{
return queueList.removeFromFront();
} // end method dequeue
// determine if queue is empty
public boolean isEmpty()
{
return queueList.isEmpty();
} // end method isEmpty
// output queue contents
public void print()
{
queueList.print();
} // end method print
} // end class Queue
/**********************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and
* Pearson Education, Inc. All Rights Reserved.
*
* DISCLAIMER: The authors and publisher of this book have used their
* best efforts in preparing the book. These efforts include the
* development, research, and testing of the theories and programs
* to determine their effectiveness. The authors and publisher make
* no warranty of any kind, expressed or implied, with regard to these
* programs or to the documentation contained in these books. The authors
* and publisher shall not be liable in any event for incidental or
* consequential damages in connection with, or arising out of, the
* furnishing, performance, or use of these programs.
*********************************************************************/
// Queue Class – Sample 2
Queue.java
// Below is the syntax highlighted version of Queue.java from §4.3 Stacks and Queues
// from Princeton University
/*******************************************************************
* Compilation: javac Queue.java
* Execution:
java Queue
*
* A generic queue, implemented using a linked list. Each queue
* element is of type Item.
*******************************************************************/
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Queue<Item> implements Iterable<Item> {
private int N;
// number of elements on queue
private Node first;
// beginning of queue
private Node last;
// end of queue
// helper linked list class
private class Node {
private Item item;
private Node next;
}
// create an empty queue
public Queue() {
first = null;
last = null;
}
// is the queue empty?
public boolean isEmpty() { return first == null; }
public int length()
{ return N;
}
public int size()
{ return N;
}
// add an item to the queue
public void enqueue(Item item) {
Node x = new Node();
x.item = item;
if (isEmpty()) { first = x;
last = x; }
else
{ last.next = x; last = x; }
N++;
}
// remove and return the least recently added item
public Item dequeue() {
if (isEmpty()) throw new RuntimeException("Queue underflow");
Item item = first.item;
first = first.next;
N--;
return item;
}
// remove and return the least recently added item
public String toString() {
String s = "";
for (Node x = first; x != null; x = x.next)
s += x.item + " ";
return s;
}
public Iterator<Item> iterator()
{ return new QueueIterator();
// an iterator, doesn't implement remove() since it's optional
private class QueueIterator implements Iterator<Item> {
private Node current = first;
public boolean hasNext() { return current != null; }
public void remove() { throw new
UnsupportedOperationException(); }
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
// a test client
public static void main(String[] args) {
/***********************************************
* A queue of strings
***********************************************/
Queue<String> q1 = new Queue<String>();
q1.enqueue("Vertigo");
q1.enqueue("Just Lose It");
q1.enqueue("Pieces of Me");
System.out.println(q1.dequeue());
q1.enqueue("Drop It Like It's Hot");
while (!q1.isEmpty())
System.out.println(q1.dequeue());
System.out.println();
/*********************************************************
* A queue of integers. Illustrates autoboxing and
* auto-unboxing.
*********************************************************/
}
Queue<Integer> q2 = new Queue<Integer>();
for (int i = 0; i < 10; i++)
q2.enqueue(i);
// test out iterator
for (int i : q2)
StdOut.print(i + " ");
StdOut.println();
// test out dequeue and enqueue
while (q2.size() >= 2) {
int a = q2.dequeue();
int b = q2.dequeue();
int c = a + b;
StdOut.println(c);
q2.enqueue(a + b);
}
}
}
Copyright © 2007, Robert Sedgewick and Kevin Wayne.
Last updated: Mon Nov 19 12:53:50 EST 2007.
Related documents