Created
January 2, 2015 18:12
-
-
Save AlexAbraham1/7552c342e16572c04b00 to your computer and use it in GitHub Desktop.
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
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