Skip to content

Instantly share code, notes, and snippets.

Created February 20, 2012 20:20
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 anonymous/6ad1bc384d6662423bfe to your computer and use it in GitHub Desktop.
Save anonymous/6ad1bc384d6662423bfe to your computer and use it in GitHub Desktop.
public class LinkedStack<T> {
private static class Nude<U> {
private U item;
private Nude<U> next;
Nude() { item = null; next = null; }
Nude(U item, Nude<U> next) { this.item = item; this.next = next; }
boolean end() { return item == null && next == null; }
}
private Nude<T> top = new Nude<T>();
public void push(T item) { top = new Nude<T>(item, top); }
public T pop() {
T s = top.item;
if (!top.end())
top = top.next;
return s;
}
public static void main(String[] args) {
LinkedStack<String> string = new LinkedStack<String>();
for(String s : "Phasers on stun!".split(" "))
string.push(s);
String s;
while((s = string.pop()) != null)
System.out.print("'" + s + "' ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment