Skip to content

Instantly share code, notes, and snippets.

@wszdwp
Last active December 23, 2015 12:09
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 wszdwp/6633006 to your computer and use it in GitHub Desktop.
Save wszdwp/6633006 to your computer and use it in GitHub Desktop.
Coursera_Algorithm class practice: Generic stack using linkedlist implementation
public class MyStack<Item>
{
private Node first = null;
private class Node
{
Item item;
Node next;
}
public boolean isEmpty() {
return first == null;
}
public void push(Item item) {
Node oldFirst = first;
first = new Node();
first.item = item;
first.next = oldFirst;
}
public Item pop() {
if(first == null) return null;
Item tmp = first.item;
first = first.next;
return tmp;
}
public static void main(String[] args) {
MyStack<Integer> s1 = new MyStack<Integer>();
s1.push(1);
s1.push(2);
s1.push(3);
System.out.println(s1.pop());
System.out.println(s1.pop());
System.out.println(s1.pop());
System.out.println(s1.pop());
System.out.println(s1.isEmpty());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment