Cosc241 - Java
Lab 14b: Queue
Queue.java
package lab15e;
public interface Queue<E> {
public int size();
public void clear();
public void addLast(E elem);
public E removeFirst();
public E getFirst();
public String toString();
public boolean isEmpty();
}
QueueApp.java
package lab15e;
import java.util.*;
public class QueueApp<E> {
public static void main(String[] args){
Queue<String> queue = new TwoStackQueue<String>();
Scanner input = new Scanner(System.in);
while (input.hasNextLine()){
handleLine(new Scanner(input.nextLine()), queue);
}
}
public static void handleLine(Scanner scanLine, Queue<String> queue){
if(scanLine.hasNext("[agrp]")){
char command = scanLine.next().charAt(0);
switch(command){
case 'a':
while(scanLine.hasNext()){
queue.addLast(scanLine.next());
}
break;
case 'g':
System.out.println(queue.getFirst());
break;
case 'r':
System.out.println(queue.removeFirst());
break;
case 'p':
System.out.println(queue.toString());
System.out.println(queue.size());
break;
}
}
}
}
TwoStackQueue.java
package lab15e;
import java.util.*;
public class TwoStackQueue<E> implements Queue<E> {
public Stack<E> leftStack = new Stack<E>();
public Stack<E> rightStack = new Stack<E>();
int size = 0;
public boolean isEmpty(){
return(leftStack.empty() && rightStack.empty());
}
public int size(){
return size;
}
public void clear(){
leftStack = new Stack<E>();
rightStack = new Stack<E>();
size = 0;
}
public E getFirst(){
if(isEmpty()){
throw new EmptyQueueException();
}
if(rightStack.empty()){
while(!leftStack.empty()){
rightStack.push(leftStack.pop());
}
}
return rightStack.peek();
}
public E removeFirst(){
if(isEmpty()){
throw new EmptyQueueException();
}
if(rightStack.empty()){
while(!leftStack.empty()){
rightStack.push(leftStack.pop());
}
}
size--;
return rightStack.pop();
}
public void addLast(E elem){
leftStack.push(elem);
size++;
}
public String toString(){
String leftRight = leftStack.toString() + rightStack.toString();
return leftRight;
}
}
EmptyQueueException.java
package lab15e;
public class EmptyQueueException extends RuntimeException {
private static final long serialVersionUID = 1;
public EmptyQueueException(){
super();
}
}
public interface Queue<E> {
public int size();
public void clear();
public void addLast(E elem);
public E removeFirst();
public E getFirst();
public String toString();
public boolean isEmpty();
}
import java.util.*;
public class QueueApp<E> {
public static void main(String[] args){
Queue<String> queue = new TwoStackQueue<String>();
Scanner input = new Scanner(System.in);
while (input.hasNextLine()){
handleLine(new Scanner(input.nextLine()), queue);
}
}
public static void handleLine(Scanner scanLine, Queue<String> queue){
if(scanLine.hasNext("[agrp]")){
char command = scanLine.next().charAt(0);
switch(command){
case 'a':
while(scanLine.hasNext()){
queue.addLast(scanLine.next());
}
break;
case 'g':
System.out.println(queue.getFirst());
break;
case 'r':
System.out.println(queue.removeFirst());
break;
case 'p':
System.out.println(queue.toString());
System.out.println(queue.size());
break;
}
}
}
}
import java.util.*;
public class TwoStackQueue<E> implements Queue<E> {
public Stack<E> leftStack = new Stack<E>();
public Stack<E> rightStack = new Stack<E>();
int size = 0;
public boolean isEmpty(){
return(leftStack.empty() && rightStack.empty());
}
public int size(){
return size;
}
public void clear(){
leftStack = new Stack<E>();
rightStack = new Stack<E>();
size = 0;
}
public E getFirst(){
if(isEmpty()){
throw new EmptyQueueException();
}
if(rightStack.empty()){
while(!leftStack.empty()){
rightStack.push(leftStack.pop());
}
}
return rightStack.peek();
}
public E removeFirst(){
if(isEmpty()){
throw new EmptyQueueException();
}
if(rightStack.empty()){
while(!leftStack.empty()){
rightStack.push(leftStack.pop());
}
}
size--;
return rightStack.pop();
}
public void addLast(E elem){
leftStack.push(elem);
size++;
}
public String toString(){
String leftRight = leftStack.toString() + rightStack.toString();
return leftRight;
}
}
public class EmptyQueueException extends RuntimeException {
private static final long serialVersionUID = 1;
public EmptyQueueException(){
super();
}
}
