Easy Coding
  Forum Wiki Tagging Projekte Karte RSS
» Start
» All Recent Changes
» Wiki Suche
» Wiki Hilfe

Coder How To's

Algorithmen Informationen

edit SideBar

Neue Wiki Eintrage finden Sie unter easy-coding.de/wiki.

Priority Queue

PriorityQueue.java
  1. /**
  2. * Klasse einer prioritaets-Warteschlange
  3. */
  4. public class ListPriorityQueue {
  5.  
  6.         Node head, tail;
  7.  
  8.         /**
  9.          * Klasse von Knoten fuer eine double-Linked-Queue
  10.          */
  11.         private class Node implements Comparable {
  12.                 Node next, prev;
  13.                 int data;
  14.  
  15.                 /**
  16.                  * Vergleicht den Inhalt zweier Knoten
  17.                  * @param o Object (Hier:Integer)
  18.                  * @return int
  19.                  */
  20.                 public int compareTo(Object o) {
  21.                         if (this.data > (Integer) o)
  22.                                 return 1;
  23.                         else if (this.data < (Integer) o)
  24.                                 return -1;
  25.                         else
  26.                                 return 0;
  27.                 }
  28.         }
  29.  
  30.         /**
  31.          * Konstruktor
  32.          */
  33.         public ListPriorityQueue() {
  34.                 this.head = new Node();
  35.                 this.tail = new Node();
  36.  
  37.                 head.prev = tail;
  38.                 head.next = null;
  39.                 tail.prev = null;
  40.                 tail.next = head;
  41.         }
  42.  
  43.         /**
  44.          * fuegt, abhaengig von der Groesse des Inhalts, neuen Knoten in die
  45.          * Schlange ein.
  46.          *
  47.          * @param i int
  48.          */
  49.         public void enter(int i) {
  50.                 // erzeugt neuen Knoten
  51.                 Node n = new Node();
  52.                 n.data = i;
  53.                 Node search = tail;
  54.  
  55.                 // sucht richtige Stelle zum einfuegen, abhaengig von prioritaet
  56.                 while (search.next.next != null) {
  57.                         if (search.next.data > i)
  58.                                 break;
  59.                         else
  60.                                 search = search.next;
  61.                 }
  62.  
  63.                 // fuegt an richtiger Stelle den Knoten ein
  64.                 n.next = search.next;
  65.                 n.prev = search;
  66.                 search.next.prev = n;
  67.                 search.next = n;
  68.         }
  69.  
  70.         /**
  71.          * entfernt Knoten am head
  72.          */
  73.         public void leave() throws QueueException {
  74.                 if (tail.next.next == null)
  75.                         throw new QueueException("PriorityQueue empty!");
  76.                 else {
  77.                         head.prev.prev.next = head;
  78.                         head.prev = head.prev.prev;
  79.                 }
  80.         }
  81.  
  82.         /**
  83.          * gibt Wert des ersten Knotes am head zurueck
  84.          */
  85.         public int front() throws QueueException {
  86.                 return head.prev.data;
  87.         }
  88.  
  89.         /**
  90.          * prueft, ob Queue leer ist
  91.          */
  92.         public boolean is_empty() {
  93.                 return tail.next.next == null;
  94.         }
  95. }
Zuletzt geändert am 28.09.2006 14:17 Uhr
  Impressum