JavaScript資料結構與演算法-鏈表練習

來源:互聯網
上載者:User

標籤:reverse   back   insert   eve   mes   lis   javascrip   rip   顯示   

鏈表的實現一. 單向鏈表
// Node類function Node (element) {    this.element = element;    this.next = null;}// LinkedList類function LList () {    this.head = new Node('head');    this.find = find;    this.insert = insert;    this.findPrevious = findPrevious;    this.remove = remove;    this.display = display;}// 尋找function find (item) {    let currNode = this.head;    while (currNode.element !== item) {        currNode = currNode.next;    }    return currNode;}// 插入function insert (newElement, item) {    let newNode = new Node(newElement);    let current = this.find(item);    newNode.next = current.next;    current.next = newNode;}// 顯示function display () {    let currNode = this.head;    while (currNode.next !== null) {        currNode = currNode.next;        console.log(currNode.element);    }}// 檢查下一個節點function findPrevious (item) {    let currNode = this.head;    while (currNode.next !== null && currNode.next.element !== item) {        currNode = currNode.next;    }    return currNode;}// 刪除function remove (item) {    let prevNode = this.findPrevious(item);    if (prevNode.next !== null) {        prevNode.next = prevNode.next.next;    }}
二. 雙向鏈表
function Node (element) {    this.element = element;    this.next = null;    this.previous = null;}function DList () {    this.head = new Node('head');    this.find = find;    this.insert = insert;    this.display = display;    this.remove = remove;    this.findLast = findLast;    this.dispReverse = dispReverse;}function dispReverse () {    let currNode = this.head;    currNode = this.findLast();    while (currNode !== null && currNode.element !== 'head') {        console.log(currNode.element);        currNode = currNode.previous;    }}function findLast () {    let currNode = this.head;    while (currNode.next !== null) {        currNode = currNode.next;    }    return currNode;}function remove (item) {    let currNode = this.find(item);    if (currNode.next !== null) {        currNode.previous.next = currNode.next;        currNode.next.previous = currNode.previous;        currNode.next = null;        currNode.previous = null;    }}function display () {    let currNode = this.head;    while (currNode.next !== null) {        console.log(currNode.next.element);        currNode = currNode.next;    }}function find (item) {    let currNode = this.head;    while (currNode.element !== item) {        currNode = currNode.next;    }    return currNode;}function insert (newElement, item) {    let newNode = new Node(newElement);    let currNode  = this.find(item);    newNode.next = currNode.next;    newNode.previous = currNode;    currNode.next = newNode;}
三. 迴圈鏈表
// Node類function Node (element) {    this.element = element;    this.next = null;}// LinkedList類function CList () {    this.head = new Node('head');    // 修改    this.head.next = this.head;    this.find = find;    this.insert = insert;    this.findPrevious = findPrevious;    this.remove = remove;    this.display = display;}// 其他// ...
練習一. 實現advance(n)方法,使當前節點向前移動n個節點。
// 向前移動一位DList.prototype.goPrevious = function (thisNode) {    let node1, node2, node3, node4;    if (thisNode.previous.element !== 'head') {        node1 = thisNode.previous.previous;        node2 = thisNode.previous;        node3 = thisNode;        node4 = thisNode.next;        // 位置調整        node1.next = node3;        node3.previous = node1;        node3.next = node2;        node2.previous = node3;        node2.next = node4;        node4.previous = node2;    }};DList.prototype.advance = function (n, item) {    let currNode  = this.find(item);    while (n--) {        this.goPrevious(currNode);    }};// 樣本let names = new DList();names.insert('Mazey', 'head');names.insert('Cherrie', 'Mazey');names.insert('John', 'Cherrie');names.insert('Luna', 'John');names.insert('Ada', 'Luna');names.display();console.log('---');names.advance(2, 'Luna');names.display(); // Mazey, Luna, Cherrie, John, Ada
二. 實現back(n)方法,使當前節點向後移動n個節點。
// 向前移動一位DList.prototype.goNext = function (thisNode) {    let node1, node2, node3, node4;    if (thisNode.next.element !== null) {        node1 = thisNode.previous;        node2 = thisNode;        node3 = thisNode.next;        node4 = thisNode.next.next;        // 位置調整        node1.next = node3;        node3.previous = node1;        node3.next = node2;        node2.previous = node3;        node2.next = node4;        node4.previous = node2;    }};DList.prototype.back = function (n, item) {    let currNode  = this.find(item);    while (n--) {        this.goNext(currNode);    }};// 樣本let names = new DList();names.insert('Mazey', 'head');names.insert('Cherrie', 'Mazey');names.insert('John', 'Cherrie');names.insert('Luna', 'John');names.insert('Ada', 'Luna');names.display();console.log('---');names.back(2, 'Cherrie');names.display(); // Mazey, John, Luna, Cherrie, Ada

JavaScript資料結構與演算法-鏈表練習

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.