Sort by

recency

|

505 Discussions

|

  • + 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
    
  • + 0 comments
    class QueueUsingTwoStacks:
        def __init__(self):
            self.primary_stack = []
            self.temp_stack = []
            
        def enqueue(self, item):
            self.primary_stack.append(item)
            
        def dequeue(self):
            if self.is_empty():
                return None
            
            if not self.temp_stack:
                while self.primary_stack:
                    self.temp_stack.append(self.primary_stack.pop())
                
            return self.temp_stack.pop()
            
        def peek(self):
            if self.is_empty():
                return None
            
            if not self.temp_stack:
                while self.primary_stack:
                    self.temp_stack.append(self.primary_stack.pop())
                
            return self.temp_stack[-1]
            
        def is_empty(self):
            return len(self.primary_stack) == 0 and len(self.temp_stack) == 0
            
    if __name__ == '__main__':
        queue = QueueUsingTwoStacks()
        Q = int(input())
        queries = []
        for _ in range(Q):
            queries.append(list(map(int, input().split())))
    
        for query in queries:
            cmd = query[0]
            if cmd == 1:
                item = query[1]
                queue.enqueue(item)
            elif cmd == 2:
                queue.dequeue()
            else:
                print(queue.peek())
    
  • + 0 comments

    Typescript with two queues

    class Queue {
        private sIn: number[] = []
        private sOut: number[] = []
        private top: number
        
        add(e: number) {
            if (this.sIn.length === 0) this.top = e
            this.sIn.push(e)
        }
        
        pop(): number {
            if (this.sOut.length === 0) {
                while(this.sIn.length > 0) {
                    this.sOut.push(this.sIn.pop())
                }
            }
            return this.sOut.pop()
        }
        
        peek():number {
            return this.sOut[this.sOut.length - 1] ?? this.top
        }
    }
    
    function handler() {
        const q = new Queue()
        for (let i = 1; i < inputLines.length; i++) {
            const [c, p] = inputLines[i].split(' ')
            if (c === '1') q.add(parseInt(p))
            else if (c === '2') q.pop()
            else console.log(q.peek())
        }
    }
    
  • + 1 comment

    perl:

    while(<>){($c,$v)=split;$c==1?push@q,$v:$c==2?shift@q:$c==3&&print"$q[0]\n"}