> For the complete documentation index, see [llms.txt](https://ondrej-kvasnovsky-2.gitbook.io/algorithms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ondrej-kvasnovsky-2.gitbook.io/algorithms/data-structures/queue.md).

# Queue

Queue is first in last out.

We can implement it using the same approaches as in Stack, linked list or array. ![](/files/-M3wYchA5raIFUzLLPpy)Solution

Here is an implementation of Queue in java.

```
package datastructures;

public class QueueTest {

    public static void main(String[] args) {
        MyQueue q = new MyQueue();
        q.push(1);
        q.push(2);
        System.out.println(q.poll()); // 1
        q.push(3);
        System.out.println(q.poll()); // 2
        System.out.println(q.poll()); // 3
    }
}

class MyQueue {
    MyQueueNode head;
    MyQueueNode tail;

    void push(int i) {
        MyQueueNode n = new MyQueueNode(i);
        if (head == null) {
            head = n;
            tail = n;
        } else {
            tail.next = n;
            tail = n;
        }
    }

    int poll() {
        if (head == null) return -1;
        else {
            int val = head.val;
            head = head.next;
            return val;
        }
    }
}

class MyQueueNode {
    int val;
    MyQueueNode next;

    public MyQueueNode(int val) {
        this.val = val;
    }
}
```

Here is a naive implementation of queue in Kotlin. If we know the items upfront, we could do this.

```
class Queue<T>(vararg items: T) {
    private val values = items.toMutableList()
    fun enqueue(item: T) {
        values.add(item)
    }

    fun dequeue(): T? {
        if (values.isNotEmpty()) {
            return values.removeAt(0)
        }
        return null
    }
}

fun <T> queueOf(vararg items: T): Queue<T> {
    return Queue(*items) // * is spread operator
}

fun main(args: Array<String>) {
    val queue = queueOf(1, 2)
    queue.enqueue(3)
    println(queue.dequeue())
    println(queue.dequeue())
    println(queue.dequeue())
    println(queue.dequeue())
}
```

Here is the output for illustration what is going on.

```
1
2
3
null
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://ondrej-kvasnovsky-2.gitbook.io/algorithms/data-structures/queue.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
