यहां स्टैक क्लास का पूर्ण कार्यान्वयन है:
उदाहरण
class Stack { constructor(maxSize) { // यदि प्रदान नहीं किया गया तो डिफ़ॉल्ट अधिकतम आकार सेट करें if (isNaN(maxSize)) { maxSize = 10; } this.maxSize = maxSize; // एक एरे इनिशियलाइज़ करें जिसमें स्टैक मान होंगे। this.container = []; } display() { console.log(this.container); } isEmpty() { return this.container.length === 0; } isFull() { return this.container.length >= this.maxSize; } push(element) { // जांचें कि स्टैक भरा हुआ है या नहीं if (this.isFull()) { console.log("Stack Overflow!") return; } this.container.push(element) } pop() { // जांचें कि खाली है या नहीं if (this.isEmpty()) { console.log("Stack Underflow!") return; } this.container.pop() } peek() { if (isEmpty()) { console.log("Stack Underflow!"); return; } return this.container[this.container.length - 1]; } clear() { this.container = []; } }