Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1.  
  2. public class PriorityQueue implements Worklist {
  3.     private Node first = null; // first element of the Queue
  4.     private Node last = null; // last element of the Queue
  5.     private int size = 0;
  6.  
  7.     @Override
  8.     public void add(String data) {
  9.         Node tempNode = new Node(data, first);
  10.  
  11.         if (last == null) {
  12.             first = tempNode;
  13.             last = tempNode;
  14.         } else if (data.compareTo(last.getData()) <= 0){
  15.             last = tempNode;
  16.             last.link = tempNode;
  17.            
  18.         } else {
  19.             Node swap = last;
  20.             remove();
  21.             last = tempNode;
  22.             last.link = swap;
  23.            
  24.             /*Node swap = new Node(last.getData(), last);
  25.             remove();
  26.             last = tempNode;
  27.             last.link = swap;*/
  28.         }
  29.         size++;
  30.     }
  31.  
  32.     @Override
  33.     public boolean hasMore() {
  34.         return (first != null);
  35.     }
  36.  
  37.     @Override
  38.     public String remove() {
  39.         if (size == 0) {
  40.             first = null;
  41.             last = null;
  42.         } else if (hasMore()){
  43.             Node temp = first;
  44.             first = first.getLink();
  45.             size--;
  46.             return temp.getData();
  47.         }
  48.         return null;
  49.  
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement