> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/algo/queue-and-stack/implement-queue-by-interface.md).

# Implement Queue by Interface

{% embed url="<https://www.lintcode.com/problem/546/>" %}

## Solution 1 (Java)

```java
interface InterfaceQueue {
    void push(int element);

    // define an interface for pop method
    /* write your code here */
    int pop();

    // define an interface for top method
    /* write your code here */
    int top();
}

class Node {
    public int val;
    public Node prev, next;
    public Node(int val) {
        this.val = val;
        prev = next = null;
    }
}

public class MyQueue implements InterfaceQueue {
    /* you can declare your private attributes here */
    private Node first, last;
    public MyQueue() {
       first = last = null;
    }

    // implement the push method
    /* write your code here */
    @Override
    public void push(int val) {
       if (last == null) {
           last = new Node(val);
           first = last;
       } else {
           Node newNode = new Node(val);
           last.next = newNode;
           newNode.prev = last;
           last = newNode;
       }
    }
	
    // implement the pop method
    /* write your code here */
    @Override
    public int pop() {
        if (first == null)
            return -1;
        int popVal = first.val;
        first = first.next;
        if (first == null) {
            last = null;
        } else {
            first.prev = null;
        }
        return popVal;
    }
    
	// implement the top method
    /* write your code here */
    @Override
    public int top() {
        if (first == null)
            return -1;
        return first.val;
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue queue = new MyQueue();
 * queue.push(123);
 * queue.top(); will return 123;
 * queue.pop(); will return 123 and pop the first element in the queue
 */
```


---

# 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://blog.yushunchen.com/algo/queue-and-stack/implement-queue-by-interface.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.
