Skip to content

Instantly share code, notes, and snippets.

@AhnMo
Last active July 2, 2019 12:19
Show Gist options
  • Save AhnMo/dd0ba66bcbf7a7d2a939e2ce37032508 to your computer and use it in GitHub Desktop.
Save AhnMo/dd0ba66bcbf7a7d2a939e2ce37032508 to your computer and use it in GitHub Desktop.
import java.util.Iterator;
import java.util.Random;
public class Queue<T> implements QueueInterface<T>, Iterable<T> {
private static final int DEFAULT_CAPACITY = 10;
private int current = 0;
private int rear = 0;
private Object[] queueArray = null;
private int capacity = 0;
// wrapping implementation
private class Wrapping<T> {
private T value;
private int tag;
public Wrapping(T v, int t) {
this.value = v;
this.tag = t;
}
public T getValue() { return this.value; }
public int getTag() {return this.tag; }
}
@SuppressWarnings("unchecked")
public Queue() {
capacity = DEFAULT_CAPACITY;
queueArray = new Object[DEFAULT_CAPACITY];
rear = 0;
current = 0;
}
@Override
public boolean empty() {
return capacity == current;
}
@Override
public void enqueue(T item) {
if(full()) ensureCapacity();
// wrapping implementation
int tag = new Random().nextInt();
Wrapping<T> newItem = new Wrapping<>(item, tag);
queueArray[current] = (Object) newItem;
current++;
System.out.println("Enqueue, TAG: " + String.valueOf(tag) + " / VALUE: " + String.valueOf(item));
/*
// original implementation
queueArray[current] = (Object) item;
current++;
*/
}
@Override
public T dequeue() throws QueueException {
if (rear == current) throw new QueueException();
T dequeuedItem = front();
rear++;
return dequeuedItem;
}
@Override
public T front() throws QueueException {
// wrapping implementation
Wrapping<T> x = (Wrapping<T>) queueArray[rear];
System.out.println("VALUE: " + String.valueOf(x.getValue()) + " / TAG: " + String.valueOf(x.getTag()));
return x.getValue();
/*
// original implementation
return (T) queueArray[rear];
*/
}
@Override
public void clear() {
for (int i = 0; i < capacity; i++)
queueArray[i] = null;
current = 0;
rear = 0;
}
@SuppressWarnings("unchecked")
private void ensureCapacity() {
if (rear != 0) {
copyElements(queueArray);
} else {
capacity *= 2;
Object[] tempQueueArray = new Object[capacity];
copyElements(tempQueueArray);
}
current -= rear;
rear = 0;
}
private void copyElements(Object[] array) {
for (int i = rear; i < current; i++)
array[i - rear] = queueArray[i];
queueArray = array;
}
@Override
public Iterator<T> iterator() {
return new QueueItearator<T>();
}
public boolean full() {
return current == capacity;
}
private class QueueItearator<T> implements Iterator<T> {
private int index = rear;
@Override
public boolean hasNext() {
return index < current;
}
@SuppressWarnings("unchecked")
@Override
public T next() {
// wrapping implementation
return (T) ((Wrapping) queueArray[index++]).getValue();
/*
// original implementation
//return (T) queueArray[index++];
*/
}
}
}
public class QueueException extends Exception {
private static final long serialVersionUID = -884127093599336807L;
public QueueException() {
super();
}
public QueueException(String message) {
super(message);
}
public QueueException(Throwable e) {
super(e);
}
public QueueException(String message, Throwable e) {
super(message, e);
}
}
public interface QueueInterface<T> {
public boolean empty();
public void enqueue(T item);
public T dequeue() throws QueueException;
public T front() throws QueueException;
public void clear();
}
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
try {
Queue<String> q = new Queue<String>();
// enequeue
System.out.println("[*] Enqueue Items");
for (int i = 0; i < 100; ++i) {
q.enqueue(String.valueOf(i));
}
// iteration
Iterator iterator = q.iterator();
System.out.println("[*] List Items : ");
while (iterator.hasNext())
System.out.print(iterator.next() + " ");
System.out.println();
// dequeue
System.out.println("[*] Dequeue Items");
for (int i = 0; i < 200; ++i)
System.out.println(" - Dequeue Item: " + String.valueOf(q.dequeue()));
} catch (Exception e) {
System.out.println("[!] Exception:" + e.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment