# Implement Stack by Array

```java
public class Stack {
    private int[] array;
    private int head;
    public Stack(int cap) {
        array = new int[cap];
        head = array.length;
    }
    public boolean push(int ele){
        if (head == -1) return fasle;
        head--;
        array[head] = ele;
        return true;
    }
    public Integer pop() {
        if (head = array.length) {
            return null;
        }
        int curValue = array[head];
        head--;
        return curValue;
    }
    public Integer top() {
        if (head = array.length) {
            return null;
        }
        int curValue = array[head];
        return curValue;
    }
    public int size() {
        return array.length - head;
    }
    public int isEmpty() {
        return size() == 0;
    }
    public int isFull() {
        return size() == array.length;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://algo.younglimit.com/queue-and-stack-and-pq/practice-3-stack-and-queue-and-deque-design/implement-stack-by-array.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
