Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java - Queues #588

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions java/queue/README.md
Original file line number Diff line number Diff line change
@@ -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

54 changes: 54 additions & 0 deletions java/queue/guess.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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();
}
}