Last active
June 9, 2018 18:34
-
-
Save ebenezergraham/6b744055808485eb746549ed5964650a to your computer and use it in GitHub Desktop.
Queue implementation using arrays as the base data structure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2018 Ebenezer K. A. Graham . | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package dsa.weekthree; | |
import java.util.Arrays; | |
/** | |
* | |
* @author Ebenezer K. A. Graham | |
* @param <E> | |
*/ | |
public class Queue<E> implements QueueInterface<E> { | |
private int capacity, entries, head, tail; | |
private E[] queue; | |
public Queue(){ | |
this.head = 0; | |
this.tail = 0; | |
this.entries = 0; | |
this.capacity = 30; | |
this.queue = (E[]) new Object[capacity]; | |
} | |
@Override | |
public boolean enqueue(E e) { | |
//check if queue isn't full then insert into the head else expand | |
//and enqueue | |
if(!isFull()){ | |
queue[this.tail++] = e; | |
++this.entries; | |
return true; | |
}else{ | |
expand(); | |
enqueue(e); | |
} | |
return false; | |
} | |
@Override | |
public E peek() { | |
//returns the head element without removing | |
return queue[head]; | |
} | |
@Override | |
public E dequeue() { | |
//check if queue isn't full then insert into the head else expand | |
//and enqueue | |
if(!empty()){ | |
E element = queue[this.head++]; | |
--this.entries; | |
return element; | |
}else{ | |
throw new NullPointerException(); | |
} | |
} | |
@Override | |
public boolean empty() { | |
//given that a circular queue is a bit tricky you can only determine if | |
//its empty my checking the number of elements between the head and tail | |
return entries == 0; | |
} | |
protected void expand(){ | |
// if the queue is full then double the capacity else | |
if(isFull()){ | |
this.capacity = capacity *2; | |
this.queue = Arrays.copyOf(queue, capacity); | |
}else if (this.head != 0 && !isFull()){ | |
head = 0; | |
this.queue = Arrays.copyOf(queue, capacity); | |
} | |
} | |
protected boolean isFull() { | |
//given that a circular queue is a bit tricky you can only determine if | |
//its empty my checking the number of elements between the head and tail | |
return entries == queue.length; | |
} | |
@Override | |
public int contains(E e){ | |
return Arrays.binarySearch(this.queue, e); | |
} | |
//for testing purposes | |
public E[] getQueue(){ | |
return this.queue; | |
} | |
public int size(){ | |
return this.entries; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2018 Ebenezer K. A. Graham . | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package dsa.weekthree; | |
/** | |
* | |
* @author Ebenezer K. A. Graham | |
*/ | |
public interface QueueInterface<E> { | |
public boolean enqueue(E e); | |
public E peek(); | |
public E dequeue(); | |
public boolean empty(); | |
public int contains(E e); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2018 Ebenezer K. A. Graham . | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package dsa.weekthree; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
/** | |
* | |
* @author Ebenezer K. A. Graham | |
*/ | |
public class QueueTest { | |
public QueueTest() { | |
} | |
/** | |
* Test of enqueue method, of class Queue. | |
*/ | |
@Test | |
public void testEnqueue() { | |
System.out.println("enqueue"); | |
Object e = Integer.MAX_VALUE; | |
Queue instance = new Queue(); | |
boolean expResult = true; | |
boolean result = instance.enqueue(e); | |
assertEquals(expResult, result); | |
instance.enqueue(1); | |
instance.enqueue(2); | |
instance.enqueue(3); | |
instance.enqueue(4); | |
assertEquals(5,instance.size()); | |
} | |
/** | |
* Test of peek method, of class Queue. | |
*/ | |
@Test | |
public void testPeek() { | |
System.out.println("peek"); | |
Queue instance = new Queue(); | |
Object expResult = null; | |
Object result = instance.peek(); | |
assertEquals(expResult, result); | |
} | |
/** | |
* Test of dequeue method, of class Queue. | |
*/ | |
@Test | |
public void testDequeue() { | |
System.out.println("dequeue"); | |
Queue instance = new Queue(); | |
Object e = Integer.MAX_VALUE; | |
instance.enqueue(e); | |
Object expResult = e; | |
Object result = instance.dequeue(); | |
assertEquals(expResult, result); | |
} | |
/** | |
* Test of isEmpty method, of class Queue. | |
*/ | |
@Test | |
public void testIsEmpty() { | |
System.out.println("isEmpty"); | |
Queue instance = new Queue(); | |
boolean expResult = true; | |
boolean result = instance.empty(); | |
assertEquals(expResult, result); | |
} | |
/** | |
* Test of expand method, of class Queue. | |
*/ | |
@Test | |
public void testExpand() { | |
System.out.println("expand"); | |
Queue instance = new Queue(); | |
int counter = 0; | |
for (int index = 0; index<instance.getQueue().length && index<100; index++){ | |
instance.enqueue(index); | |
counter++; | |
} | |
assertEquals(counter,instance.getQueue().length); | |
} | |
/** | |
* Test of isFull method, of class Queue. | |
*/ | |
@Test | |
public void testIsFull() { | |
System.out.println("isFull"); | |
Queue instance = new Queue(); | |
boolean expResult = false; | |
boolean result = instance.isFull(); | |
assertEquals(expResult, result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment