js实现单向链表

  1. add() 添加元素
  2. clear() 清空链表
  3. contains(data) 是否包含元素
  4. display() 显示链表
  5. get(position) 得到索引位置的元素
  6. isEmpty() 链表是否为空
  7. remove(position) 移除索引位置的元素
  8. reverse() 倒置链表
  9. set(position, data) 向链表指定位置设置修改元素
  10. size() 返回链表的长度
var Node = function (data) {
  this.data = data;
  this.next = null;
};

var LList = function () {
  this.firstNode = null;
  this.lastNode = null;
  this.length = 0;
};

LList.prototype = {
  clear: function () {
    this.firstNode = null;
    this.lastNode = null;
    this.length = 0;
  },

  get: function (position) {
    var currentNode = this.firstNode;
    for (var i = 1; i < position + 1; i++) {
      currentNode = currentNode.next;
    }
    return currentNode;
  },
  isEmpty: function () {
    var result = false;
    if (this.length == 0 && this.firstNode == null) {
      result = true;
    }
    return result;
  },
  add: function () {
    if (arguments.length == 1) {
      data = arguments[0];
      var newNode = new Node(data);
      if (this.isEmpty()) {
        this.firstNode = newNode;
      } else {
        this.lastNode.next = newNode;
      }
      this.lastNode = newNode;
      this.length++;
      return true;
    } else if (arguments.length == 2) {
      var isSuccessful = true;
      var position = arguments[0];
      var data = arguments[1];
      if (position < this.length + 1) {
        var newNode = new Node(data);
        if (this.isEmpty()) {
          this.firstNode = newNode;
          this.lastNode = newNode;
        } else if (position == this.length) {
          this.lastNode.next = newNode;
          this.lastNode = newNode;
        } else if (position == 0) {
          newNode.next = this.firstNode;
          this.firstNode = newNode;
        } else {
          var nodeBefore = this.get(position - 1);
          var nodeAfter = nodeBefore.next;
          newNode.next = nodeAfter;
          nodeBefore.next = newNode;
        }
        this.length++;
      } else {
        isSuccessful = false;
      }
      return isSuccessful;
    }
  },
  display: function () {
    var str = '';
    var currentNode = this.firstNode;
    while (currentNode != null) {
      str += currentNode.data + ', '
      currentNode = currentNode.next;
    }
    str = str.substr(0, str.length - 2);
    console.log(str);
  },
  remove: function (position) {
    var result = null;
    if (!this.isEmpty() && (position < this.length)) {
      if (position == 0) {
        result = this.firstNode.data;
        this.firstNode = this.firstNode.next;
        this.lastNode = null;
      } else {
        var nodeBefore = this.get(position - 1);
        var nodeToRemove = nodeBefore.next;
        var nodeAfter = nodeToRemove.next;
        nodeBefore.next = nodeAfter;
        result = nodeToRemove.data;
        if (position == this.length - 1) {
          this.lastNode = nodeBefore;
        }
      }
      this.length--;
    }
    return result;
  },
  set: function (position, data) {
    var isSuccessfule = true;
    if (!this.isEmpty() && (position < this.length)) {
      var desireNode = this.get(position);
      desireNode.data = data;
    } else {
      isSuccessfule = false;
    }
    return isSuccessfule;
  },
  contains: function (data) {
    var found = false;
    var currentNode = this.firstNode;

    while (!found && (currentNode != null)) {
      if (data === currentNode.data) {
        found = true;
      } else {
        currentNode = currentNode.next;
      }
    }
    return found;
  },
  size: function() {
    return this.length;
  },
  reverse: function() {
    var newList = new LList();
    for(var i=this.length-1; i>=0; i--) {
      newList.add(this.get(i).data);
    }
    return newList;
  }
};
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值