In: Computer Science
Using the Queue ADT:
Create a program that uses a Queue. Your program should ask the user to input a few lines of text and then outputs strings in same order of entry. (use of the library ArrayDeque)
In Java please.
ANSWER:--
GIVEN THAT:--
CODE:
// import ArrayDequeue
import java.util.*;
// extends abstract queue
public class Queue extends adtqueue {
Deque<Character> deque = new
ArrayDeque<Character>(10);
public static void main(String[] args) {
// making instance of
Queue
Queue q= new Queue();
//
System.out.println();
System.out.println("Operation on
String : WASHINGTON");
System.out.println();
String test="WASHINGTON";
System.out.print("String entered
into Queue : WASHINGTON");
q.enqueue('W');
q.enqueue('A');
q.enqueue('S');
q.enqueue('H');
q.enqueue('I');
q.enqueue('N');
q.enqueue('G');
q.enqueue('T');
q.enqueue('O');
q.enqueue('N');
System.out.println();
System.out.println();
System.out.print("String Dequeueed
from Queue by one by one chars : ");
while(!q.isEmpty()) {
System.out.print(q.dequeue()+" ");
}
System.out.println();
System.out.println();
System.out.println("Yes! String is
in Same order");
}
// return is Empty
boolean isEmpty() {
return deque.isEmpty();
}
@Override
void enqueue(char i) {
deque.add(i);
}
@Override
char dequeue() {
char c=deque.remove();
return c;
}
@Override
char top() {
char c=deque.peekFirst();
return 0;
}
}
// Abstract Queue class
abstract class adtqueue{
abstract void enqueue(char i);
abstract char dequeue();
abstract char top();
}
SAMPLE OUTPUT: