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/6633065 to your computer and use it in GitHub Desktop.
Save wszdwp/6633065 to your computer and use it in GitHub Desktop.
Coursera_Algorithm class practice: Generic stack using fixed size array implementation
public class MyArrayStack<Item>
{
private Item[] s;
private int index;
public MyArrayStack(int capacity) {
s = (Item[]) new Object[capacity];
index = 0;
}
public boolean isEmpty() {
return index == 0;
}
public void push(Item item) {
s[index++] = item;
}
public Item pop() {
if( index == 0 ) return null;
return s[--index];
}
public static void main(String[] args) {
MyArrayStack<Integer> s1 = new MyArrayStack<Integer>(5);
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