//computes all primes up to some n and store them the queue public void computeTo(int n) { // add int 2:n into queue int i=2; int count=0; int value1=0; int value2=0; int size=0; int whilesize=(int)Math.sqrt(n); while(i<=n)// n=10 { queueNumbers.enqueue(i);//2 3 4 5 6 7 8 9 10 i++; } do{ value1= (int)queueNumbers.dequeue(); //value1=2, queuePrime.enqueue(value1);// 2 for (int count=0; count<queueNumbers.getsize(); count++)// 0-10 { value2=(int)queueNumber.dequeue();// 3 if(value2%value1 !=0)//3%2 !=0 { queueNumbers.enqueue(value2);// store 3 in queueNumbers, } } }// end do while(value2 <= whilesize) { size= queueNumbers.getsize(); for(int k=0; i<size; i++) { queueNumbers.dequeue(value2); queuePrime.enqueue(value2); } }// end while /** create a queue and fill it with the consecutive integers 2 through n inclusive. create an empty queue to store primes. do { obtain the next prime by removing the first value in the queue of numbers. put the next prime into the queue of primes. go through the queue of numbers, eliminating numbers divisible by the next prime. } while (the next prime < sqrt(n)) all remaining values in numbers queue are prime, so transfer them to primes queue */ }
/** * 处理下一个 * * @param identity */ public void next(String identity) { Queue queue = _queues.get(identity); if (queue != null) { queue.isRunning = false; queue.dequeue(); } }
public void bfs(int s) { Queue<Integer> q = new Queue<Integer>(); distTo[s] = 0; marked[s] = true; q.enqueue(s); while (!q.isEmpty()) { int v = q.dequeue(); for (Route r : adj[v]) { int w = r.other(cities[v]).id() - 1; // index of other city on route if (!marked[w]) { edgeTo[w] = r; distTo[w] = distTo[v] + 1; marked[w] = true; q.enqueue(w); } } } }
public static void main(String args[]) { // Queue q = new Queue(); Queue<Integer> q = new LinkedList<Integer>(); q.enqueue(20); q.enqueue(30); q.enqueue(40); System.out.println(q); q.dequeue(); q.dequeue(); System.out.println(st); Queue<String> collection = new LinkedList<String>(); collection.enqueue("COMMON MAN"); collection.enqueue("VIP"); collection.enqueue("VVIP"); System.out.println(collection); collection.enqueue(); collection.enqueue(); System.out.println(collection); for (String s : collection) System.out.println(s); }