javascript MAX-VAL-IN列表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript MAX-VAL-IN列表相关的知识,希望对你有一定的参考价值。

// Find a maximum value in a linked list

let node = function(data) {
  this.next = null;
  this.data = data;
}

let list = function() {
  this.head = null;
}


list.prototype.add = function(data) {
  if (!this.head) {
    this.head = new node(data);
    return this.head;
  } else {
    let curr = this.head;
    while(curr.next) {
      curr = curr.next;
    }
    curr.next = new node(data);
    return curr.next;
  }
}

list.prototype.print = function(name) {
  console.log('printing ' + (name ? name : '...'));
  let curr = this.head;
  while(curr) {
    console.log(curr.data);
    curr = curr.next;
  }
}

list.prototype.getMax = function() {
  let curr = this.head;
  if (!curr) return null;
  let max = curr.data;
  while(curr.next) {
    if (curr.data > max) max = curr.data;
    curr = curr.next;
  }
  return max;
}


let l = new list();
l.add(9);
l.add(1);
l.add(10);
l.add(1);
// l.print();

console.log('Max: ', l.getMax());

以上是关于javascript MAX-VAL-IN列表的主要内容,如果未能解决你的问题,请参考以下文章

Javascript实现列表结构

Javascript:推送整个列表?

使用JavaScript浅谈列表

javascript JavaScript中的链接列表

javascript 按列表#js过滤列表

javascript 按列表#js过滤列表