Created
June 20, 2012 05:47
-
-
Save pfmiles/2958323 to your computer and use it in GitHub Desktop.
A data structure which have both stack & set features, especially useful to detect 'circles' in trees or graphs.
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
/** | |
* A data structure which have both stack & set features, especially useful to | |
* detect 'circles' in trees or graphs. | |
* | |
* @author pf-miles | |
* | |
*/ | |
public class SetStack<E> extends LinkedHashSet<E> { | |
private static final long serialVersionUID = -3159906489148656808L; | |
private Deque<E> innerStack = new ArrayDeque<E>(); | |
public E push(E item) { | |
if (super.contains(item)) | |
return item; | |
super.add(item); | |
this.innerStack.push(item); | |
return item; | |
} | |
public E pop() { | |
E i = this.innerStack.pop(); | |
super.remove(i); | |
return i; | |
} | |
public E peek() { | |
return this.innerStack.peek(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment