Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.41 KB | None | 0 0
  1. /**
  2.    A class of priority queues represented by a chain of linked nodes.
  3.    
  4.    
  5. */
  6. public class LinkedPriorityQueue<T extends Comparable<? super T>>
  7.              implements PriorityQueueInterface<T>
  8. {
  9.    private Node[] firstNode; // reference to first node of chain and the front
  10.                            // of the priority queue, which has the
  11.                            // highest priority
  12.    private int  length;    // number of entries in chain
  13.  
  14.    public LinkedPriorityQueue(int max)
  15.     {
  16.        firstNode =  (Node[])new LinkedPriorityQueue.Node[max];
  17.        for (int index = 0; index < max; index ++)
  18.            firstNode[index] = null;
  19.       length = 0;
  20.    } // end default constructor
  21.  
  22.    public void add(T newEntry)
  23.    {
  24.       Node newNode = new Node(newEntry, null);
  25.       Node nodeBefore = getNodeBefore(newEntry);
  26.  
  27.       if (isEmpty() || (nodeBefore == null)) // add at beginning
  28.       {
  29.          newNode.setNextNode(firstNode);
  30.          firstNode = newNode;
  31.       }
  32.       else                                   // add after nodeBefore
  33.       {
  34.          Node nodeAfter = nodeBefore.getNextNode();
  35.          newNode.setNextNode(nodeAfter);
  36.          nodeBefore.setNextNode(newNode);
  37.       } // end if
  38.  
  39.       length++;
  40.    } // end add
  41.  
  42.    public T remove()
  43.    {
  44.       return remove(1);  // 1 based, so this is the front
  45.    } // end remove
  46.  
  47.    public T peek()
  48.    {
  49.      return getEntry(1);  // 1 based, so this is the front
  50.    } // end getFront
  51.  
  52.    public boolean isEmpty()
  53.    {
  54.       boolean result;
  55.            
  56.       if (length == 0)
  57.       {
  58.          assert firstNode == null;
  59.          result = true;
  60.       }
  61.       else
  62.       {
  63.          assert firstNode != null;
  64.          result = false;
  65.       } // end if
  66.          
  67.       return result;
  68.    } // end isEmpty
  69.  
  70.    public int getSize()
  71.    {
  72.       return length;
  73.    }
  74.  
  75.    public void clear()
  76.    {
  77.       firstNode = null;
  78.       length = 0;
  79.    } // end clear
  80.  
  81.    public void display()
  82.    {
  83.       Node currentNode = firstNode;
  84.       while (currentNode != null)
  85.       {
  86.          System.out.println(currentNode.getData());
  87.          currentNode = currentNode.getNextNode();
  88.       } // end while  
  89.    } // end display
  90.  
  91.    // Returns either a reference to the node that is before the node
  92.    // that contains or should contain anEntry, or null if
  93.    // no prior node exists (that is, if anEntry belongs at
  94.    // the beginning of the chain)
  95.    private Node getNodeBefore(T anEntry)
  96.    {
  97.      Node currentNode = firstNode;
  98.      Node nodeBefore = null;
  99.      
  100.      while ( (currentNode != null) &&
  101.              (anEntry.compareTo(currentNode.getData()) >= 0) ) // use >= instead of >
  102.      {
  103.         nodeBefore = currentNode;
  104.         currentNode = currentNode.getNextNode();
  105.      } // end while
  106.      
  107.      return nodeBefore;
  108.    } // end getNodeBefore
  109.  
  110.    private Node getNodeAt(int givenPosition)
  111.    {
  112.       assert !isEmpty() && (1 <= givenPosition) && (givenPosition <= length);
  113.       Node currentNode = firstNode;
  114.  
  115.       // traverse the chain to locate the desired node
  116.       for (int counter = 1; counter < givenPosition; counter++)
  117.          currentNode = currentNode.getNextNode();
  118.  
  119.       assert currentNode != null;
  120.       return currentNode;
  121.    } // end getNodeAt
  122.  
  123.    private T getEntry(int givenPosition)
  124.    {
  125.       T result = null;  // result to return
  126.      
  127.       if ((givenPosition >= 1) && (givenPosition <= length))
  128.       {
  129.          assert !isEmpty();
  130.          result = getNodeAt(givenPosition).getData();
  131.       } // end if
  132.          
  133.       return result;
  134.    } // end getEntry
  135.  
  136.    private T remove(int givenPosition)
  137.    {
  138.       T result = null;                          // return value
  139.  
  140.       if ((givenPosition >= 1) && (givenPosition <= length))
  141.       {
  142.          assert !isEmpty();
  143.  
  144.          if (givenPosition == 1)                 // case 1: remove first entry
  145.          {
  146.             result = firstNode.getData();        // save entry to be removed
  147.             firstNode = firstNode.getNextNode();
  148.          }
  149.          else                                    // case 2: givenPosition > 1
  150.          {
  151.             Node nodeBefore = getNodeAt(givenPosition - 1);
  152.             Node nodeToRemove = nodeBefore.getNextNode();
  153.             Node nodeAfter = nodeToRemove.getNextNode();
  154.             nodeBefore.setNextNode(nodeAfter);   // disconnect the node to be removed
  155.             result = nodeToRemove.getData();     // save entry to be removed
  156.          } // end if
  157.  
  158.          length--;
  159.         } // end if
  160.          
  161.         return result;
  162.    } // end remove
  163.  
  164.    private class Node
  165.    {
  166.       private T    data; // entry in list
  167.       private Node next; // link to next node
  168.  
  169.       private Node(T dataPortion)
  170.       {
  171.          data = dataPortion;
  172.          next = null;  
  173.       } // end constructor
  174.    
  175.       private Node(T dataPortion, Node nextNode)
  176.       {
  177.          data = dataPortion;
  178.          next = nextNode;  
  179.       } // end constructor
  180.      
  181.       private T getData()
  182.       {
  183.          return data;
  184.       } // end getData
  185.      
  186.       private void setData(T newData)
  187.       {
  188.          data = newData;
  189.       } // end setData
  190.      
  191.       private Node getNextNode()
  192.       {
  193.          return next;
  194.       } // end getNextNode
  195.      
  196.       private void setNextNode(Node nextNode)
  197.       {
  198.          next = nextNode;
  199.       } // end setNextNode
  200.    } // end Node
  201. } // end LinkedPriorityQueue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement