Queue.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 5k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    Queue.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.core;
  22. import java.io.*;
  23. /** 
  24.  * Class representing a FIFO queue.
  25.  *
  26.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  27.  * @version $Revision: 1.6 $
  28.  */
  29. public class Queue extends Object implements Serializable {
  30.   /**
  31.    * Represents one node in the queue.
  32.    */
  33.   protected class QueueNode implements Serializable {
  34.     /** The next node in the queue */
  35.     protected QueueNode m_Next;
  36.     /** The nodes contents */
  37.     protected Object m_Contents;
  38.     /** 
  39.      * Creates a queue node with the given contents 
  40.      */
  41.     public QueueNode(Object contents) {
  42.       m_Contents = contents;
  43.       next(null);
  44.     }
  45.     /**
  46.      * Sets the next node in the queue, and returns it.
  47.      */
  48.     public QueueNode next(QueueNode next) {
  49.       return m_Next = next;
  50.     }
  51.     /**
  52.      * Gets the next node in the queue. 
  53.      */
  54.     public QueueNode next() {
  55.       return m_Next;
  56.     }
  57.     /**
  58.      * Sets the contents of the node.
  59.      */
  60.     public Object contents(Object contents) {
  61.       return m_Contents = contents;
  62.     }
  63.     /**
  64.      * Returns the contents in the node.
  65.      */
  66.     public Object contents() {
  67.       return m_Contents;
  68.     }
  69.   }
  70.   /** Store a reference to the head of the queue */
  71.   protected QueueNode m_Head = null;
  72.   /** Store a reference to the tail of the queue */
  73.   protected QueueNode m_Tail = null;
  74.   /** Store the current number of elements in the queue */
  75.   protected int m_Size = 0;
  76.   /**
  77.    * Removes all objects from the queue.
  78.    */
  79.   public final synchronized void removeAllElements() {
  80.     m_Size = 0;
  81.     m_Head = null;
  82.     m_Tail = null;
  83.   }
  84.   /**
  85.    * Appends an object to the back of the queue.
  86.    *
  87.    * @param item the object to be appended
  88.    * @return the object appended
  89.    */
  90.   public synchronized Object push(Object item) {
  91.     QueueNode newNode = new QueueNode(item);
  92.     if (m_Head == null) {
  93.       m_Head = m_Tail = newNode;
  94.     } else {
  95.       m_Tail = m_Tail.next(newNode);
  96.     }
  97.     m_Size++;
  98.     return item;
  99.   }
  100.   /**
  101.    * Pops an object from the front of the queue.
  102.    *
  103.    * @return the object at the front of the queue
  104.    * @exception RuntimeException if the queue is empty
  105.    */
  106.   public synchronized Object pop() {
  107.     if (m_Head == null) {
  108.       throw new RuntimeException("Queue is empty");
  109.     }
  110.     Object retval = m_Head.contents();
  111.     m_Size--;
  112.     m_Head = m_Head.next();
  113.     if (m_Head == null) {
  114.       m_Tail = null;
  115.     }
  116.     return retval;
  117.   }
  118.   /**
  119.    * Gets object from the front of the queue.
  120.    *
  121.    * @return the object at the front of the queue
  122.    * @exception RuntimeException if the queue is empty
  123.    */
  124.   public synchronized Object peek() {
  125.     
  126.     if (m_Head == null) {
  127.       throw new RuntimeException("Queue is empty");
  128.     }
  129.     return m_Head.contents();
  130.   }
  131.   /**
  132.    * Checks if queue is empty.
  133.    * 
  134.    * @return true if queue is empty
  135.    */
  136.   public boolean empty() {
  137.     return (m_Head == null);
  138.   }
  139.   /** 
  140.    * Gets queue's size.
  141.    *
  142.    * @return size of queue
  143.    */
  144.   public int size() {
  145.     return m_Size;
  146.   }
  147.   /**
  148.    * Produces textual description of queue.
  149.    *
  150.    * @return textual description of queue
  151.    */
  152.   public String toString() {
  153.     String retval = "Queue Contents "+m_Size+" elementsn";
  154.     QueueNode current = m_Head;
  155.     if (current == null) {
  156.       return retval + "Emptyn";
  157.     } else {
  158.       while (current != null) {
  159. retval += current.contents().toString()+"n";
  160. current = current.next();
  161.       }
  162.     }
  163.     return retval;
  164.   }
  165.   /**
  166.    * Main method for testing this class.
  167.    *
  168.    * @param argv a set of strings that are pushed on a test queue
  169.    */
  170.   public static void main(String [] argv) {
  171.     try {
  172.       Queue queue = new Queue();
  173.       for(int i = 0; i < argv.length; i++) {
  174. queue.push(argv[i]);
  175.       }
  176.       System.out.println("After Pushing");
  177.       System.out.println(queue.toString());
  178.       System.out.println("Popping...");
  179.       while (!queue.empty()) {
  180. System.out.println(queue.pop().toString());
  181.       }
  182.     } catch (Exception ex) {
  183.       System.out.println(ex.getMessage());
  184.     }
  185.   }
  186. }