prateeksharma

circular queue Using Array

Feb 18th, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class CircularQueueExample
  3. {
  4.  
  5. int ar[];
  6. int Front;
  7. int Rear;
  8. CircularQueueExample()
  9. {
  10. ar=new int[5];
  11. Front= -1;
  12. Rear = -1;
  13. }
  14.  
  15. void insert()
  16. {
  17.  
  18.  
  19. if((Front==0&&Rear==4)||(Rear==Front-1)){
  20. {
  21. System.out.println("Queue is full");
  22.  
  23. }
  24. else
  25. {
  26.  
  27. System.out.println("enter the element");
  28. Scanner sc = new Scanner(System.in);
  29. int var =sc.nextInt();
  30.  
  31. if(Front==-1)
  32. {
  33. Front=0;
  34. }
  35. if(Rear<4)
  36. {
  37. Rear=Rear+1;
  38. ar[Rear]=var;
  39.  
  40. }
  41. else if(Rear==4&&Front!=0)
  42. {
  43. Rear=0;
  44. ar[Rear]=var;
  45. }
  46.  
  47.  
  48. System.out.println("element inserted");
  49. }
  50. }
  51. void delete()
  52. {
  53. if(Front==-1)
  54. {
  55. System.out.println("Queue empty");
  56. }
  57. else
  58. {
  59. if(Front==Rear)
  60. {
  61. System.out.println("deleted "+ar[Front]);
  62. Front=-1;
  63. Rear=-1;
  64. }
  65. else if(Front<Rear)
  66. {
  67. System.out.println("deleted "+ar[Front]);
  68. Front=Front+1;
  69. }
  70. else if(Rear<Front&&Front<4)
  71. {
  72. System.out.println("deleted "+ar[Front]);
  73. Front=Front+1;
  74.  
  75. }
  76. else if(Rear<Front&& Front<4){
  77. System.out.println("Deleted "+ar[Front]);
  78. Front=Front+1;
  79.  
  80. }
  81.  
  82. }
  83.  
  84.  
  85. void traverse()
  86. {
  87.  
  88. if(Front==-1)
  89. {
  90. System.out.println("queue is empty");
  91.  
  92. }
  93. else
  94. {
  95. if(Rear<Front)
  96. {
  97. for(int i=Front;i<=4;i++)
  98. {
  99. System.out.println(" "+ar[i]);
  100.  
  101. }
  102. }
  103. for(int i=0;i<=Rear;i++)
  104. {
  105. System.out.println(" "+ar[i]);
  106. }
  107. }
  108. else
  109. {
  110. for(int i=Front;i<=Rear;i++)
  111. {
  112. System.out.println(" "+ar[i]);
  113. }
  114.  
  115.  
  116.  
  117. public static void main(String args[])
  118. {
  119. CircularQueueExample obj = new CircularQueueExample();
  120. }
  121.  
  122.  
  123. while(true)
  124. {
  125. System.out.println("press 1 for insert");
  126. System.out.println("press 2 for delete");
  127. System.out.println("press 3 for traverse");
  128. System.out.println("press 4 for exit");
  129.  
  130. System.out.println("enter ur choice");
  131. Scanner choice = new Scanner (System.in);
  132. int ch = choice.nextInt();
  133.  
  134. switch(ch)
  135. {
  136. case 1:
  137. obj.insert();
  138. break;
  139. case 2:
  140. obj.delete();
  141. case 3:
  142. obj.traverse();
  143. break;
  144. case 4:
  145. System.exit(0);
  146. break;
  147. default:
  148. System.out.println("invalid choice");
  149. }
  150.  
  151. }
  152. }
  153. }
Add Comment
Please, Sign In to add comment