Advertisement
Guest User

MAIN

a guest
Dec 15th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Main {
  3. //==============================QUEUE================================\\
  4. int maxi = 1000;
  5. int[] a = new int[maxi];
  6. int f, r;
  7.  
  8. void initialize() {
  9. f = r = -1;
  10. }
  11.  
  12. void enqueue(int val) {
  13. if (r == a[maxi] - 1) {
  14. System.out.println("Queue Overflow!");
  15. } else if (f == -1 && r == -1) {
  16. f = r = 1;
  17. a[r] = val;
  18. } else {
  19. r++;
  20. a[r] = val;
  21. }
  22. }
  23.  
  24. void dequeue() {
  25. if (f == -1 && r == -1) {
  26. System.out.println("Queue is already empty!\n\n");
  27. } else if (f == r) {
  28. System.out.println("The dequeued element is: \n" + a[f]);
  29. f = r = -1;
  30. } else {
  31. System.out.println("The dequeued element is: " + a[f]);
  32. System.out.println("\n\n");
  33. f++;
  34. }
  35. }
  36.  
  37. void call() {
  38. int x;
  39. if (f == -1 && r == -1) {
  40. System.out.println("The Queue is empty!\n\n");
  41. return;
  42. }
  43. System.out.println("The Contents of the Queue is: ");
  44. System.out.println("\n\n");
  45. for (x = f; x <= r; x++) {
  46. System.out.println(a[x] + " ");
  47. }
  48. }
  49.  
  50. public static void main(String[] args) {
  51. Scanner sc = new Scanner(System.in);
  52. Main obj = new Main();
  53.  
  54. obj.initialize();
  55. char qw = 0;
  56. int value;
  57. System.out.println("\t------------------Queue------------------\n");
  58. System.out.println("Select below:\n");
  59. System.out.print("[a] - Enqueue\n[b] - Dequeue\n[c] - Exit\n");
  60. System.out.print("Select your choice: ");
  61. qw = sc.nextLine().charAt(0);
  62.  
  63. switch (qw) {
  64. case 'a':
  65. System.out.print("Enter the element to be enqueued: ");
  66. value = sc.nextInt();
  67. sc.nextLine();
  68. obj.enqueue(value);
  69. break;
  70. case 'b':
  71. obj.dequeue();
  72. break;
  73. case 'c':
  74. obj.call();
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement