Queue using Two Stacks

Sort by

recency

|

234 Discussions

|

  • + 0 comments

    YES I CHEATED but it was fun to see it passing all test cases

    arr = []
    for i in range(int(input())):
        query = input().split(" ")
        if query[0] == "1":
            arr.append(query[1])
        elif query[0] == "2":
            arr.pop(0)
        else:
            print(arr[0])
    
  • + 0 comments

    Here is my c++ solution

    The idea behind moving items from stack 1 to stack 2 only when stack 2 is empty is because any item pushed unto stack 1 is the latest.

    When stack 2 is empty, and stack 1 is inverted unto stack 2 the oldest item is at the top of stack 2. Stack 2 keeps the order of oldest to newer from top to bottom.

    That way any item newer than bottom of stack 2 will be placed on stack 1.

    struct Queue {
       stack<long> s1, s2;
    
        // Enqueue an item to the queue
        void enQueue(long x)
        {
    
            // Push item into the first stack
            s1.push(x);
        }
    
        // Dequeue an item from the queue
        void deQueue()
        {
    
            int output ;
            if (s2.empty()) {
                while (!s1.empty()) {
                    s2.push(s1.top());
                    s1.pop();
                }
            }
            s2.pop();
    
        }
    
        int printFront(){
            long x = 0;
            if (s2.empty()){
                while (!s1.empty()) {
                    s2.push(s1.top());
                    s1.pop();
                }
            }
           
            x = s2.top();
            
            return x;
        }
    };
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
            Queue q;
        long input;
         int numQueries;
        long elementEnqueue;
        long printedOut;
        string outString;
        cin >> numQueries;
       vector<long>outputs;
    
        for(int i = 0; i<numQueries; i++){
    
            cin >> input;
    
            if(input == 1){
                cin >> elementEnqueue;
                q.enQueue(elementEnqueue);
            }
            if(input == 2){
                 q.deQueue();
            }
            if(input == 3){
               printedOut = q.printFront();
               outputs.push_back(printedOut);
            }
        }
    
        for(int i = 0; i < outputs.size(); i++){
            cout << outputs[i] << endl;
        }
       
    
        return 0;
    }
    
  • + 0 comments

    This is broken, you should be able to read in the args (in java) by just accessing the array, and if the system isnt set up for that, that part of the setup shouldn't be necessary for developmetn of the solution, which is the stacks.

  • + 1 comment
    public static void main(String[] args) throws IOException{
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int q = Integer.parseInt(br.readLine());
        Stack<Integer> in = new Stack<>();
        Stack<Integer> out = new Stack<>();
    
        while (q-- > 0) {
            String query = br.readLine();
            // Enqueue -> use in stack
            if (query.charAt(0) == '1') {
                int x = Integer.valueOf(query.substring(2, query.length()));
                in.push(x);
            } // Dequeue or Print(peek)
            // use out stack
            else {
                if (out.size() == 0) {
                    while (in.size() > 0) out.push(in.pop());
                }
                // Dequeue
                if (query.charAt(0) == '2') out.pop();
                else {
                    System.out.println(out.peek());
                }
            }
        }
    }
    
  • + 0 comments

    My Python solution:

    class Queue:
        def __init__(self):
            self.stack1 = []
            self.stack2 = []
            
        def enqueue(self, x):
            self.stack1.append(x)
        
        def dequeue(self):
            if not self.stack2:
                while self.stack1:
                    self.stack2.append(self.stack1.pop())
            return self.stack2.pop()
        
        def printqueue(self):
            if self.stack2:
                print(self.stack2[-1])
            else:
                print(self.stack1[0])
                
    if __name__ == "__main__":
        queue = Queue()
        n = int(input())
        for _ in range(n):
            query = input().strip().split()
            command = int(query[0])
            if command == 1:
                queue.enqueue(int(query[1]))
            elif command == 2:
                queue.dequeue()
            elif command == 3:
                queue.printqueue()