GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — array-stack.java/ArrayStack.java at master · moocf/array-stack.java Expand file tree/ Copy pathArrayStack.javaMore file actions 66 lines (59 loc) · 1.48 KB / Copy pathArrayStack.javaFile metadata and controls66 lines (59 loc) · 1.48 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 import java.nio.*; import java.util.concurrent.locks.*; // Array stack is a bounded lock-based stack using an // array. It uses a common lock for both push and pop // operations. class ArrayStack<T> { Lock lock; T[] data; int top; // lock: common lock for push, pop // data: array of values in stack // top: top of stack (0 if empty) @SuppressWarnings("unchecked") public ArrayStack(int capacity) { lock = new ReentrantLock(); data = (T[]) new Object[capacity]; top = 0; } // 1. Lock stack. // 2. Try push. // 3. Unlock stack. public void push(T x) throws BufferOverflowException { try { lock.lock(); // 1 tryPush(x); // 2 } finally { lock.unlock(); // 3 } } // 1. Lock stack. // 2. Try pop. // 3. Unlock stack. public T pop() throws BufferUnderflowException { try { lock.lock(); // 1 return tryPop(); // 2 } finally { lock.unlock(); // 3 } } // 1. Ensure stack is not full // 2. Save data at top. // 3. Increment top. protected void tryPush(T x) throws BufferOverflowException { if (top == data.length) // 1 throw new BufferOverflowException(); // 1 data[top++] = x; // 2, 3 } // 1. Ensure stack is not empty. // 2. Decrement top. // 3. Return data at top. protected T tryPop() throws BufferUnderflowException { if (top == 0) // 1 throw new BufferUnderflowException(); // 1 return data[--top]; // 2, 3 } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |