Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> JavaScript

JavaScript में डबल लिंक्ड लिस्ट क्लास


यहां डबल लिंक्ड लिस्ट क्लास का पूर्ण कार्यान्वयन है:

उदाहरण

class DoublyLinkedList {    constructor() {       this.head = null;       this.tail = null;       this.length = 0;    }    insert(data, position = this.length) {       let node = new this.Node(data);       // लिस्ट वर्तमान में खाली है       if (this.head === null) {          this.head = node;          this.tail = node;          this.length++;          return this.head;       }       // हेड पर इन्सर्शन       if (position == 0) {          node.prev = null;          node.next = this.head;          this.head.prev = node;          this.head = node;          return this.head;       }       let iter = 1;       let currNode = this.head;       while (currNode.next != null && iter < position) {          currNode = currNode.next; iter++;       }       // नए नोड को लिस्ट में अगले नोड की ओर इंगित करें       node.next = currNode.next;       // अगले नोड के प्रीवियस को नए नोड की ओर इंगित करें       if (currNode.next != null) {          currNode.next.prev = node;       }       // अपने नोड को प्रीवियस नोड की ओर इंगित करें       node.prev = currNode;       // प्रीवियस नोड के नेक्स्ट को नए नोड की ओर इंगित करें       currNode.next = node;       // जांचें कि क्या इन्सर्ट किया गया एलिमेंट टेल पर था, यदि हां तो टेल को उस पर इंगित करें       if (this.tail.next != null) {          this.tail = this.tail.next;       }       this.length++;       return node;    }    remove(data, position = 0) {       if (this.length === 0) {          console.log("List is already empty");          return;       }       this.length--;       let currNode = this.head;       if (position <= 0) {          this.head = this.head.next;          this.head.prev = null;       } else if (position >= this.length - 1) {          this.tail = this.tail.prev;          this.tail.next = null;       } else {          let iter = 0;          while (iter < position) {             currNode = currNode.next;             iter++;          }          currNode.next = currNode.next.next;          currNode.next.prev = currNode;       }       return currNode;    }    display() {       let currNode = this.head;       while (currNode != null) {          console.log(currNode.data + " <-> ");          currNode = currNode.next;       }    } } DoublyLinkedList.prototype.Node = class {    constructor(data) {       this.data = data;       this.next = null;       this.prev = null;    } };

  1. JavaScript का उपयोग करके लिंक्ड लिस्ट बनाना JavaScript का उपयोग करके लिंक्ड लिस्ट बनाना

    आइए एक साधारण क्लास को परिभाषित करके शुरू करें जिसमें एक कंस्ट्रक्टर हो जो हेड को null पर इनिशियलाइज़ करता है। हम LinkedList क्लास के प्रोटोटाइप पर एक और स्ट्रक्चर भी परिभाषित करेंगे जो लिंक्ड लिस्ट में प्रत्येक नोड का प्रतिनिधित्व करेगा। उदाहरण class LinkedList {    constructor() {  

  1. JavaScript में डबल लिंक्ड लिस्ट को सर्कुलर बनाना JavaScript में डबल लिंक्ड लिस्ट को सर्कुलर बनाना

    डबल लिंक्ड लिस्ट में, अंतिम नोड का नेक्स्ट पॉइंटर पहले नोड की ओर इंगित करता है और पहले नोड का प्रीवियस पॉइंटर अंतिम नोड की ओर इंगित करता है, जिससे यह दोनों दिशाओं में सर्कुलर बन जाता है। सर्कुलर लिंक्ड लिस्ट में इन्सर्शन्स और डिलीशन्स अन्य लिंक्ड लिस्ट के समान ही हैं। आपको बस लिंक्ड लिस्ट के किसी

  1. JavaScript में सिंगली लिंक्ड लिस्ट को सर्कुलर बनाना JavaScript में सिंगली लिंक्ड लिस्ट को सर्कुलर बनाना

    सिंगली लिंक्ड लिस्ट में, अंतिम नोड का नेक्स्ट पॉइंटर पहले नोड की ओर इंगित करता है।