Skip to content

Instantly share code, notes, and snippets.

@ntub46010
Last active June 29, 2021 02:36
Show Gist options
  • Save ntub46010/7c222c5bf882e547028ae600075a40a0 to your computer and use it in GitHub Desktop.
Save ntub46010/7c222c5bf882e547028ae600075a40a0 to your computer and use it in GitHub Desktop.
public class ArrayListQueue<E> implements IQueue<E> {
private Object[] elements;
private int frontIndex = -1;
private int rearIndex = -1;
public ArrayListQueue() {
this.elements = new Object[0];
}
public ArrayListQueue(int capacity) {
this.elements = new Object[capacity];
}
public boolean enqueue(E data) {
if (isFull()) {
grow();
}
if (this.rearIndex == this.elements.length - 1 && this.frontIndex > 0) {
forwardElements();
}
if (size() == 0) {
this.frontIndex = 0;
this.rearIndex = 0;
} else {
this.rearIndex++;
}
this.elements[this.rearIndex] = data;
return true;
}
public E dequeue() {
if (size() == 0) {
throw new RuntimeException();
}
E data = (E) this.elements[this.frontIndex];
this.elements[this.frontIndex] = null;
this.frontIndex++;
return data;
}
public E peek() {
if (size() == 0) {
throw new RuntimeException();
}
return (E) this.elements[frontIndex];
}
public int size() {
if (this.frontIndex > this.rearIndex) {
return 0;
} else if (this.frontIndex == -1) {
return 0;
} else {
return this.rearIndex - this.frontIndex + 1;
}
}
public boolean isFull() {
return size() >= this.elements.length;
}
private void forwardElements() {
int size = size();
for (int i = 0; i < size; i++) {
this.elements[i] = this.elements[this.frontIndex + i];
}
int uncleanedAmount = size > this.frontIndex
? this.frontIndex
: size;
for (int i = uncleanedAmount - 1; i >= 0; i--) {
this.elements[size + i] = null;
}
this.frontIndex = 0;
this.rearIndex = size - 1;
}
private void grow() {
int newCapacity = this.elements.length == 0
? 10
: (int) (this.elements.length * 1.5);
Object[] newElements = new Object[newCapacity];
for (int i = 0; i < this.elements.length; i++) {
newElements[i] = this.elements[i];
}
this.elements = newElements;
}
}
public class ArrayQueue<E> implements IQueue<E> {
private Object[] elements;
private int frontIndex = -1;
private int rearIndex = -1;
public ArrayQueue(int capacity) {
this.elements = new Object[capacity];
}
public boolean enqueue(E data) {
if (isFull()) {
return false;
}
if (this.rearIndex == this.elements.length - 1 && this.frontIndex > 0) {
forwardElements();
}
if (size() == 0) {
this.frontIndex = 0;
this.rearIndex = 0;
} else {
this.rearIndex++;
}
this.elements[this.rearIndex] = data;
return true;
}
public E dequeue() {
if (size() == 0) {
throw new RuntimeException();
}
E data = (E) this.elements[this.frontIndex];
this.elements[this.frontIndex] = null;
this.frontIndex++;
return data;
}
public E peek() {
if (size() == 0) {
throw new RuntimeException();
}
return (E) this.elements[frontIndex];
}
public int size() {
if (this.frontIndex > this.rearIndex) {
return 0;
} else if (this.frontIndex == -1) {
return 0;
} else {
return this.rearIndex - this.frontIndex + 1;
}
}
public boolean isFull() {
return size() >= this.elements.length;
}
private void forwardElements() {
int size = size();
for (int i = 0; i < size; i++) {
this.elements[i] = this.elements[this.frontIndex + i];
}
int uncleanedAmount = size > this.frontIndex
? this.frontIndex
: size;
for (int i = uncleanedAmount - 1; i >= 0; i--) {
this.elements[size + i] = null;
}
this.frontIndex = 0;
this.rearIndex = size - 1;
}
}
public interface IQueue<E> {
boolean enqueue(E data);
E dequeue();
E peek();
int size();
boolean isFull();
}
public class LinkedListQueue<E> implements IQueue<E> {
private Node<E> frontNode;
private Node<E> rearNode;
private int size;
public boolean enqueue(E data) {
Node<E> node = new Node<>(data);
if (this.size == 0) {
this.frontNode = node;
} else {
this.rearNode.setNext(node);
}
this.rearNode = node;
this.size++;
return true;
}
public E dequeue() {
if (this.size == 0) {
throw new RuntimeException();
}
Node<E> node = this.frontNode;
this.frontNode = node.getNext();
this.size--;
if (node == this.rearNode) {
this.rearNode = null;
}
return node.getData();
}
public E peek() {
if (this.size == 0) {
throw new RuntimeException();
}
return this.frontNode.getData();
}
public int size() {
return this.size;
}
public boolean isFull() {
return false;
}
}
public class Node<E> {
private E data;
private Node<E> next;
public Node(E data) {
this.data = data;
}
public E getData() {
return data;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment