诚心求教:Java 转 Ruby

诚心求教:Java 转 Ruby

对ruby还不是太熟悉, 想通过把java改成ruby来多了解一些ruby,望高人指教, 多谢!!!

/**
* An interface for collections of Strings.
*/
public interface Worklist {
 /**
 * Add one String to the worklist.
 * @param item the String to add
 */
 void add(String item);

 /**
 * Test whether there are more elements in the
 * worklist; that is, test whether more elements have
 * been added than have been removed.
 * @return true if there are more elements
 */
 boolean hasMore();

 /**
 * Remove one String from the worklist and return it.
 * There must be at least one element in the worklist.
 * @return the String item removed
 */
 String remove();
}

/**
* A Node is an object that holds a String and a php?name=link" onclick="tagshow(event)" class="t_tag">link
* to the next Node. It can be used to build linked
* lists of Strings.
*/
public class Node {
 private String data; // Each node has a String...
 private Node link; // ...and a link to the next Node

 /**
 * Node constructor.
 * @param theData the String to store in this Node
 * @param theLink a link to the next Node
 */
 public Node(String theData, Node theLink) {
  data = theData;
  link = theLink;
 }
 
 /**
 * Accessor for the String data stored in this Node.
 * @return our String item
 */
 public String getData() {
  return data;
 }
 
 /**
 * Accessor for the link to the next Node.
 * @return the next Node
 */
 public Node getLink() {
  return link;
 }
}

/**
* A Stack is an object that holds a collection of
* Strings.
*/
public class Stack implements Worklist {
 private Node top = null; // The top Node in the stack
 /**
 * Push a String on top of this stack.
 * @param data the String to add
 */
 public void add(String data) {
  top = new Node(data,top);
 }
 /**
 * Test whether this stack has more elements.
 * @return true if this stack is not empty
 */
 public boolean hasMore() {
  return (top != null);
 }
 /**
 * Pop the top String from this stack and return it.
 * This should be called only if the stack is
 * not empty.
 * @return the popped String
 */
 public String remove() {
  Node n = top;
  top = n.getLink();
  return n.getData();
 }
}

public class Queue implements Worklist {

   private Node head;
   private Node tail;

   public Queue() {
      head = null;
      tail = null;
   }

   public boolean hasMore() {
      return (head!=null);
   }
   public void add(String item) {
      if (head==null)
      {
        head =new Node(item,null);
        tail=head;
      }
      else {
        tail=new Node(item, tail);
      }
   }

   public String remove() {
      if(tail==null) return "Wrong Remove";
      if (tail == head) {
       String x=head.getData();
        head = null;
        tail = null;

        return x;
      }
      Node n=tail;
      while(n.getLink()!=head) n=n.getLink();
      String x=head.getData();
      head=n;
      return x;
   }

class WorkList, Node, Stack 来自网络, class Queue为原创
欢迎加入我们。

请你把你的帖子发到Ruby语言区,自有高手和版主们来帮你的。

谢谢!