Advertisement
raffaelegriecoit

queue

Jun 13th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. class MyQueue {
  2.  
  3. int[] queue;
  4. int head;
  5. int tail;
  6.  
  7. public MyQueue() {
  8. queue = new int[10];
  9. head = 0;
  10. tail = 0;
  11. }
  12.  
  13. public void push(int x) {
  14. if(tail < queue.length){
  15. queue[tail] = x;
  16. tail++;
  17. }
  18. else{
  19. System.out.println("The queue is full");
  20. }
  21. }
  22.  
  23. public int pop() {
  24. if(head < tail){
  25. int elemento = queue[head];
  26. head++;
  27. return elemento;
  28. }
  29. else{
  30. System.out.println("The queue is empty");
  31. return -1;
  32. }
  33. }
  34.  
  35. public int peek() {
  36. if(head < tail){
  37. return queue[head];
  38. }
  39. else {
  40. System.out.println("The queue is empty");
  41. return -1;
  42. }
  43. }
  44.  
  45. public boolean empty() {
  46. if(head >= tail){
  47. return true;
  48. }
  49. else{
  50. return false;
  51. }
  52. }
  53. }
  54.  
  55. /**
  56. * Your MyQueue object will be instantiated and called as such:
  57. * MyQueue obj = new MyQueue();
  58. * obj.push(x);
  59. * int param_2 = obj.pop();
  60. * int param_3 = obj.peek();
  61. * boolean param_4 = obj.empty();
  62. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement