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