We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Queue using Two Stacks
- Discussions
Queue using Two Stacks
Queue using Two Stacks
+ 0 comments java 8
public static void main(String[] args) { Stack<Integer> stack1 = new Stack<>(); Stack<Integer> stack2 = new Stack<>(); Scanner scanner = new Scanner(System.in); while(scanner.hasNextLine()){ switch(scanner.nextInt()){ case 1: stack1.push(scanner.nextInt()); break; case 2: if(stack2.isEmpty()){ while(!stack1.isEmpty()){ stack2.push(stack1.pop()); } } if(!stack2.isEmpty()){ stack2.pop(); } break; case 3: if(stack2.isEmpty()){ while(!stack1.isEmpty()){ stack2.push(stack1.pop()); } } if(!stack2.isEmpty()){ ///---------- System.out.println(stack2.peek()); } break; } } scanner.close(); }
+ 0 comments PHP solution.
Can be change if -else statements to swich-case but it will be not more elegant i think. Suits all tests.
while($row = fgets(STDIN)){ $rawCommands[]= trim($row); } unset($rawCommands[0]); // do not need the commands counter $queue = array(); foreach($rawCommands as $aRawCommand) { $aCommand = explode(' ', trim($aRawCommand)); if ($aCommand[0] == 1) { $queue[]= $aCommand[1]; } else if ($aCommand[0] == 2) { array_shift($queue); } else if ($aCommand[0] == 3) { echo $queue[0] . "\n"; } }
+ 1 comment JS
class Queue{ constructor(){ this.queue = [] this.ops = [ ()=>{return null}, this.enqueue, this.dequeue, this.print_head, ] } enqueue = (val) => { this.queue.unshift(val) } dequeue = (val) => { return this.queue.pop(val) } print_head = () => { console.log(this.queue.slice(-1)[0]) } } function processData(input) { let q = new Queue() let ops = input.split('\n') let num_queries = ops.shift(); for ( op of ops ){ let [f, arg] = op.split(" ") q.ops[f](arg) } }
+ 0 comments My solution using Java8:
import java.io.*; import java.util.*; import java.util.stream.*; import java.math.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); List<String> lines = reader.lines().collect(Collectors.toList()); Stack<BigInteger> fifoStack = new Stack<BigInteger>(); Stack<BigInteger> lifoStack = new Stack<BigInteger>(); BigInteger fifoStackFirst = new BigInteger("0"); for(int i = 1; i < lines.size(); i++) { if (lines.get(i).contains(" ")) { // Enqueue at the end of the queueu BigInteger numToEnque = new BigInteger(lines.get(i).substring(2)); if (fifoStack.isEmpty()) { fifoStackFirst = numToEnque; } fifoStack.push(numToEnque); } else { if (Integer.parseInt(lines.get(i)) == 2) { // Dequeue at the front of the queue if (!lifoStack.isEmpty()) { lifoStack.pop(); continue; } while(fifoStack.size() > 1) { lifoStack.push(fifoStack.pop()); } fifoStack.pop(); } else { // Print front of stack if (!lifoStack.isEmpty()) { System.out.println(lifoStack.peek()); continue; } System.out.println(fifoStackFirst); } } } } }
+ 0 comments Another task with a very long description, a poor example, again with an error in the description(1 14 enqueue 42) and a bunch of ballast code. The solution itself: implementing a queue using two stacks is trivial.
Load more conversations
Sort 191 Discussions, By:
Please Login in order to post a comment