Skip to content

Instantly share code, notes, and snippets.

@AlexAbraham1
Created January 2, 2015 18:12
Show Gist options
  • Save AlexAbraham1/7552c342e16572c04b00 to your computer and use it in GitHub Desktop.
Save AlexAbraham1/7552c342e16572c04b00 to your computer and use it in GitHub Desktop.
public class StackList {
StackListNode first;
int size;
public static void main(String[] args) {
StackList list = new StackList();
list.push(1);
list.push(2);
list.push(3);
System.out.println(list.peek());
System.out.println(list.pop());
System.out.println(list.peek());
System.out.println(list.pop());
System.out.println(list.peek());
System.out.println(list.pop());
System.out.println(list.peek());
System.out.println(list.pop());
}
public StackList()
{
this.size = 0;
}
public void push(Object o)
{
StackListNode node = new StackListNode(o, first);
first = node;
this.size++;
}
public Object pop()
{
if (!isEmpty()) {
Object info = first.info;
first = first.next;
this.size--;
return info;
}
return null;
}
public Object peek()
{
if (first != null) {
return first.info;
}
return null;
}
public boolean isEmpty()
{
return size == 0;
}
class StackListNode {
Object info;
StackListNode next;
StackListNode(Object info, StackListNode next) {
this.info = info;
this.next = next;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment