Last active
December 23, 2015 12:09
-
-
Save wszdwp/6633006 to your computer and use it in GitHub Desktop.
Coursera_Algorithm class practice: Generic stack using linkedlist implementation
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 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