第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從鏈表中刪除奇數(shù)/偶數(shù)

從鏈表中刪除奇數(shù)/偶數(shù)

不負(fù)相思意 2021-11-18 20:41:43
這是一個帶有數(shù)據(jù)和下一個屬性的標(biāo)準(zhǔn)鏈表。這就是我正在嘗試的:class Node {    constructor(data, next) {        this.data = data;        this.next = next;    }}class LinkedList {    constructor() {        this.head = null;    }    insertFirst(data) {        this.head = new Node(data, this.head);    }    size() {        let counter = 0, node = this.head;        while (node) {            counter++;            node = node.next;        }        return counter;    }    toArray() {        let node = this.head;        const result = [];        while (node) {            result.push(node.data);            node = node.next;        }        return result;    }    removeEven() {        let previous = this.head;        let node = this.head.next;        if (this.isEven(previous.data)) {            console.log('outside loop, found one: ' + previous.data)            this.head = this.head.next;        }        while (node) {            if (this.isEven(node.data)) {                 console.log('found ' + node.data);                 previous.next = node.next;            }            node = node.next;        }    }    isEven(num) { return num % 2 === 0 ? true : false; }}const q = new LinkedList();q.insertFirst(16)q.insertFirst(3)q.insertFirst(4)q.insertFirst(7)q.insertFirst(5)q.insertFirst(2)q.insertFirst(1)q.insertFirst(15)q.insertFirst(18)q.removeEven();console.log(q.toArray());和輸出:outside loop, found one: 18found 2found 4found 16[ 15, 1, 2, 5, 7, 4, 3, 16 ] 所以它只刪除循環(huán)外的第一個值,我如何刪除其他值?編輯:添加了完整代碼,但是,它要求我添加更多文本,除了我已經(jīng)添加的內(nèi)容之外,我沒有更多要添加的內(nèi)容。
查看完整描述

3 回答

?
慕神8447489

TA貢獻(xiàn)1780條經(jīng)驗 獲得超1個贊

你應(yīng)該previous在循環(huán)中更新。


class Node {

  constructor(data, next) {

    this.data = data;

    this.next = next;

  }

}


class LinkedList {

  constructor() {

    this.head = null;

  }


  insertFirst(data) {

    this.head = new Node(data, this.head);

  }


  size() {

    let counter = 0,

      node = this.head;


    while (node) {

      counter++;

      node = node.next;

    }


    return counter;

  }


  toArray() {

    let node = this.head;

    const result = [];


    while (node) {

      result.push(node.data);

      node = node.next;

    }


    return result;

  }


  removeEven() {

    let previous = this.head;

    let node = this.head.next;


    if (this.isEven(previous.data)) {

      console.log('outside loop, found one: ' + previous.data)

      this.head = this.head.next;

    }


    while (node) {

      if (this.isEven(node.data)) {

        console.log('found ' + node.data);

        previous.next = node.next;

      } else {

        previous = node;

      }

      node = node.next;

    }


  }


  removeOdd() {

    let previous = this.head;

    let node = this.head.next;


    if (!this.isEven(previous.data)) {

      console.log('outside loop, found one: ' + previous.data)

      this.head = this.head.next;

    }


    while (node) {

      if (!this.isEven(node.data)) {

        console.log('found ' + node.data);

        previous.next = node.next;

      } else {

        previous = node;

      }

      node = node.next;

    }


  }


  isEven(num) {

    return num % 2 === 0 ? true : false;

  }

}


const q = new LinkedList();

q.insertFirst(16)

q.insertFirst(3)

q.insertFirst(4)

q.insertFirst(7)

q.insertFirst(5)

q.insertFirst(2)

q.insertFirst(1)

q.insertFirst(15)

q.insertFirst(18)

q.removeOdd();


console.log(q.toArray());



查看完整回答
反對 回復(fù) 2021-11-18
?
喵喵時光機

TA貢獻(xiàn)1846條經(jīng)驗 獲得超7個贊

您非常接近,您需要保留對上次更新值的引用


class Node {

  constructor(data, next) {

    this.data = data;

    this.next = next;

  }

}


class LinkedList {

  constructor() {

    this.head = null;

  }


  insertFirst(data) {

    this.head = new Node(data, this.head);

  }


  size() {

    let counter = 0,

      node = this.head;


    while (node) {

      counter++;

      node = node.next;

    }


    return counter;

  }


  toArray() {

    let node = this.head;

    const result = [];


    while (node) {

      result.push(node.data);

      node = node.next;

    }


    return result;

  }



  removeEven() {

    let current = this.head;

    let final;


    while (current.next) {

      if (this.isEven(current.data)) {

        current = current.next;

      } else {

        if (!final) {

          final = current

          this.head = final

        } else {

          final.next = current

          final = current

        }

        current = current.next

      }

    }

    if (this.isEven(current.data)) {

      final.next = null

    }

  }


  isEven(num) {

    return num % 2 === 0 ? true : false;

  }

}


const q = new LinkedList();

q.insertFirst(16)

q.insertFirst(3)

q.insertFirst(4)

q.insertFirst(7)

q.insertFirst(5)

q.insertFirst(2)

q.insertFirst(1)

q.insertFirst(15)

q.insertFirst(18)

q.removeEven();


console.log(q.toArray());



查看完整回答
反對 回復(fù) 2021-11-18
?
慕仙森

TA貢獻(xiàn)1827條經(jīng)驗 獲得超8個贊

class Node {

  constructor(data, next) {

    this.data = data;

    this.next = next;

  }

}


class LinkedList {

  constructor() {

    this.head = null;

  }


  removeNodesWithNumberType(type = "isEven") {

    if (!this.head) return;

    let previousNode = this.head;

    let traversingNode = this.head;

    while (traversingNode) {

      if (this[type](traversingNode.data)) {

        this.removeNode(previousNode, traversingNode);

      } else {

        previousNode = traversingNode;

      }

      traversingNode = traversingNode.next;

    }

  }


  removeNode(previousNode, node) {

    if (this.isFirstNode(node)) this.head = node.next;

    previousNode.next = node.next;

  }


  isFirstNode(node) {

    return this.head === node;

  }


  isEven(num) {

    return num % 2 === 0;

  }


  isOdd(num) {

    return !this.isEven(num);

  }


  insertFirst(data) {

    this.head = new Node(data, this.head);

  }


  size() {

    let counter = 0,

      node = this.head;


    while (node) {

      counter++;

      node = node.next;

    }


    return counter;

  }


  toArray() {

    let node = this.head;

    const result = [];


    while (node) {

      result.push(node.data);

      node = node.next;

    }


    return result;

  }


  clear() {

    this.head = null;

  }

}


const q = new LinkedList();


// Test Case:1  Empty List

removeAndFormateOutput("isEven");


// Test Case:2  Single Node

q.insertFirst(16);

removeAndFormateOutput("isEven");


q.insertFirst(13);

removeAndFormateOutput("isOdd");


// Test Case:3 Two Consecutive Even

q.insertFirst(16);

q.insertFirst(18);

removeAndFormateOutput("isEven");


// Test Case:3 Two Consecutive odd

q.insertFirst(11);

q.insertFirst(13);

removeAndFormateOutput("isOdd");


// Test Case:4 Random List

q.insertFirst(3);

q.insertFirst(4);

q.insertFirst(7);

q.insertFirst(5);

q.insertFirst(2);

q.insertFirst(1);

q.insertFirst(15);

q.insertFirst(18);

q.insertFirst(20);

removeAndFormateOutput("isEven");


function removeAndFormateOutput(type) {

  console.log(`Remove if ${type} \n Before : ${q.toArray()}`);

  q.removeNodesWithNumberType(type);

  console.log(` After : ${q.toArray()}\n`);

  q.clear();

}


查看完整回答
反對 回復(fù) 2021-11-18
  • 3 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號