Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,307 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 787 people online right now. Registration is fast and FREE... Join Now!




Prioritized Queue

 
Reply to this topicStart new topic

Prioritized Queue

projeckt
15 Nov, 2008 - 01:15 AM
Post #1

New D.I.C Head
*

Joined: 15 Nov, 2008
Posts: 10

.

This post has been edited by projeckt: 15 Nov, 2008 - 10:36 AM

User is offlineProfile CardPM
+Quote Post


projeckt
RE: Prioritized Queue
15 Nov, 2008 - 01:16 AM
Post #2

New D.I.C Head
*

Joined: 15 Nov, 2008
Posts: 10


Created a Prioritized Queue. Below is how far I got. Need guidance for my enqueue, positionOf, removeAll, and clear methods.

Essentially for my enqueue I want to associate a priority number with whatever item I decide to add to the list. removeAll - I got lost along the way.
positionOf and clear I need a hand in.

CODE



/**
    Prioritized queue class
    

    
    @version 1.0
*/
import java.util.NoSuchElementException;

public class PrioritizedQueue<T> {

    private static class Node<T> {
        public T data;
        public int priority;
        public Node <T> link;
    }

    private Node<T> head, last, cursor;
    private int count;

    /**
        Construct an empty queue
    */
    public PrioritizedQueue() {
        head = last = new Node<T>();
        cursor = null;
        count = 0;
        head.data = null;
        head.link = null;
    }

    /**
        Add specified item to the rear of this queue, subject to the
        specified priority; lower priority numbers have preference
        [Runs in O(n) time]

        @param item  the item to be added
    */
    public void enqueue(T item, int priority) {
        last.link = new Node<T>();
        last = last.link;
        last.priority = priority;
        last.data = item;
        count++;
    }

    /**
        Remove and return the item at the front of this queue
        Throw NoSuchElementException if queue is empty
        [Runs in O(1) time]

        @param item  the item to be inserted
    */
    public T dequeue(T item) {
        if (isEmpty()) {
     throw new NoSuchElementException();
        }
        T result = head.link.data;
        head.link = head.link.link;
        count--;
        if (count == 0) {
           last = head;
        }
        return result;
    }

    /**
        Determine the number of elements in the queue
        [Runs in O(1) time]

        @return the number of elements in the queue
    */
    public int size() {
        return count;
    }

    /**
        Is the queue empty?
        [Runs in O(1) time]

        @return true if the queue is empty, false otherwise
    */
    public boolean isEmpty() {
       return size() == 0;
    }

    /**
        Return the position of the specified item in this queue,
        if found, and -1 if not found.
        Position 0 is the front of the queue
        [Runs in O(n) time]

        @param item  the item to locate
        @return the position of the item, or -1
    */
   int positionOf(T item) {
     return;
    }

    /**
        Clear the queue so that it is now empty
        [Runs in O(1) time]
    */
    public void clear() {
        head = null;
        count = 0;
        cursor = null;
    }

    /**
        Remove the first occurence of the specified item from the queue

     Throw NoSuchElementException if item is not in queue
        [Runs in O(n) time]

        @param item  the item to remove
    */
    public void remove(T item) {
        Node<T> prev = head;
        Node<T> curr = head.link;
        while (curr != null) {
            if (item.equals(curr.data)) {
                prev.link = curr.link;
                if (curr == last ) {
                    last = prev;
                }
               count--;
               return;
            }
            curr = curr.link;
            prev = prev.link;
        }
        throw new NoSuchElementException();
    }

    /**
        Remove all occurences of the specified item from the queue
        Throw NoSuchElementException if item is not in queue
        [Runs in O(n) time]

      @param item  the item to remove
    */
    public void removeAll(T item) {
        Node<T> curr = head.link;
        Boolean found = false;
        while (curr != head) {
            if (item.equals(curr.data)) {
                  found = true;
                  curr.link = null;
                  count--;
            }
            curr = curr.link;
        }
        if(!found){
                  throw new NoSuchElementException();
        }

     }

    /**
        Get the first element in the queue; set the cursor for iteration
        [Runs in O(1) time]

        @return  the first element in the last; null if queue is empty
    */
    public T first() {
        cursor = head.link;
        if (cursor == null) {
        return null;
        }
        return cursor.data;
    }

    /**
        Get the next element in the queue, relative to a previous call
        [Runs in O(1) time]

        @return the next element in the queue; null if no more elements
    */
    public T next() {
        cursor = cursor.link;
        if (cursor == null) {
           return null;
        }
        return cursor.data;
    }

}




User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Prioritized Queue
15 Nov, 2008 - 08:34 AM
Post #3

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 3,813



Thanked: 203 times
Dream Kudos: 1325
My Contributions
Merged smile.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Prioritized Queue
15 Nov, 2008 - 02:05 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,954



Thanked: 673 times
Dream Kudos: 200
My Contributions
Do your priorities have a predifed range (like 0 to 10) or they can go from -9999999 to +9999999 ?
User is online!Profile CardPM
+Quote Post

projeckt
RE: Prioritized Queue
15 Nov, 2008 - 05:39 PM
Post #5

New D.I.C Head
*

Joined: 15 Nov, 2008
Posts: 10

QUOTE(pbl @ 15 Nov, 2008 - 02:05 PM) *

Do your priorities have a predifed range (like 0 to 10) or they can go from -9999999 to +9999999 ?


They can go from -9999999 to +9999999. Lower priorities will be closer to the front of the queue.
User is offlineProfile CardPM
+Quote Post

projeckt
RE: Prioritized Queue
16 Nov, 2008 - 08:08 PM
Post #6

New D.I.C Head
*

Joined: 15 Nov, 2008
Posts: 10

QUOTE(projeckt @ 15 Nov, 2008 - 05:39 PM) *

QUOTE(pbl @ 15 Nov, 2008 - 02:05 PM) *

Do your priorities have a predifed range (like 0 to 10) or they can go from -9999999 to +9999999 ?


They can go from -9999999 to +9999999. Lower priorities will be closer to the front of the queue.



Added a merge method to order the list. Still having trouble with incorporating it with my enqueue.. positionOf, clear and removeall not so much headway either.


CODE
/**
    Prioritized queue class
    @version 1.0
*/
import java.util.NoSuchElementException;

public class PrioritizedQueue<T extends Comparable<T>> {

    private static class Node<T> {
        public T data;
        public int priority;
        public Node <T> link;
    }

    private Node<T> head, last, cursor;
    private int count;

    /**
        Construct an empty queue
    */
    public PrioritizedQueue() {
        head = last = new Node<T>();
        cursor = null;
        count = 0;
        head.data = null;
        head.link = null;
    }  /**
        Add specified item to the rear of this queue, subject to the
        specified priority; lower priority numbers have preference
        [Runs in O(n) time]

        @param item  the item to be added
    */
    public void enqueue(T item, int priority) {
        last.link = new Node<T>();
        last = last.link;
        last.priority = priority;
        last.data = item;
        count++;
    }

   public PrioritizedQueue<T> merge(PrioritizedQueue<T> a, PrioritizedQueue<T> b){
            PrioritizedQueue<T> result = new PrioritizedQueue<T>();
            T aItem = a.first();
            T bItem = b.first();
            while (aItem != null && bItem != null) {
                int c = aItem.compareTo(bItem);
                if (c == 0) {
                   result.enqueue(aItem, last.priority);
                   aItem = a.next();
                   bItem = b.next();
                } else if (c < 0) {
                   result.enqueue(aItem, last.priority);
                   aItem = a.next();
                } else {
                   result.enqueue(bItem, last.priority);
                   bItem = b.next();
                }
           }
           while (aItem != null) {
               result.enqueue(aItem, last.priority);
               aItem = a.next();
           }
           while (bItem != null) {
               result.enqueue(bItem, last.priority);
               bItem = b.next();
}
           return  result;
        }




    /**
        Remove and return the item at the front of this queue
        Throw NoSuchElementException if queue is empty
        [Runs in O(1) time]

        @param item  the item to be inserted
    */
    public T dequeue(T item) {
        if (isEmpty()) {
           throw new NoSuchElementException();
        }
        T result = head.link.data;
        head.link = head.link.link;
        count--;
        if (count == 0) {
           last = head;
        }
        return result;
    }

    /**
     Determine the number of elements in the queue
        [Runs in O(1) time]

        @return the number of elements in the queue
    */
    public int size() {
        return count;
    }

    /**
        Is the queue empty?
        [Runs in O(1) time]

        @return true if the queue is empty, false otherwise
    */
    public boolean isEmpty() {
        return size() == 0;
    }

    /**
        Return the position of the specified item in this queue,
        if found, and -1 if not found.
        Position 0 is the front of the queue
        [Runs in O(n) time]

        @param item  the item to locate
        @return the position of the item, or -1
    */
  int positionOf(T item) {
   return;
}

    /**
        Clear the queue so that it is now empty
        [Runs in O(1) time]
    */
    public void clear() {
        head = null;
        count = 0;
        cursor = null;
    }

    /**
        Remove the first occurence of the specified item from the queue
        Throw NoSuchElementException if item is not in queue
        [Runs in O(n) time]

        @param item  the item to remove
    */
    public void remove(T item) {
        Node<T> prev = head;
        Node<T> curr = head.link;
        while (curr != null) {
            if (item.equals(curr.data)) {
                prev.link = curr.link;
                if (curr == last ) {

                  last = prev;
                }
               count--;
               return;
            }
            curr = curr.link;
            prev = prev.link;
        }
        throw new NoSuchElementException();
    }

    /**
        Remove all occurences of the specified item from the queue
        Throw NoSuchElementException if item is not in queue
        [Runs in O(n) time]

        @param item  the item to remove
    */
    public void removeAll(T item) {
        Node<T> curr = head.link;
        Boolean found = false;
        while (curr != head) {
            if (item.equals(curr.data)) {
                  found = true;
                  curr.link = curr.link.link;
                  count--;
            }
            curr = curr.link;
   }
        if(!found){
                  throw new NoSuchElementException();
        }

     }

    /**
        Get the first element in the queue; set the cursor for iteration
        [Runs in O(1) time]

        @return  the first element in the last; null if queue is empty
    */
    public T first() {
        cursor = head.link;
        if (cursor == null) {
           return null;
        }
        return cursor.data;
    }

    /**
        Get the next element in the queue, relative to a previous call
        [Runs in O(1) time]

        @return the next element in the queue; null if no more elements
    */
    public T next() {
    cursor = cursor.link;
        if (cursor == null) {
           return null;
        }
        return cursor.data;
    }

}









User is offlineProfile CardPM
+Quote Post

pbl
RE: Prioritized Queue
16 Nov, 2008 - 08:19 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,954



Thanked: 673 times
Dream Kudos: 200
My Contributions
I hope this is a school assigment
In REAL life nobody will think to implement such an heavy duty time consuming algorithm

All the priority queues I have seen (in 35 years) had a range of priority
Then it is easy

- you implement your Queue (withour priority)

test your Queue method form A to Z and make it bullit proof

when your Queue class works... then think of PriorityQueue let say from 0 to 10
Your PriorityQueue class would have an array of 10 of your previouly tested Queus class

so when priorityQueue.insert(priority....
you add into queue[priority]

and when you retreive you just scan you array of queues for the first one (ordered by priority) that has an element

Again, if it is for a school assigment you can still continue to fudge around... but in real life that would be the implementation
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 06:54PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month