java - Priority queue (from scratch) and linked lists -


i trying create linked list unordered raw data (string1...priority10, string2...intpriority2, etc) , have had trouble conceptualizing how can sort write method priority queueing. need method enqueue each object, in order, without using sorting algorithm on final linked list, or using linkedlist or priorityqueue itself.

my enqueue method, nothing hard here:

    public class objectqueue{ object front = null;           //points first element of queue object prev = null;            //points last element of queue    /**  * creates object of object , adds class queue  * @param name of object  * @param rank of priority sort  */ public void enqueue(string name, int rank) {     object current = new object(name, rank);  //uses current entry string name, int rank      if(isempty())               //if empty, add element front     {         front = current;     }     else                        //if elements exist, go end , create new element     {         prev.next = current;     }     prev = current; 

and priority sort , add method i'm having trouble with:

/**  * adds each object , rank on ascending rank basis  * @param filename name of data file  * @throws exc in case of missing file  */ public void addpriority(string filename) throws ioexception {     try     {     file infile = new file(filename);           //inst. file import     scanner read = new scanner(infile);         //inst. scanner object      string name1 = read.next();              //scanner reads next string, primes @ front     int rank1 = read.nextint();              //reads next int, assigns rank      while (read.hasnext())                      //reads until end of text     {         string name2 = read.next();              //next string of next object tested         int rank2 = read.nextint();              //rank test rank1 against          if (rank1 > rank2)                      //if current higher priority test         {             enqueue(name1, rank1);              //enqueue current object             name1 = name2;                      //move test name down current             rank1 = rank2;                      //move test rank down current         }         else         {             enqueue(name2, rank2);              //enqueue current object         }     }     read.close();                               //ends read when empty      }     catch(exception exec)     {         system.out.println("error: file not found.");     }  } 

i need 1 single method either pre-sort objects without sending them list, or sorting them properly, 1 time, while on-the-fly, , i'm running out of ideas.

conceptually (ignoring implementation) priority queue pretty simple. needs able add item priority , needs able item highest (or, in implementations, lowest) priority. additional constraint included 2 items equal priority item added first must retrieved first.

so that's concept. might need provide more details on how priority queue supposed work. following notes i'll assume: - retrieve highest priority first - equal priority should retrieved in insertion order

looking @ implementation, underlying structure collection long insertion allowed , insertion order preserved. traditional implementation heap because use memory efficiently , fast linked list (single or double) fine functionally.

clearly priority queues imply order of retrieval. can implemented @ insertion or retrieval time. again decision dictated usage , again seems implementation can ignore these factors.

so suggestion be, keep simple, sort @ insertion time rather @ retrieval time. without supplying actual code (which i'm assuming task) here's basic algorithm implement insertion time priority queue.

class priorityitem {     private final item item;     private final int priority; }  collection<priorityitem> queue;  void insert(item item, int priority) {     priorityitem element = new priorityitem(item, priority);      if queue not empty {         step through queue {             if current.priority < priority {                 insert element here                 return             }         }     }     add element end queue } 

then retrieval trivial: it's first item in queue.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -