Wednesday, February 9, 2011

My very simple Array-based Stack

// array-based stack
class JArrayStack
{
int capacity = 100;
int a[] = new int [100];
int i = 0;

public void push(int x) { a[i++] = x; }
public void pop() { i--; }
public int top() { return a[i-1]; }
public boolean isFull() { return i == capacity; }
public boolean isEmpty() { return i == 0; }
public int size() { return i; }
}

int [] temp = new int [capacity * 2];
copy from a into temp using for loop or memcopy
a = temp
capacity *= 2

No comments:

Post a Comment