Guest User

Queue

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