Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. public class Queue {
  2. private Double[] elements;
  3. private int back;
  4. private static final int FRONT = 0;
  5. public static final int DEFAULT_CAPACITY = 10;
  6. /** Construct a queue with an ad hoc capacity */
  7. /** Construct a queue with the default capacity */
  8. /** put a new object at the end of the queue;
  9. * follow the model of StackOfIntegers to expand the capacity
  10. * if the queue is full
  11. */
  12. int initCapacity;
  13.  
  14. public Queue()
  15. {
  16. initCapacity = DEFAULT_CAPACITY;
  17. elements = new Double [initCapacity];
  18. }
  19.  
  20. public Queue(int newValue){
  21. initCapacity = newValue;
  22. elements = new Double [initCapacity];
  23. }
  24.  
  25.  
  26. public void enqueue(Double value) {
  27. if (back == initCapacity){
  28. initCapacity = initCapacity * 2;
  29. Double [] elements2 = new Double [initCapacity];
  30. for (int i =0; i < elements.length; i++){
  31. elements2[i] = elements[i];
  32. }
  33. elements = elements2;
  34. }
  35. elements[back]= value;
  36. back+=1;
  37.  
  38. }
  39. /** Return and remove the front element from the queue ;
  40. * this method must call the private method shift()
  41. */
  42. public Double dequeue() {
  43. Double temp = elements[FRONT] ;
  44. shift();
  45. back = back - 1;
  46. // we have to temporarily store the current front of the line
  47. return temp;
  48.  
  49. }
  50. /** Return the element at the front of the queue for inspection */
  51. public Double peek() {
  52. return elements[FRONT];
  53. }
  54. /** Test whether the queue is empty */
  55. public boolean isEmpty() {
  56. return back == FRONT;
  57. }
  58. /** Return the number of elements in the queue */
  59. public int getSize() {
  60. return back;
  61. }
  62. /** shift everything one position to the front and null out the
  63. old back of the line */
  64. private void shift() {
  65. for ( int i = 1; i < back; i++ )
  66. elements[i-1] = elements[i] ;
  67. elements[--back] = null ;
  68. }
  69. /** A rough-and-ready way to see what is going on in our queue */
  70. public void dump() {
  71. System.out.print( "Elements: " ) ;
  72. for ( int i = 0; i < elements.length; i++ )
  73. System.out.print( elements[i] + " " ) ;
  74. System.out.println() ;
  75. System.out.println( "Back: " + back ) ;
  76. }
  77. }
  78.  
  79. public class TestQueueOfDoubles {
  80. public static void main(String[] args){
  81. Queue Object = new Queue( 12);
  82. Queue DefaultObject = new Queue();
  83. Queue Object2 = new Queue( 14);
  84. Queue Object3 = new Queue( 9);
  85. System.out.println(Object.initCapacity);
  86. System.out.println(DefaultObject.initCapacity);
  87. System.out.println(Object2.initCapacity);
  88. System.out.println(Object3.initCapacity);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement