Queues: A Tale of Two Stacks

Sort by

recency

|

422 Discussions

|

  • + 1 comment

    Python:

    class MyQueue(object):
        # FIFO
        def __init__(self):
            self.queue = []
        
        def peek(self):
            return self.queue[0]
            
        def pop(self):
            if len(self.queue) != 0:
                return self.queue.pop(0)
            
        def put(self, value):
            self.queue.append(value)
    
  • + 0 comments

    Solution in PHP

    <?php
    $_fp = fopen("php://stdin", "r");
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    
    $q = intval(trim(fgets(STDIN)));
    $array = [];
    for ($t_itr = 0; $t_itr < $q; $t_itr++) {
        $s = rtrim(fgets(STDIN), "\r\n");
        $arr = array_map('intval', preg_split('/ /', $s, -1, PREG_SPLIT_NO_EMPTY));
        if ($arr[0] == 1 && !empty($arr[1])) {
            array_unshift($array, $arr[1]);
        } elseif ($arr[0] == 2) {
            array_pop($array);
        } elseif ($arr[0] == 3) {
            echo $array[count($array)- 1] . PHP_EOL;
        }
    }
    fclose($_fp);
    ?>
    
  • + 0 comments

    javascript with customized Queue

    class Queue {
      constructor() {
        this.inbox = []
        this.outbox = []
      }
    
      put(element) {
        this.inbox.push(element)
      }
    
      pop() {
        if (this.outbox.length == 0) {
          while (this.inbox.length) this.outbox.push(this.inbox.pop())
        }
        return this.outbox.pop()
      }
    
      peek() {
        if (this.outbox.length == 0) {
          while (this.inbox.length) this.outbox.push(this.inbox.pop())
        }
        return this.outbox[this.outbox.length - 1]
      }
    }
    
    function processData(input) {
      let lines = input.split('\n').slice(1)
      let queue = new Queue()
    
      for (let i = 0; i < lines.length; i++) {
        let [op, n] = lines[i].split(' ')
        switch (op) {
          case '1': queue.put(n); break
          case '2': queue.pop(); break
          case '3': console.log(queue.peek())
        }
      }
    }
    
  • + 0 comments

    JavaScript

    class Queue {
      constructor() {
        this.result = [];
      }
      enque(value) {
        this.result.push(value);
      }
    
      dequeue() {
        if (this.result.length != 0) {
          this.result.shift();
        }
      }
    
      print() {
        if (this.result.length != 0) {
          console.log(this.result[0]);
        }
      }
    }
    function processData(input) {
      let inputArr = input.split("\n");
      let tab = new Queue();
      for (let i = 1; i < inputArr.length; i++) {
        switch (inputArr[i][0]) {
          case "1":
            let value = inputArr[i].slice(1).trim();
            tab.enque(value);
            break;
          case "2":
            tab.dequeue();
            break;
          case "3":
            tab.print();
            break;
        }
      }
    }
    
  • + 1 comment

    With some of these challenges, I have no idea where to start and no idea how anyone else does either, like my brain isn't wired in the necessary way. I suspected this would be another example but on this occasion I'm baffled as to why this is a challenge at all. The simplistic solution below works for all test cases.

    class MyQueue(object):
        def __init__(self):
            self.queue = []
            
        
        def peek(self):
            return self.queue[0]
            
        def pop(self):
            del self.queue[0]
            
            
        def put(self, value):
            self.queue.append(value)