473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

could you help me in java in priority queue

hello everyone could you teach how to let my priority queue let the user input
i don't know because i always have default in it but could teach me how to let the user input
Expand|Select|Wrap|Line Numbers
  1. public class PriorityQ {
  2.  
  3.   private int maxSize;
  4.  
  5.   private long[] queArray;
  6.  
  7.   private int nItems;
  8.  
  9.   public PriorityQ(int s) {
  10.     maxSize = s;
  11.     queArray = new long[maxSize];
  12.     nItems = 0;
  13.   }
  14.  
  15.   public void insert(long item) {
  16.     int i;
  17.  
  18.     if (nItems == 0)
  19.       queArray[nItems++] = item; 
  20.     else 
  21.     {
  22.       for (i = nItems - 1; i >= 0; i--) 
  23.       {
  24.         if (item > queArray[i]) 
  25.           queArray[i + 1] = queArray[i]; 
  26.         else
  27.  
  28.           break; /
  29.       }
  30.       queArray[i + 1] = item; 
  31.       nItems++;
  32.     } 
  33.   }
  34.  
  35.   public long remove(){
  36.     return queArray[--nItems];
  37.   }
  38.  
  39.   public long peekMin(){
  40.     return queArray[nItems - 1];
  41.   }
  42.  
  43.   public boolean isEmpty(){
  44.     return (nItems == 0);
  45.   }
  46.  
  47.   public boolean isFull(){
  48.     return (nItems == maxSize);
  49.   }
  50.   public static void main(String[] args) {
  51.     PriorityQ thePQ = new PriorityQ(5);
  52.     thePQ.insert(30);
  53.     thePQ.insert(50);
  54.     thePQ.insert(10);
  55.     thePQ.insert(40);
  56.     thePQ.insert(20);
  57.  
  58.     while (!thePQ.isEmpty()) {
  59.       long item = thePQ.remove();
  60.       System.out.print(item + " ");
  61.     }
  62.     System.out.println("");
  63.   }
  64. }
  65.  
Feb 27 '10 #1
1 1574
jkmyoung
2,057 Expert 2GB
Are you using Java 1.5 or above?
Try looking at the Java Scanner Class
http://www.ensta.fr/~diam/java/onlin...l/Scanner.html
Mar 1 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

38
by: Aaron W. LaFramboise | last post by:
Hello, I understand that an easy way to make the standard std::priority_queue stable is by including an integer stamp with each node that is incremented each time a new node is pushed into the...
6
by: Der Andere | last post by:
Are priority queues implemented in the STL in Visual Studio 6? If no, which is the easiest way to simulate them? Multisets? BTW: The container contains pointers to instances of a class. The...
5
by: Dan H. | last post by:
Hello, I have implemented a C# priority queue using an ArrayList. The objects being inserted into the priority queue are being sorted by 2 fields, Time (ulong) and Priority (0-100). When I...
3
by: Erik | last post by:
Hi Everyone, I was thinking of how to do this for a while now and I cant get my head round it so I though I'd ask the experts! :-) What I am doing is reading in large amounts of data over TCP...
16
by: Crirus | last post by:
hello I read somewhere about priority queue...what is that and Vb net have such class? -- Ceers, Crirus ------------------------------ If work were a good thing, the boss would take it...
4
by: vfunc | last post by:
Is the STL priority queue a proper implementation of a heap with siftup algorithm etc ? How do you implement an STL priority queue (link to an example) ? Is there a resort method, why ? Thanks
3
by: PicO | last post by:
i need some explanation about the difference between priority queue & set & heap ... as they all sort the data in ( n log n ) ... but the only i see that priority queue only can pop the top (...
4
by: jjh5030 | last post by:
This is a programming assignment. You are asked to work with pointers. Be aware that error messages are often not very helpful when your pointers point to bad locations. Therefore, reserve...
24
by: Joe, G.I. | last post by:
Can anyone help me w/ a priority_queue. I'm generating MyEvent classes and I put them on a priority_queue, but I don't know how to get them in priority. The priority is the event w/ the smallest...
14
by: AlarV | last post by:
Hello everyone, here is my problem. I have to make a dynamic priority queue,which is a heap, and my book isn't helpful at all, so I have no clues on how to create it.. I read something like a static...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.