Advertisement
_Alpha_

Implement Queue

Feb 17th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class queue{
  5.     static int n=201;
  6.     int[] a=new int[n];
  7.     int r=-1,f=-1;
  8.     void enqueue(int x){
  9.         r=(r+1)%n;
  10.         a[r%n]=x;
  11.     }
  12.     int dequeue(){
  13.         if(r%n==f%n)
  14.             return 0;
  15.         int z=f;
  16.         f=(f+1)%n;
  17.         return a[(1+z)%n];
  18.     }
  19.     int front(){
  20.         return a[(f+1)%n];
  21.     }
  22.     int rear(){
  23.         return a[r];
  24.     }
  25.     int size(){
  26.         return (r-f+n)%n;
  27.     }
  28. }
  29. public class Solution {
  30.     static int n=201;
  31.    
  32.     public static void main(String[] args) {
  33.         Scanner sc=new Scanner(System.in);
  34.         int t=sc.nextInt();
  35.         queue q=new queue();
  36.         String p=" ";
  37.         while(t>0){
  38.             String st=sc.next();
  39.             if(st.equals("Enqueue")){
  40.                 int x=sc.nextInt();
  41.                 q.enqueue(x);
  42.             }
  43.             else{
  44.                 int tz=q.dequeue();
  45.                 if(p.equals("Enqueue") && tz==0){
  46.                     System.out.println(n);
  47.                 }
  48.                 else if(tz==0){
  49.                     System.out.println("Empty");
  50.                 }
  51.                 else
  52.                     System.out.println(tz);
  53.             }
  54.             p=st;
  55.             t--;
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement