Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. //@author kevinc3
  2. public class Queue {
  3. private double[] queue = new double[0];
  4. private int size = 0;
  5. /** Adds the value to the front of the queue.
  6. * Note the queue automatically resizes as more items are added. */
  7. public void add(double value) {
  8. size ++;
  9. double[] old = queue;
  10. queue = new double[size];
  11. queue[0] = value;
  12. for (int i = 1; i < queue.length; i++){
  13. queue[i] = old[i-1];
  14. }
  15. }
  16. /** Removes the value from the end of the queue. If the queue is empty, returns 0 */
  17. public double remove() {
  18. if (size == 0){
  19. return 0.0;
  20. }
  21. size --;
  22. double[] old = queue;
  23. for (int i = 0; i < size; i++){
  24. queue[i] = old[i];
  25. }
  26. return queue[size];
  27. }
  28.  
  29. /** Returns the number of items in the queue. */
  30. public int length() {
  31. return size;
  32. }
  33.  
  34. /** Returns true iff the queue is empty */
  35. public boolean isEmpty() {
  36. if(size == 0){
  37. return true;
  38. }
  39. return false;
  40. }
  41.  
  42. /** Returns a comma separated string representation of the queue. */
  43. public String toString() {
  44. String result = "";
  45. result = result + this.queue[this.size-1];
  46. for (int i = this.size - 2; i >= 0; i--){
  47. result = result + "," + this.queue[i];
  48. }
  49. return result;
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement