{"id":22513752,"url":"https://github.com/emahtab/implement-queue-using-stacks","last_synced_at":"2026-02-02T14:33:09.175Z","repository":{"id":79525549,"uuid":"242364673","full_name":"eMahtab/implement-queue-using-stacks","owner":"eMahtab","description":"Implement Queue using Stacks","archived":false,"fork":false,"pushed_at":"2020-02-22T16:20:20.000Z","size":12,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-20T11:45:41.603Z","etag":null,"topics":["leetcode","problem-solving","queue","stack"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-02-22T15:29:43.000Z","updated_at":"2020-02-22T16:20:22.000Z","dependencies_parsed_at":"2023-05-10T17:15:40.006Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/implement-queue-using-stacks","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/implement-queue-using-stacks","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fimplement-queue-using-stacks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fimplement-queue-using-stacks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fimplement-queue-using-stacks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fimplement-queue-using-stacks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/implement-queue-using-stacks/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fimplement-queue-using-stacks/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29013031,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-02T12:48:30.580Z","status":"ssl_error","status_checked_at":"2026-02-02T12:46:38.384Z","response_time":58,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["leetcode","problem-solving","queue","stack"],"created_at":"2024-12-07T03:14:21.373Z","updated_at":"2026-02-02T14:33:09.164Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Implement Queue using Stacks\n## https://leetcode.com/problems/implement-queue-using-stacks\n\nImplement the following operations of a queue using stacks.\n\n1. push(x) -- Push element x to the back of queue.\n2. pop() -- Removes the element from in front of queue.\n3. peek() -- Get the front element.\n4. empty() -- Return whether the queue is empty.\n\n```\nExample:\n\nMyQueue queue = new MyQueue();\n\nqueue.push(1);\nqueue.push(2);  \nqueue.peek();  // returns 1\nqueue.pop();   // returns 1\nqueue.empty(); // returns false\n```\n**Notes:**\n\n1. You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.\n2. Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.\n3. You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).\n\n## Implementation 1 : 2 Stacks\n\n```java\nclass MyQueue {\n\n    /** Initialize your data structure here. */\n    private Stack\u003cInteger\u003e stack = new Stack\u003c\u003e();\n   \n    /** Push element x to the back of queue. */\n    public void push(int x) {\n        stack.push(x);\n    }\n    \n    /** Removes the element from in front of queue and returns that element. */\n    public int pop() {\n        Stack\u003cInteger\u003e temp = new Stack\u003c\u003e();\n        while(!stack.isEmpty()){\n            temp.push(stack.pop());\n        }\n        int top = temp.pop();\n        while(!temp.isEmpty()){\n            stack.push(temp.pop());\n        }\n        return top;\n    }\n    \n    /** Get the front element. */\n    public int peek() {\n        Stack\u003cInteger\u003e temp = new Stack\u003c\u003e();\n        while(!stack.isEmpty()){\n            temp.push(stack.pop());\n        }\n        int top = temp.peek();\n        while(!temp.isEmpty()){\n            stack.push(temp.pop());\n        }\n        return top;\n    }\n    \n    /** Returns whether the queue is empty. */\n    public boolean empty() {\n        return stack.isEmpty();\n    }\n}\n```\n## Implementation 1 : 2 Stacks (Improvements)\n```java\nclass MyQueue {\n\n    /** Initialize your data structure here. */\n    private Stack\u003cInteger\u003e stack = new Stack\u003c\u003e();\n    private Stack\u003cInteger\u003e auxiliaryStack = new Stack\u003c\u003e();\n    private int front;\n    \n    /** Push element x to the back of queue. */\n    public void push(int x) {\n        if(stack.isEmpty())\n            front = x;\n        stack.push(x);\n    }\n    \n    /** Removes the element from in front of queue and returns that element. */\n    public int pop() {\n        while(!stack.isEmpty()){\n            auxiliaryStack.push(stack.pop());\n        }\n        int top = auxiliaryStack.pop();\n        while(!auxiliaryStack.isEmpty()){ \n            if(stack.isEmpty())\n                front = auxiliaryStack.peek();\n            stack.push(auxiliaryStack.pop());\n        }\n        return top;\n    }\n    \n    /** Get the front element. */\n    public int peek() {\n       return front;\n    }\n    \n    /** Returns whether the queue is empty. */\n    public boolean empty() {\n        return stack.isEmpty();\n    }\n}\n```\n\n## Implementation 2 : 2 Stacks (Much better approach)\n```java\nclass MyQueue {\n\n    /** Initialize your data structure here. */\n    private Stack\u003cInteger\u003e stack = new Stack\u003c\u003e();\n    private Stack\u003cInteger\u003e auxiliaryStack = new Stack\u003c\u003e();\n    private int front;\n    \n    /** Push element x to the back of queue. */\n    public void push(int x) {\n        if(stack.isEmpty())\n            front = x;\n        stack.push(x);\n    }\n    \n    /** Removes the element from in front of queue and returns that element. */\n    public int pop() {\n        if (auxiliaryStack.isEmpty()) {\n            while (!stack.isEmpty())\n              auxiliaryStack.push(stack.pop());\n        }\n        return auxiliaryStack.pop();  \n    }\n    \n    /** Get the front element. */\n    public int peek() {\n       if (!auxiliaryStack.isEmpty()) {\n            return auxiliaryStack.peek();\n        }\n       return front;\n    }\n    \n    /** Returns whether the queue is empty. */\n    public boolean empty() {\n        return stack.isEmpty() \u0026\u0026 auxiliaryStack.isEmpty();\n    }\n}\n\n```\n\n# References :\n1. https://www.youtube.com/watch?v=Wg8IiY1LbII\n2. https://leetcode.com/articles/implement-queue-using-stacks\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fimplement-queue-using-stacks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fimplement-queue-using-stacks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fimplement-queue-using-stacks/lists"}