Constant-time Queue using Stack

Developer
Size
2,175 Kb
Views
4,048

How do I make an constant-time queue using stack?

What is a constant-time queue using stack? How do you make a constant-time queue using stack? This script and codes were developed by Seoh Char on 14 December 2022, Wednesday.

Constant-time Queue using Stack Previews

Constant-time Queue using Stack - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>constant-time Queue using Stack </title>
</head>
<body> <script src="js/index.js"></script>
</body>
</html>

Constant-time Queue using Stack - Script Codes JS Codes

'use strict';
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Stack = function () { function Stack() { _classCallCheck(this, Stack); this._buff = []; } Stack.prototype.push = function push(data) { this._buff.push(data); return this; }; Stack.prototype.pop = function pop() { return this._buff.pop(); }; Stack.prototype.isEmpty = function isEmpty() { return this._buff.length === 0; }; return Stack;
}();
var Queue = function () { function Queue() { _classCallCheck(this, Queue); this._stack = new Stack(); this._cache = new Stack(); } Queue.prototype.enqueue = function enqueue(data) { this._stack.push(data); }; Queue.prototype.dequeue = function dequeue() { if (this._cache.isEmpty()) { this.refill(); } return this._cache.pop(); }; Queue.prototype.refill = function refill() { while (!this._stack.isEmpty()) { this._cache.push(this._stack.pop()); } }; return Queue;
}();
var q = new Queue();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
var log = function log(line) { return document.body.innerHTML += line + '<br />';
};
log(q.dequeue());
log(q.dequeue());
log(q.dequeue());
Constant-time Queue using Stack - Script Codes
Constant-time Queue using Stack - Script Codes
Home Page Home
Developer Seoh Char
Username seoh
Uploaded December 14, 2022
Rating 3
Size 2,175 Kb
Views 4,048
Do you need developer help for Constant-time Queue using Stack?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Seoh Char (seoh) Script Codes
Create amazing love letters with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!