diff --git a/java/queue/README.md b/java/queue/README.md index e69de29bb..f6def1fdd 100644 --- a/java/queue/README.md +++ b/java/queue/README.md @@ -0,0 +1,28 @@ +# Queues + +## What are queues? +Queues are just like the line you have in a supermarket. You have a line that is First In and First Out. This is called FIFO structure. But what is the hype surrounding queues? + +## What can you use queues for? +Queue data structures are commonly used in various scenarios where items or tasks need to be processed in a first-in, first-out order. Remember these are a few of the many ways that queues can be used! + +1. Job Scheduling: In computer systems and networks, queues are used to handle job scheduling. Jobs are added to a queue and processed in the order they were received. +2. Call Center Phone Systems: In call center phone systems, incoming calls are added to a queue, and customers are served in the order their calls were received. +3. Online Multiplayer Games: In online multiplayer games, queues are often used to manage the order in which players join game sessions or matchmaking queues. + +## What can you do with Queue Data Structure? +- Dequeue: deletes and returns the element that is at the front of the queue +- Enqueue: inserts the element at the back of the queue +- Peek: peeks at the front of the queue and returns the first element +- isEmpty: is the queue empty? It checks it +- isFull: is the queue full? It checks it + +## Methods that can be used for Queues +1. add() - adds an element to the queue +2. remove() - removes an element into the queue +3. offer() - inserts an element into the queue and if successful will return true +4. peek() - returns the head of the queue; if empty will return null +5. poll() - removes and returns the head of the queue; if empty will return null +6. element() - returns the head of the queue; if empty will throw an exception +7. peek() - returns the head of the queue; if empty will return null + diff --git a/java/queue/guess.java b/java/queue/guess.java new file mode 100644 index 000000000..8d15adadb --- /dev/null +++ b/java/queue/guess.java @@ -0,0 +1,54 @@ +package java.queue; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; +import java.util.Random; + +// This is a small guessing game that uses linkedlists and queues + +public class guess { + public static void main(String[] args) { + guess(); + } + + public static void guess() { + Random random = new Random(); + int secretNumber = random.nextInt(100) + 1; + Queue previousGuesses = new LinkedList<>(); + + System.out.println("We are going to play a quick guessing gam \n"); + System.out.println("Guess a number between 1 and 100 \n"); + + // Scanner allows you to add inputs and for the system to recognize it + Scanner scanner = new Scanner(System.in); + + // uses a while loop to allow you to continue to guess the number until you get + // it + while (true) { + System.out.println("Guess the number"); + String input = scanner.nextLine(); + + if (!input.matches("\\d+")) { + System.out.println("Please enter a valid number."); + continue; + } + + int guess = Integer.parseInt(input); + + if (guess < secretNumber) { + System.out.println("Too low! Try again"); + // This adds the guess to the line + previousGuesses.add(guess); + } else if (guess > secretNumber) { + System.out.println("Too high! Try again"); + previousGuesses.add(guess); + } else { + System.out.println("You got it! It was " + secretNumber); + break; + } + System.out.println("Previous guess" + previousGuesses); + } + scanner.close(); + } +} \ No newline at end of file