Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. public class MyQueue {
  2.  
  3. private float[] arrayQueue;
  4. private int start;
  5. private int count;
  6.  
  7. MyQueue(int size) {
  8. arrayQueue = new float[size];
  9. for (int i = 0; i < size; i++) {
  10. arrayQueue[i] = -1;
  11. }
  12. start = count = 0;
  13. }
  14.  
  15. private void changeSize(int size) {
  16. System.out.println("---- Changing queue size = " + size);
  17. float[] tmp = new float[size];
  18.  
  19. for (int i = 0; i < count; i++) {
  20. tmp[i] = arrayQueue[(start + i) % arrayQueue.length];
  21. }
  22. for (int i = count; i < size; i++) {
  23. tmp[i] = -1;
  24. }
  25. start = 0;
  26. arrayQueue = tmp;
  27. }
  28.  
  29.  
  30. public boolean put(float value) {
  31. if (count == arrayQueue.length) {
  32. changeSize(arrayQueue.length * 2);
  33. }
  34. arrayQueue[(start + count) % arrayQueue.length] = value;
  35. count++;
  36. return true;
  37. }
  38.  
  39. public float get() throws RuntimeException {
  40. if (count == 0) {
  41. throw new RuntimeException("Queue is empty!");
  42. } else {
  43. float returnValue = arrayQueue[start];
  44. arrayQueue[start] = -1;
  45. start = (start + 1) % arrayQueue.length;
  46. if (--count <= (int)(arrayQueue.length * 0.25)) {
  47. changeSize(Math.max(1, arrayQueue.length / 4));
  48. }
  49. return returnValue;
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement