> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/useful-java-knowledge/stack-and-queue.md).

# Stack & Queue

### Initialization

{% tabs %}
{% tab title="Stack" %}

```javascript
Stack<String> stack = new Stack<>(); 
```

{% endtab %}

{% tab title="Queue" %}

```java
Queue<String> queue = new LinkedList<>();
```

{% endtab %}
{% endtabs %}

### Useful methods

{% tabs %}
{% tab title="Stack" %}

```java
element = stack.push(element); // push one in, O(1)
element = stack.pop(); // pop one out, O(1)
element = stack.peek(); // see the top element, O(1)
stack.isEmpty(); // return true or false
l = stack.length();
```

{% endtab %}

{% tab title="Queue " %}

```java
q.offer(element);  // O(1), out of bound raise Exception
q.poll(); // O(1), out of bound raise Exception
l = q.size();

```

{% endtab %}
{% endtabs %}
