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
Queue using Two Stacks
Sort by
recency
|
511 Discussions
|
Please Login in order to post a comment
C solution:
typescript boilerplate:
`use strict";
process.stdin.resume(); process.stdin.setEncoding("utf-8"); let inputString: string = ""; let inputLines: string[] = []; let currentLine: number = 0; process.stdin.on("data", function (inputStdin: string): void { inputString += inputStdin; });
process.stdin.on("end", function (): void { inputLines = inputString.split("\n"); inputString = ""; main(); });
function readLine(): string { return inputLines[currentLine++]; }
class stackQueue { private s1: number[] = []; private s2: number[] = [];
constructor() { this.s1 = []; this.s2 = []; }
enqueue(x: number) { // Enter your code here }
dequeue() { // Enter your code here }
print() { // Enter your code here } }
function main() { const testQueue = new stackQueue(); const commandMap = { "1": "enqueue", "2": "dequeue", "3": "print" } as const;
inputLines.shift();
inputLines.map((inputLine: string) => { const [command, input] = inputLine.split(" ") as ["1" | "2" | "3", string]; const method = commandMap[command]; if (method == "enqueue") { return testQueue.enqueue(Number(input)); } return testQueuemethod; }); }
`
javascript boilerplate "use strict";
process.stdin.resume(); process.stdin.setEncoding("utf-8"); let inputString = ""; let inputLines = []; let currentLine = 0; process.stdin.on("data", function (inputStdin) { inputString += inputStdin; });
process.stdin.on("end", function () { inputLines = inputString.split("\n"); inputString = ""; main(); });
function readLine() { return inputLines[currentLine++]; }
class stackQueue { constructor() { this.s1 = []; this.s2 = []; }
enqueue(x) { // Enter your code here }
dequeue() { // Enter your code here }
print() { // Enter your code here } }
function main() { const testQueue = new stackQueue(); const commandMap = { 1: "enqueue", 2: "dequeue", 3: "print" };
inputLines.shift();
inputLines.map((inputLine) => { const [command, input] = inputLine.split(" "); const method = commandMap[command]; if (method == "enqueue") { return testQueue.enqueue(Number(input)); } return testQueuemethod; }); }
`
Java solution
public class Solution { private Stack stackEnqueue = new Stack<>(); private Stack stackDequeue = new Stack<>();
}
Using Python3