Skip to content

Instantly share code, notes, and snippets.

@pfmiles
Created June 20, 2012 05:47
Show Gist options
  • Save pfmiles/2958323 to your computer and use it in GitHub Desktop.
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.
/**
* 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