Universität Paderborn - Home Universität Paderborn
Die Universität der Informationsgesellschaft

Parallel Programming WS 2014/2015 - File Queue.java

public class Queue {

   private Node head; // the oldest queue element, if any;
   private Node tail; // the youngest queue element, if any

   static class Node {
      Object data;
      Node link;
      Node(Object elem, Node tail) {
            data = elem;
            link = tail;
      }
   }

   public boolean empty() {
      return head == null;
   }

   public void enqueue(Object elem) {
      Node x = new Node(elem, null);

      if (head == null) {
            head = x;
            tail = x;
      } else {
            tail.link = x;
            tail = tail.link;
      }
   }

   public void dequeue() {
      if (head != null) {
            head = head.link;
      }
      if (head == null) {
            tail = null;
      }
   }

   public Object front() {
      if (head != null) {
            return head.data;
      } else {
            return null;
      }
   }
}

Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 26.01.2015