Advertisement
Guest User

Queue

a guest
Dec 15th, 2012
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3.  
  4.  
  5. class Queue3 {
  6.     private int maxSize;
  7.     private char[] queueArr;
  8.     private int front;
  9.     private int rear;
  10.     private int nItems;
  11.     public Queue3(int s) {
  12.         maxSize=s;
  13.         queueArr=new char[maxSize];
  14.         front=0; rear=-1; nItems=0;
  15.     }
  16.     public void insert(char letter) {
  17.         if(rear==maxSize-1) rear=-1;
  18.         queueArr[++rear]=letter; nItems++;
  19.     }
  20.     public char remove() {
  21.         char temp=queueArr[front++];
  22.         if(front==maxSize) front=0;
  23.         nItems--;
  24.         return temp;
  25.     }
  26.     public char peekFront() {
  27.         return queueArr[front];
  28.     }
  29.     public boolean isEmpty() {
  30.         return (nItems==0);
  31.     }
  32.     public boolean isFull() {
  33.         return (nItems==maxSize);
  34.     }
  35.     public int size() {
  36.         return nItems;
  37.     }
  38. }
  39.  
  40. class QueueApp3 {
  41.     public static void main(String [] args) throws IOException {
  42.         Scanner keyboard = new Scanner (System.in);
  43.         int a, b, d;
  44.         char c;
  45.        
  46.         System.out.print("Enter the size of the array:");
  47.         a=keyboard.nextInt();
  48.         Queue3 theQueue=new Queue3(a);
  49.         System.out.print("Enter the number of items to be inserted:");
  50.         b=keyboard.nextInt();
  51.        
  52.         for(int i=1;i<=b;i++) {
  53.             System.out.print("Enter the item:");
  54.             c=keyboard.next().charAt(0);
  55.             theQueue.insert(c);
  56.         }
  57.        
  58.         System.out.print("Enter the number of items to be removed:");
  59.         d=keyboard.nextInt();
  60.         for(int k=1;k<=d;k++) {
  61.             theQueue.remove();
  62.         }
  63.        
  64.         System.out.print("Enter the number of items to be inserted:");
  65.         b=keyboard.nextInt();
  66.         for(int i=1;i<=b;i++) {
  67.             System.out.print("Enter the item:");
  68.             c=keyboard.next().charAt(0);
  69.             theQueue.insert(c);
  70.         }
  71.        
  72.         while(!theQueue.isEmpty()) {
  73.             char n=theQueue.remove();
  74.             System.out.print(n);
  75.             System.out.print(" ");
  76.         }
  77.         System.out.println(" ");
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement