Guest User

Queue

a guest
Apr 27th, 2021
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. public class Queue {
  2.  
  3. private int size;
  4. char queueArr[];
  5. int front;
  6. int rear;
  7. int currentSize = 0;
  8.  
  9. public Queue(int size) {
  10. this.size = size;
  11. front = 0;
  12. rear = -1;
  13. queueArr = new char[this.size];
  14. }
  15.  
  16. public void enqueue(char data) {
  17. if (!isFull()){
  18. rear++;
  19. if (rear == size) {
  20. rear = 0;
  21. }
  22. queueArr[rear] = data;
  23. currentSize++;
  24. }
  25. }
  26.  
  27. public char dequeue() {
  28. char fr = 0;
  29. if (!isEmpty()){
  30. fr = queueArr[front];
  31. front++;
  32. if (front == size) {
  33. front = 0;
  34. }
  35. currentSize--;
  36. return fr;
  37. }
  38. return fr;
  39. }
  40.  
  41. public boolean isFull() {
  42. if (currentSize == size) {
  43. return true;
  44. }
  45. return false;
  46. }
  47.  
  48. public boolean isEmpty() {
  49.  
  50. if (currentSize == 0) {
  51. return true;
  52. }
  53. return false;
  54. }
  55.  
  56. public String getString(){
  57. String str = "";
  58. while(!isEmpty()){
  59. str = str + dequeue();
  60. }
  61. return str;
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment