js 数据结构(三):链表

链表

链表存储有序的元素集合, 但不同于数组的是链表中的元素在内存中并不是连续放置的。每个元素由一个存储元素本身的节点和一个指向下一个元素的引用指针或(链接)组成。下图展示了一个链表的结构:

我们来实现链表, 下面是LinkedList类的骨架:

基本框架

function LinkedList() {
  var Node = function (element) {
    this.element = element;
    // 指向列表中下一个节点项的指针
    this.next = null;
  };
  var length = 0;
  var head = null;
  // 向列表尾部添加一个新的项
  this.append = function (element) {};
  // 向列表的特定位置插入一个新的项
  this.insert = function (position, element) {};
  // 从列表的特定位置移除一项
  this.removeAt = function (position) {};
  // 从列表中移除一项
  this.remove = function (element) {};
  // 返回元素在列表中的索引。如果列表中没有该元素则返回-1
  this.indexOf = function (element) {};
  // 链表是否为空
  this.isEmpty = function () {};
  // 返回链表包含的元素个数
  this.size = function () {};
  // 输出元素的值
  this.toString = function () {};
  this.print = function () {};
}

下面咱们具体来实现每个方法:

向链表尾部追加元素---append

this.append = function (element) {
  var node = new Node(element),
    current;
  if (head === null) {
    // 列表中第一个节点
    head = node;
  } else {
    current = head;
    // 循环列表,直到找到最后一项
    while (current.next) {
      current = current.next;
    }
    // 找到最后一项,将其next赋为node,建立链接
    current.next = node;
  }
  // 更新列表的长度
  length++;
};

从链表中移除元素---removeAt 

this.removeAt = function (position) {
  //检查是否越界
  if (position > -1 && position < length) {
    var current = head,
      previous,
      index = 0;
    // 移除第一项
    if (position === 0) {
      head = current.next;
    } else {
      while (index++ < position) {
        previous = current;
        current = current.next;
      }
      //将previous与current下一项链接起来,跳过current,从而移除它
      previous.next = current.next;
    }
    // 更新列表的长度
    length--;
    return current.element;
  } else {
    return null;
  }
};
this.insert = function(position, element){ 
 //检查越界
 if (position >= 0 && position <= length){ 
 var node = new Node(element), 
 current = head, 
 previous, 
 index = 0; 
 if (position === 0){ //在第一个位置添加
 node.next = current;  
 head = node; 
 } else { 
 while (index++ < position){ 
 previous = current; 
 current = current.next; 
 } 
 node.next = current; 
 previous.next = node; 
 } 
 length++;
 return true; 
 } else { 
 return false; //{6} 
 } 
};

在任意位置插入一个元素---insert

this.insert = function (position, element) {
  // 检查越界值
  if (position >= 0 && position <= length) {
    var node = new Node(element),
      current = head,
      previous,
      index = 0;
    if (position === 0) {
      // 在第一个位置添加
      node.next = current; 
      head = node;
    } else {
      // 找到目标位置
      while (index++ < position) {
        previous = current;
        current = current.next;
      }
      node.next = current; 
      previous.next = node; 
    }
    // 更新列表的长度
    length++; 
    return true;
  } else {
    return false; 
  }
};

查找元素的位置---indexOf

this.indexOf = function (element) {
  var current = head, 
    index = -1;
  while (current) {
    if (element === current.element) {
      return index; 
    }
    index++; 
    current = current.next; 
  }
  return -1;
};

移除元素---remove

this.remove = function (element) {
  var index = this.indexOf(element);
  return this.removeAt(index);
};

链表是否为空---isEmpty

this.isEmpty = function () {
  return length === 0;
};

链表长度---size

this.size = function () {
  return length;
};

转换成字符串---toString

this.toString = function () {
  var current = head, 
    arr = []; 
  while (current != null) {
    arr.push(current.element); 
    current = current.next; 
  }
  return arr.join(' '); 
};

测试

var list = new LinkedList();
list.append(100);
list.append(2);
list.insert(1, 1);
list.toString();  // 100 1 2
list.indexOf(2);  // 2
list.removeAt(0); // 100
list.toString();  // 1 2

参考链接: 学习JavaScript数据结构与算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值