Guest User

Queue

a guest
Dec 15th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  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.         String tmp;
  47.  
  48.         System.out.print("Enter the size of the array:");
  49.         a=keyboard.nextInt();
  50.         Queue3 theQueue=new Queue3(a);
  51.         System.out.print("Enter the number of items to be inserted:");
  52.         b=keyboard.nextInt();
  53.        
  54.         for(int i=1;i<=b;i++) {
  55.             System.out.print("Enter the item:");
  56.             tmp=keyboard.next();
  57.             c=tmp.charAt(0);
  58.             theQueue.insert(c);
  59.         }
  60.        
  61.         System.out.print("Enter the number of items to be removed:");
  62.         d=keyboard.nextInt();
  63.         for(int k=1;k<=d;k++) {
  64.             theQueue.remove();
  65.         }
  66.        
  67.         System.out.print("Enter the number of items to be inserted:");
  68.         b=keyboard.nextInt();
  69.         for(int i=1;i<=b;i++) {
  70.             System.out.print("Enter the item:");
  71.             tmp=keyboard.next();
  72.             c=tmp.charAt(0);
  73.             theQueue.insert(c);
  74.         }
  75.        
  76.         while(!theQueue.isEmpty()) {
  77.             char n=theQueue.remove();
  78.             System.out.print(n);
  79.             System.out.print(" ");
  80.         }
  81.         System.out.println(" ");
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment