Queue using Two Stacks

Sort by

recency

|

26 Discussions

|

  • + 0 comments

    i don't really understand why it's asking to do it with 2 stacks

    class Solution {
        private static List<int> oneStack = new List<int>();
    
        static void Main(String[] args) {
            var q = int.Parse(Console.In.ReadLine()!.TrimEnd()); // number of queries
            for(var i=0; i<q; i++){
                var a = Console.In.ReadLine()!.TrimEnd().Split(" ").Select(x => int.Parse(x)).ToArray();
                var t = a[0];
                switch(t){
                    case 1:
                        enqueue(a[1]);
                        break;
                    case 2:
                        dequeue();
                        break;
                    case 3: 
                        print();
                        break;
                    default:
                        throw new Exception($"t={t}");
                }
            }
        }
    
        static void enqueue(int x){
            oneStack.Add(x);
        }
    
        static void dequeue(){
            oneStack = oneStack.Skip(1).ToList();
        }
    
        static void print(){
            Console.WriteLine(oneStack.First());
        }
    }
    
  • + 0 comments

    My answer in Python

    q = int(input())
    inpts = []
    
    for i in range(q):
        inpts.append(input())
    
    
    ans = []
    
    for i in inpts:
        if i[0] == "1":
            j, num = i.split(" ")
            ans.append(int(num))
        elif i[0] == "2":
            ans.pop(0)
        elif i[0] == "3":
            print(ans[0])
    
  • + 0 comments

    Java and O(1)

    public static class QueueUsingTwoStacks<T> {
            private Stack<T> stack1; 
            private Stack<T> stack2; 
            
            public QueueUsingTwoStacks() {
                stack1 = new Stack<>();
                stack2 = new Stack<>();
            }
            
            public void enqueue(T element) {
                stack1.push(element);
            }
            
            public T dequeue() {
                if (stack2.isEmpty()) {
                    while (!stack1.isEmpty()) {
                        stack2.push(stack1.pop());
                    }
                }
                return stack2.pop();
            }
            
            public T peek() {
                if (stack2.isEmpty()) {
                    while (!stack1.isEmpty()) {
                        stack2.push(stack1.pop());
                    }
                }
                return stack2.peek();
            }
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int q = scanner.nextInt();
            QueueUsingTwoStacks<Integer> queue = new QueueUsingTwoStacks<>();
            
            for (int i = 0; i < q; i++) {
                int type = scanner.nextInt();
                if (type == 1) {
                    int x = scanner.nextInt();
                    queue.enqueue(x);
                } else if (type == 2) {
                    queue.dequeue();
                } else if (type == 3) {
                    System.out.println(queue.peek());
                }
            }
            scanner.close();
        }
    
  • + 0 comments

    JS

    Barely able to pass without the timeout error after trying multiple times:

    function processData(input) {
        let s1 = [], s2 = []
        let inputs = input.split('\n')
        for (let i = 1; i < inputs.length; i++) {
            let arr = inputs[i].split(' ')
            switch (arr[0]) {
                case '1': {
                    s1.push(arr[1])
                    break
                }
                case '2': {
                    while (s1.length > 0) s2.push(s1.pop())
                    s2.pop()
                    while (s2.length > 0) s1.push(s2.pop())
                    break
                }
                case '3': {
                    console.log(s1[0])
                    break
                }
            }
        }
    }
    
  • + 0 comments

    Java8 Straightforward and best time-complexity solution:

    import java.io.*;
    import java.util.*;
    
    public class Solution {
        private static Scanner scanner = new Scanner(System.in);
        private static Stack<Integer> s1 = new Stack<Integer>();
        private static Stack<Integer> s2 = new Stack<Integer>();
        private static void enqueue(int value){
                s1.push(value);
            }
            
        private static void dequeue(){
                if (s2.isEmpty() == true){
                    while (s1.isEmpty() == false){
                        int toS2 = s1.pop();
                        s2.push(toS2);
                    }
                }
                int returnValue = s2.pop();
            }
            
        private static void peak(){
                if (s2.isEmpty() == true){
                    while (s1.isEmpty() == false){
                        int toS2 = s1.pop();
                        s2.push(toS2);
                    }
                }
                System.out.println(s2.peek());
            }
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            int total = scanner.nextInt();
            for (int i = 0; i < total; i++) {
                int type = scanner.nextInt();
                if (type == 1){
                    enqueue(scanner.nextInt());
                }
                else if (type == 2){
                    dequeue();
                }
                else if (type == 3){
                    peak();
                }
            }
            
            
            
            
        }
    }