Sort by

recency

|

508 Discussions

|

  • + 1 comment

    Java solution

    public class Solution { private Stack stackEnqueue = new Stack<>(); private Stack stackDequeue = new Stack<>();

    // Enqueue
    public void enqueue(int n){
        stackEnqueue.push(n);
    }
    
    // Dequeue
    public void dequeue(){
        reverseStack();
        if(!stackDequeue.isEmpty()){
            stackDequeue.pop();
        }
    }
    
    //Reverse
    private void reverseStack(){
        if(stackDequeue.isEmpty()){
            while(!stackEnqueue.isEmpty()){
                stackDequeue.push(stackEnqueue.pop());
            }
        }
    }
    
    //Print front element
    public void printFrontElement(){
        reverseStack();
        if(!stackDequeue.isEmpty()){
            System.out.println(stackDequeue.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. */
        Scanner scanner = new Scanner((System.in));
        Solution twoStackQueue = new Solution();
        int count = scanner.nextInt();
        for(int i = 0; i< count; i++){
            int choice = scanner.nextInt();
            switch (choice){
                case 1:
                    int enqueueNumber = scanner.nextInt();
                    twoStackQueue.enqueue(enqueueNumber);
                    break;
                case 2:
                    twoStackQueue.dequeue();
                    break;
                case 3:
                    twoStackQueue.printFrontElement();
                    break;
            }
        }
        scanner.close();
    }
    

    }

  • + 0 comments

    Using Python3

    old, new = [], []
    for _ in range(int(input())):
        val = list(map(int,input().split()))
        if val[0] == 1:
            new.append(val[1])
        elif val[0] == 2:
            if not old :
                while new : old.append(new.pop())
            old.pop()
        else:
            print(old[-1] if old else new[0])
    
  • + 0 comments

    Use perl language:

    It's a one liner code.

    while(<>){(v)=split;v:c==3&&print"$q[0]\n"}

  • + 0 comments

    For Java 7.

    import java.util.Scanner; import java.util.Stack;

    class Queue { Stack getIn = new Stack<>(); Stack getOut = new Stack<>(); int size = 0;

    Queue() {}
    
    public int size() {
        return size;
        // This cause i update size when enqueue and dequeue already
        // Of course, you can just do:  getIn.size() + getOut.size()
    }
    
    public boolean isEmpty() {
        return size == 0;
    }
    
    public void Enqueue(int value) {
        getIn.add(value);
        size++;
    
        // First, item will be contain in getIn. More detail on Dequeue.
    }
    
    public int Dequeue() {
        if (getOut.size() == 0) {
            int n = getIn.size();
            for (; n > 0; n--) {
                getOut.add(getIn.pop());
            }
        }
        size--;
        return getOut.pop();
    
        // If getOut is empty, we can take nothing out from it. So, check for more item from getIn
        // Notice: never push from getIn to getOut while getOut is not Empty. It'll break the order
    }
    
    public int peek() {
        if (getOut.size() == 0) {
            int n = getIn.size();
            for (; n > 0; n--) {
                getOut.add(getIn.pop());
            }
        }
        return getOut.peek();
    
        // Similar to dequeue
    }
    

    }

    public class Solution { public final static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int q = sc.nextInt();
    
        int ops;
        int newValue;
        Queue queue = new Queue();
        for (; q>0; q--) {
            ops = sc.nextInt();
            switch(ops) {
                case 1: // Enqueue
                    newValue = sc.nextInt();
                    queue.Enqueue(newValue);
                    break;
                case 2: // Dequeue
                    queue.Dequeue();
                    break;
                case 3: // peek and print
                    System.out.println(queue.peek());
                    break;
                default:
            }
        }
    
        sc.close();
    }
    
  • + 1 comment
    import sys
    
    class Stack:
        def __init__(self):
            self.container = []
        
        def is_empty(self) -> bool:
            if len(self.container) == 0:
                return True
            return False
            
        def push(self, num) -> None:
            self.container.append(num)
            
        def pop(self) -> int:
            val = None
            if not self.is_empty():
                val = self.container.pop()
            return val
        
        def top(self) -> int:
            val = None
            if not self.is_empty():
                val = self.container[-1]
            return val
            
    
    class Queue:
        
        def __init__(self):
            self.secondary = Stack()
            self.primary = Stack()
            
        def enqueue(self, num) -> None:
            if self.primary.is_empty():
                self.primary.push(num)
            else:
                while (not self.primary.is_empty()):
                    self.secondary.push(self.primary.pop())
                self.primary.push(num)
                while (not self.secondary.is_empty()):
                    self.primary.push(self.secondary.pop())
        
        def dequeue(self) -> int:
            if not self.primary.is_empty():
                return self.primary.pop()
            return None
        
        def head(self) -> int:
            if not self.primary.is_empty():
                return self.primary.top()
            return None
            
    queue = Queue()
    count = 0
    for line in sys.stdin:
        if count == 0:
            no_of_inputs = int(line)
        elif count > no_of_inputs:
            break
        else:
            query = line.split()
            if int(query[0]) == 1:
                queue.enqueue(int(query[1]))
            elif int(query[0]) == 2:
                value = queue.dequeue()
            else:
                print(str(queue.head()))
            
        
        count += 1