Skip to content

Instantly share code, notes, and snippets.

@pedrofurtado
Created January 3, 2016 16:17
Show Gist options
  • Save pedrofurtado/761a9f5fafefed716bea to your computer and use it in GitHub Desktop.
Save pedrofurtado/761a9f5fafefed716bea to your computer and use it in GitHub Desktop.
Implementation of Queue (in circular vector) data structure in Java
/**
* @file
* Queue.
*
* Implementation with circular vector.
* It's used nested class to improve encapsulation.
*
* @author Pedro Furtado
*/
public class QueueCircularVector {
/**
* Properties of queue.
*/
private int max = 5;
private int begin = -1;
private int end = -1;
private int number_of_elements = 0;
private QueueCircularVectorNode[] A = new QueueCircularVectorNode[max];
/**
* Element of queue.
*/
class QueueCircularVectorNode {
public int key;
QueueCircularVectorNode(int key) {
this.key = key;
}
}
/**
* Default constructor method.
*/
QueueCircularVector() {
}
/**
* Overloading of QueueCircularVector() constructor method.
*
* Allow to add to queue a single key on instantiation of the queue.
*
* @param int key
* Integer key.
*/
QueueCircularVector(int key) {
this.queue(key);
}
/**
* Overloading of QueueCircularVector() constructor method.
*
* Allow to add to queue a list of keys on instantiation of the queue.
*
* @param int[] keys
* List of integer keys.
*/
QueueCircularVector(int[] keys) {
for (int key : keys) {
this.queue(key);
}
}
/**
* Enqueue method.
*
* Add an element to the end of the queue in circular vector.
*
* @param int key
* Integer key.
* @return void
*/
public void queue(int key) {
if (this.is_full()) return;
this.number_of_elements++;
if (this.is_empty()) {
this.begin = 0;
}
this.end = (this.end + 1) % this.max;
if (this.A[this.end] == null) {
this.A[this.end] = new QueueCircularVectorNode(key);
}
else {
this.A[this.end].key = key;
}
}
/**
* Dequeue method.
*
* Remove an element from the beginning of the queue.
*
* @return int
* Queue element key or -5 if is empty.
*/
public int dequeue() {
if (this.is_empty()) return -5;
this.number_of_elements--;
int key = this.A[this.begin].key;
this.A[this.begin].key = -99;
if (this.begin == this.end) {
this.begin = -1;
this.end = -1;
}
else {
this.begin = (this.begin + 1) % this.max;
}
return key;
}
/**
* Size method.
*
* Determines the size of queue, i.e., the number of elements in queue.
*
* @return int
* Number of elements in queue.
*/
public int size() {
return this.number_of_elements;
}
/**
* Empty method.
*
* Determines if the queue is empty, i.e., if it does not have elements inside.
*
* @return boolean
*/
public boolean is_empty() {
return (this.begin == -1) && (this.end == -1);
}
/**
* Full method.
*
* Determines if the queue is full, i.e., if the circular vector have all the indexes filled with queue elements.
*
* @return boolean
*/
public boolean is_full() {
return ((this.end + 1) % this.max) == this.begin;
}
/**
* toString method.
*
* Provide some visual funcionality to see the elements inside the queue.
*
* @return String
* Representation of the queue in the moment by a string.
*/
public String toString() {
if (this.is_empty()) return "Empty vector queue.";
String description = "Vector Queue: [ ";
for (int i = 0; i < this.max; i++) {
if ((this.A[i] != null) && (this.A[i].key != -99)) {
description += this.A[i].key + " ";
}
}
description += "] Size: " + this.size();
return description;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment