Skip to content

Instantly share code, notes, and snippets.

@dimkir
Created November 13, 2013 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimkir/7449487 to your computer and use it in GitHub Desktop.
Save dimkir/7449487 to your computer and use it in GitHub Desktop.
Processing sketch : stackIterator
Stack<String> stack;
void draw(){
background(0);
text(stack.pop(), width/2, height/2 );
stack.push(mouseX + ", " + mouseY);
}
int CW = 800;
int CH = 600;
void setup(){
size(CW, CH);
stack = new Stack();
stack.push("Hello");
}
//#GIST:7449487
import java.util.Iterator;
class Stack<Item>
implements Iterable<Item>
{
Node<Item> current;
Iterator<Item> iterator(){
return new StackIterator<Item>();
// return null;
}
boolean isEmpty(){
return ( current == null);
}
void push(Item it){
Node<Item> n = new Node<Item>(current, it);
current = n;
}
Item pop(){
if ( current == null){
throw new RuntimeException("Can't pop, stack is null");
}
Item res = current.item;
current = current.next;
return res;
}
class StackIterator<Item1> implements Iterator<Item1>
{
boolean hasNext(){
throw new RuntimeException("un");
}
Item1 next(){
throw new RuntimeException("un");
}
void remove(){
throw new RuntimeException("un");
}
}
}
class Node<Item>
{
Node next;
Item item;
Node(Node node, Item it){
next = node;
item = it;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment