Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Queue3 {
- private int maxSize;
- private char[] queueArr;
- private int front;
- private int rear;
- private int nItems;
- public Queue3(int s) {
- maxSize=s;
- queueArr=new char[maxSize];
- front=0; rear=-1; nItems=0;
- }
- public void insert(char letter) {
- if(rear==maxSize-1) rear=-1;
- queueArr[++rear]=letter; nItems++;
- }
- public char remove() {
- char temp=queueArr[front++];
- if(front==maxSize) front=0;
- nItems--;
- return temp;
- }
- public char peekFront() {
- return queueArr[front];
- }
- public boolean isEmpty() {
- return (nItems==0);
- }
- public boolean isFull() {
- return (nItems==maxSize);
- }
- public int size() {
- return nItems;
- }
- }
- class QueueApp3 {
- public static void main(String [] args) throws IOException {
- Scanner keyboard = new Scanner (System.in);
- int a, b, d;
- char c;
- System.out.print("Enter the size of the array:");
- a=keyboard.nextInt();
- Queue3 theQueue=new Queue3(a);
- System.out.print("Enter the number of items to be inserted:");
- b=keyboard.nextInt();
- for(int i=1;i<=b;i++) {
- System.out.print("Enter the item:");
- c=keyboard.next().charAt(0);
- theQueue.insert(c);
- }
- System.out.print("Enter the number of items to be removed:");
- d=keyboard.nextInt();
- for(int k=1;k<=d;k++) {
- theQueue.remove();
- }
- System.out.print("Enter the number of items to be inserted:");
- b=keyboard.nextInt();
- for(int i=1;i<=b;i++) {
- System.out.print("Enter the item:");
- c=keyboard.next().charAt(0);
- theQueue.insert(c);
- }
- while(!theQueue.isEmpty()) {
- char n=theQueue.remove();
- System.out.print(n);
- System.out.print(" ");
- }
- System.out.println(" ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement