Created
January 3, 2016 16:15
-
-
Save pedrofurtado/fba27d616816e1b380ee to your computer and use it in GitHub Desktop.
Implementation of Deque data structure in Java
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
/** | |
* @file | |
* Deque (Double ended queue). | |
* | |
* Implementation with doubly linked list, through the nested class DequeNode. | |
* It's used nested class to improve encapsulation. | |
* | |
* @author Pedro Furtado | |
*/ | |
public class Deque { | |
/** | |
* Properties of deque. | |
*/ | |
private DequeNode begin = null; | |
private DequeNode end = null; | |
/** | |
* Element of deque. | |
*/ | |
class DequeNode { | |
public int key; | |
public DequeNode previous = null; | |
public DequeNode next = null; | |
DequeNode(int key) { | |
this.key = key; | |
} | |
} | |
/** | |
* Add to begin method. | |
* | |
* Add an element to the beginning of deque. | |
* | |
* @param int key | |
* Integer key. | |
* @return void | |
*/ | |
public void add_to_begin(int key) { | |
DequeNode node = new DequeNode(key); | |
if (this.is_empty()) { | |
this.end = node; | |
} | |
else { | |
this.begin.previous = node; | |
} | |
node.next = this.begin; | |
this.begin = node; | |
} | |
/** | |
* Add to end method. | |
* | |
* Add an element to the end of deque. | |
* | |
* @param int key | |
* Integer key. | |
* @return void | |
*/ | |
public void add_to_end(int key) { | |
DequeNode node = new DequeNode(key); | |
if (this.is_empty()) { | |
this.begin = node; | |
} | |
else { | |
this.end.next = node; | |
} | |
node.previous = this.end; | |
this.end = node; | |
} | |
/** | |
* Remove to begin method. | |
* | |
* Remove an element to the beginning of deque. | |
* | |
* @param int key | |
* Integer key. | |
* @return void | |
*/ | |
public int remove_to_begin() { | |
if (this.is_empty()) return -5; | |
int key = this.begin.key; | |
if (this.begin == this.end) { | |
this.begin = null; | |
this.end = null; | |
} | |
else { | |
this.begin = this.begin.next; | |
this.begin.previous = null; | |
} | |
return key; | |
} | |
/** | |
* Remove to end method. | |
* | |
* Remove an element to the end of deque. | |
* | |
* @param int key | |
* Integer key. | |
* @return void | |
*/ | |
public int remove_to_end() { | |
if (this.is_empty()) return -5; | |
int key = this.begin.key; | |
if (this.begin == this.end) { | |
this.begin = null; | |
this.end = null; | |
} | |
else { | |
this.end = this.end.previous; | |
this.end.next = null; | |
} | |
return key; | |
} | |
/** | |
* Empty method. | |
* | |
* Determines if deque is empty, i.e., if it does not have elements inside. | |
* | |
* @return boolean | |
*/ | |
public boolean is_empty() { | |
return (this.begin == null) && (this.end == null); | |
} | |
/** | |
* Size method. | |
* | |
* Determines the size of deque, i.e., the number of elements in deque. | |
* | |
* @return int | |
* Number of elements in deque. | |
*/ | |
public int size() { | |
if (this.is_empty()) return 0; | |
int i = 0; | |
DequeNode p = this.begin; | |
while(p != null) { | |
i++; | |
p = p.next; | |
} | |
return i; | |
} | |
/** | |
* toString method. | |
* | |
* Provide some visual funcionality to see the elements inside the deque. | |
* | |
* @return String | |
* Representation of the deque in the moment by a string. | |
*/ | |
public String toString() { | |
if (this.is_empty()) return "Empty deque."; | |
DequeNode p = this.begin; | |
String description = "Deque: "; | |
while(p != null) { | |
description += p.key + " "; | |
p = p.next; | |
} | |
description += " Size : " + this.size(); | |
return description; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment