Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Created October 19, 2021 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jananpatel2002/3c970a774c77dfd889b46aedab1be0b3 to your computer and use it in GitHub Desktop.
Save jananpatel2002/3c970a774c77dfd889b46aedab1be0b3 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
/*
* Name: Janan Patel
* Date: 10/18/2021
* Course Number: Java 220 / Data Structures
* Course Name: Java 220 / Data Structures
* Problem Number: 5
* Email: jkpatel2001@student.stcc.edu
* Short Description of the Problem: Making a queue system for numbers.
*/
import java.util.Scanner;
public class QueuesUsingStacks {
private final static String TITLE = "The Queue Program";
private final static String CONTINUE_PROMPT = "Do this again? [y/N] ";
private static Stack<Integer> stackNewestOnTop = new Stack<Integer>();
private static Stack<Integer> stackOldestOnTop = new Stack<Integer>();
// **********************************************
// Put as many methods you need here
public static void enqueue(int value) { // add item
stackNewestOnTop.push(value);
}
public static int peek() { // Get oldest item
shiftStacks();
return stackOldestOnTop.peek();
}
public static boolean isEmpty(Stack<Integer> stack) { // returns if it's empty
return stack.isEmpty();
}
private static void shiftStacks() { // shifts the stacks so we can get another stack with opposite information
if (isEmpty(stackOldestOnTop)) {
while (!isEmpty(stackNewestOnTop)) {
stackOldestOnTop.push(stackNewestOnTop.pop());
}
}
}
public static int dequeue() { // Get oldest item and remove it
shiftStacks();
return stackOldestOnTop.pop();
}
public static void doProcess() throws Exception {
try { // try catch block incase the user enters invalid data
@SuppressWarnings("resource")
Scanner sc1 = new Scanner(System.in);
System.out.println("\nEnter a value from 0-9 in order to start the queue up: ");
int number1 = sc1.nextInt();
URL url = new URL("https://cs.stcc.edu/~silvestri/QueueQuery/QueueQuery" + number1 + ".txt");
Scanner sc = new Scanner(url.openStream());
int numberOfLines = sc.nextInt();
while (numberOfLines > 0) {
int queryNumber = sc.nextInt();
if (queryNumber == 1) {
int number = sc.nextInt();
enqueue(number);
}
if (queryNumber == 2) {
dequeue();
}
if (queryNumber == 3)
System.out.println(peek());
numberOfLines--;
}
} catch (Exception e) {
System.out.println("The Values must be between 0 and 9");
}
}
private static void reset() { // Resets the stacks for information doesn't stack
while (!stackNewestOnTop.isEmpty()) {
stackNewestOnTop.pop();
}
while (!stackOldestOnTop.isEmpty()) {
stackOldestOnTop.pop();
}
}
// **********************************************
// Start your logic coding in the process method
private static void process(Scanner input, String args[]) throws Exception {
doProcess();
reset();
}
// **********************************************
// Do not change the doThisAgain method
private static boolean doThisAgain(Scanner input, String prompt) {
System.out.print(prompt);
String doOver = input.nextLine();
return doOver.trim().equalsIgnoreCase("Y");
}
// **********************************************
// Do not change the main method
public static void main(String args[]) throws Exception {
System.out.println("Welcome to " + TITLE);
Scanner input = new Scanner(System.in);
do {
process(input, args);
} while (doThisAgain(input, CONTINUE_PROMPT));
input.close();
System.out.println("Thank you for using " + TITLE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment