यहां क्यू क्लास का पूर्ण कार्यान्वयन है:
उदाहरण
class Queue { 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; } enqueue(element) { // जांचें कि क्यू भरा हुआ है या नहीं if (this.isFull()) { console.log("Queue Overflow!"); return; } // चूंकि हम एलिमेंट्स को अंत में जोड़ना चाहते हैं, हम बस उन्हें पुश करेंगे। this.container.push(element); } dequeue() { // जांचें कि खाली है या नहीं if (this.isEmpty()) { console.log("Queue Underflow!"); return; } return this.container.shift(); } peek() { if (this.isEmpty()) { console.log("Queue Underflow!"); return; } return this.container[0]; } clear() { this.container = []; } }