Skip to content

Instantly share code, notes, and snippets.

@ariyike
Last active June 13, 2018 08:01
Show Gist options
  • Save ariyike/e085c1ab613d2e749820ec35ce988ecf to your computer and use it in GitHub Desktop.
Save ariyike/e085c1ab613d2e749820ec35ce988ecf to your computer and use it in GitHub Desktop.
A Queue Interface specifying operations and its implementations
package pkgclass.projects;
/**
*
* @author Student
* @param <E>
*/
public interface InterfaceQueue<E> {
//inserts new element to end of queue
void enqueue(E e);
//removes and returns the first element in the queue, should return null if queue is empty
E dequeue();
//returns but the first element, doesn't remove it
E getFront();
//returns no. of elements in the queue
int size();
//checks if the queue is empty
boolean isEmpty();
}
package pkgclass.projects;
/**
*
* @author Student
* @param <E>
*/
public class QueueImplementation<E> implements InterfaceQueue{
E [] queueImplement;
int size;
int queueHead;
int queueTail;
public QueueImplementation(int sizeOfQueue){
queueImplement = (E[]) new Object[sizeOfQueue];//first we cast the Object to type E then use the parameter sizeOfQueue
this.queueHead = 0;//initialize head
this.queueTail = 0;//initialize tail
this.size = 0;//initialize tail
}
/**
* @param e
*/
@Override
//to add new elements to the end of the array
public void enqueue(Object e) {
E castedVersion = (E)e;//casts the entered parameter from an Object to type E so it can be added to the array
//first, check if the queue(array) is full
if(this.isFull()){
this.stretch();//if it is full, make it bigger, this method is at the bottom
int cap = this.queueImplement.length;//stores the length of the array
if(this.queueTail == cap){
this.queueTail = this.queueTail%cap;//ensures that the queue wraps back around to the front to fill empty spaces if there
//are no longer spaces on the other side
this.queueImplement[queueTail] = castedVersion;//adds the casted object to the Queue
}
else{
this.queueImplement[queueTail] = castedVersion;//adds the casted object to the Queue
}
}
this.queueTail = this.queueTail+1;//update position of queue tail
this.size = this.size+1;//update number of elements in the queue by adding 1
throw new UnsupportedOperationException("Not supported yet.");
}
/**
* @return E
*/
@Override
//to remove elements from the beginning of the array
public E dequeue() {
E removeThis;//will take the element in the head position and saves it so we can return it
//first check that the queue isn't empty
if(this.isEmpty()){
System.out.println("This queue is already empty");
removeThis = null;//fill removeThis with a null value and return that
}
else{
removeThis = this.queueImplement[queueHead];//saves element at head so we can return it
this.queueImplement[queueHead] = null;//removes the element in the head index and replaces it with null
this.queueHead = queueHead+1;//update position of queue head
this.size = size - 1;//update the size of the array by reducing 1 now that an element has been removed
}
return removeThis;
}
/**
* @return E
*/
@Override
//to get the element at the beginning of the array but doesn't delete it
public E getFront() {
return queueImplement[queueHead];//gets the element at the index equal to the head and returns it
}
/**
* @return Integer
*/
@Override
//returns size of the Queue, so number of elements in the queue. Is updated whenever an element is removed or added
public int size(){
return this.size;
}
/**
* @return boolean
*/
@Override
//To change body of generated methods, choose Tools | Templates.
public boolean isEmpty(){
return (this.size == 0);//checks if the number of elements in the queue is equal to 0
}
/**
* @return boolean
*/
public boolean isFull(){
return (this.size == this.queueImplement.length);//checks if the number of elements is equal to the capacity of the underlying array
}
/**
* increases the size of the underlying array
*/
private void stretch(){
int max = this.queueImplement.length;//stores the length of the previous array
E[] extArray = (E[])new Object[max*2];//create the new array with length that's 2x the size of the previous
int limit = queueHead+max;//this variable is the control for the for loop that does the transfer
//the logic is to have enough spaces for all the elements being transfered.
//if we use the length of the previous array, it'll stop at the last element
//completely ignoring all elements before the head.
int fromHere;
for(int i=queueHead; i<limit; i++){
fromHere = i%max; //this here makes sure that we wrap around to the front of the array
//once we reach the end of the previous array
extArray[i] = this.queueImplement[fromHere];
}
this.queueImplement = extArray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment