0% found this document useful (0 votes)
5 views

Linear Data Structures_ Queues Cheatsheet _ Codecademy

The document provides an overview of the Java Queue class, detailing its overloaded constructor, helper methods, and core functionalities such as enqueue, dequeue, and peek. It explains how the queue operates under a first in, first out (FIFO) protocol and includes methods to check for space and emptiness. Additionally, it describes the queue's structure and the use of LinkedList for managing its elements.

Uploaded by

usernxt123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Linear Data Structures_ Queues Cheatsheet _ Codecademy

The document provides an overview of the Java Queue class, detailing its overloaded constructor, helper methods, and core functionalities such as enqueue, dequeue, and peek. It explains how the queue operates under a first in, first out (FIFO) protocol and includes methods to check for space and emptiness. Additionally, it describes the queue's structure and the use of LinkedList for managing its elements.

Uploaded by

usernxt123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Cheatsheets / Linear Data Structures

Queues

Java Queue: Overloaded Constructor

The constructor in the Java Queue class can be public Queue() {


overloaded in order to create an unbounded queue.
this(DEFAULT_MAX_SIZE);
The main constructor takes one argument,
maxSize , which it assigns as the maximum size of }
the queue.
The overloaded constructor doesn’t take any
public Queue(int maxSize) {
arguments but assigns the maximum size to be
Integer.MAX_VALUE , which is the greatest this.queue = new LinkedList();
integer value in Java. This is stored in a variable this.size = 0;
DEFAULT_MAX_VALUE . If no specified max size is this.maxSize = maxSize;
provided as a parameter to a constructor, the
overloaded constructor calls the main constructor }
using DEFAULT_MAX_VALUE as its parameter.

Java Queue: Helper Methods

The Java Queue class should include two helper public boolean hasSpace() {
methods to determine what actions can be taken with
return this.size < this.maxSize;
the queue:
.hasSpace() returns a boolean }
representing whether or not there is room left
in a bounded queue
public boolean isEmpty() {
.isEmpty() returns a boolean
representing whether or not the queue is empty return this.size == 0;
These methods use the Queue instance variables, }
size and maxSize , to determine what value
should be returned.
Java Queue: enqueue()

The .enqueue() method of the Java Queue public void enqueue(String data) {
class is used to add new data to the queue. It takes a
if (this.hasSpace()) {
single argument, data , which is added to the end of
the queue using the LinkedList method this.queue.addToTail(data);
.addToTail() . A print statement can be this.size++;
included to describe the addition. The method then System.out.println("Added " +
increases size and throws an error if the queue is
data + "! Queue size is now " +
full. The helper method .hasSpace() is used to
verify if the queue is full. this.size);
} else {
throw new Error("Queue is
full!");
}
}

Java Queue: dequeue()

The .dequeue() method of the Java Queue public String dequeue() {


class removes the head of the queue using the
if (!this.isEmpty()) {
LinkedList method, .removeHead() , and
then returns the head’s data. A statement can be String data =
printed describing this removal. The method also this.queue.removeHead();
decreases size and throws an error if the queue is this.size--;
empty. The helper method .isEmpty() verifies if
the queue is empty. System.out.println("Removed " +
data + "! Queue size is now " + this.size
+ ".");
return data;
} else {
throw new Error("Queue is
empty!");
}
}
Java Queue: peek()

The .peek() method of the Java Queue class public String peek() {
allows us to see the element at the head of the queue
if (this.isEmpty()) {
without removing it. If a head exists (the queue is not
empty), this method returns the data in the head. return null;
Otherwise, it returns null . This is verified using the } else {
helper method .isEmpty() . return this.stack.head.data;
}
}

Queue data structure methods

The queue data structure has three main methods:


enqueue (adds a node to the back of the
queue)
dequeue (removes node at the front of the
queue)
peek (returns value of node at the front of
the queue, without removing it)

Queue follows FIFO protocol

A queue is a data structure composed of nodes, which


follows a first in, first out (FIFO) protocol.
This is analogous to a line at a grocery store, for which
the first customer in the queue is the first to checkout.

Print Share

You might also like