1. 程式人生 > >資料結構和演算法之-列表

資料結構和演算法之-列表

列表的實現


(function(window) {
var win = window,
_restore = {

listSize: function() {

return this.restore.length;

},

length: function() {

console.log(this.restore);
return this.restore.length;

},

clear: function() {

delete this.restore;
this.restore = [];
this.listIndex = 0;
return this;

},

toSting: function() {

return this.restore;

},

getElement: function(ele) {

var index = this.find(ele);
if (index > -1) {
return this.restore[index];
}
return null;

},

insert: function(ele, pos) {

this.listIndex++;
this.restore.splice(pos, 0, ele);
return this;

},

append: function(ele) {

this.listIndex++;
this.restore.push(ele);
return this;

},

remove: function(ele) {

this.listIndex--;
var index = this.find(ele);
this.restore.splice(index, 1);
return this;

},

front: function(ele) {

var index = this.find(ele);
this.restore.splice(index, 1);
this.restore.splice(0, 0, ele);
return this;

},

end: function(ele) {

var index = this.find(ele);
this.restore.splice(index, 1);
this.restore.splice(this.restore.length, 0, ele);
return this;

},

prev: function(ele) {

var index = this.find(ele);
this.restore.splice(index, 1);
this.restore.splice(index-1, 0, ele);
return this;

},

next: function(ele) {

var index = this.find(ele);
console.log(index);
this.restore.splice(index,1);
this.restore.splice(index + 1, 0, ele);
return this;

},

currPos: function(ele) {

return this.find(ele);

},

moveTo: function(ele, pos) {

var index = this.find(ele);
this.restore.splice(index,1);
this.restore.splice(pos, 0, ele)
return this;

},

find: function(ele) {

for (var i = 0, len = this.restore.length; i < len; ++i) {
  if (ele === this.restore[i]) {
    return i;
  }
}
  return -1;
}

}

function list() {

return new list.prototype.init();

}

list.fn = list.prototype ={

init: function() {
this.pos = 0;
this.listIndex = 0;
this.restore = [];
this.listSize = _restore.listSize;
this.length = _restore.length;
this.clear = _restore.clear;
this.toString = _restore.toSting;
this.getElement = _restore.getElement;
this.insert = _restore.insert;
this.append = _restore.append;
this.remove = _restore.remove;
this.front = _restore.front;
this.end = _restore.end;
this.prev = _restore.prev;
this.next = _restore.next;
this.currPos = _restore.currPos;
this.moveTo = _restore.moveTo;
this.find = _restore.find;
this._sort = _restore.sort;
}

};

list.fn.init.prototype = list.fn;
win.$ = win.list = list;
win.$$ = list();

})(window)

list.fn和list.fn.init.prototype = list.fn,初始化時候直接呼叫物件方法。就像function Foo(){}然後你要使用他作為建構函式,製造一個物件var foo = new Foo;再然後呼叫初始化方法foo.init();這樣就顯得比較麻煩,jquery原始碼,就是採用這種方式實現的。


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./列表的實現.js" charset="utf-8"></script>
</head>
<body>
<script type="text/javascript">

  var mylist = new list();
  mylist.append(1);
  mylist.append(2);
  console.log(mylist.length());

  var mylist1 = new list();
  mylist1.append(1);
  mylist1.append(2);
  mylist1.append(3);
  console.log(mylist1.length());

  $$.append("a");
    console.log($$.length());

</script>
</body>
</html>