每一个学习计算机的人应该都懂的数据结构的重要性,前端人也不能忽视它的重要性。
以下是我在学习的过程中边学边敲的过程。
一 List
列表是最基本的数据结构,后面的栈、队列都是基于此的。个人感觉和数组有些相似。
function List(){
this.listSize = 0;
this.pos = 0;
this.dataStore = [];
this.clear = clear;
this.find = find;
this.toString = toString;
this.insert = insert;
this.append = append;
this.remove = remove;
this.front = front;
this.end = end;
this.prev = prev;
this.next = next;
this.length = length;
this.currPos = currPos;
this.moveTo = moveTo;
this.getElement = getElement;
this.contains = contains;
}
/*在列表的末尾添加新元素*/
function append(element){
this.dataStore[this.listSize++] = element;
}
/*在列表中查找一个元素*/
function find(element){
for(var i=0;i<this.dataStore.length;i++){
if(this.dataStore[i] == element){
return i;
}
}
return -1;
}
/*在列表的删除元素*/
function remove(element){
var foundAt = this.find(element);
if(foundAt > -1){
this.dataStore.splice(foundAt,1);
--this.listSize;
return true;
}
return false;
}
/*返回列表中元素的个数*/
function length(){
return this.listSize;
}
/*显示列表中的元素*/
function toString(){
return this.dataStore;
}
/*向列表中插入一个元素*/
function insert(element,after){
var inserPos = this.find(after);
if(inserPos > -1){
this.dataStore.splice(inserPos+1,0,element);
++this.listSize;
return true;
}
return false;
}
/*清空列表中的所有元素*/
function clear(){
delete this.dataStore;
this.dataStore = [];
this.listSize = this.pos = 0; //新的空列表
}
/*判断给定值是否在列表中*/
function contains(element){
for(var i = 0;i<this.dataStore.length;i++){
if(this.dataStore[i] == element){
return true;
}
}
return false;
}
/*列表的第一个位置*/
function front(){
this.pos = 0;
}
/*列表的最后一个位置*/
function end(){
this.pos = this.listSize - 1;
}
/*当前位置的前一个*/
function prev(){
if(this.pos > 0 ){
--this.pos;
}
}
/*当前位置的下一个*/
function next(){
if(this.pos < this.listSize-1){
++this.pos;
}
}
/*返回当前位置*/
function currPos(){
return this.pos;
}
/*移动到指定位置*/
function moveTo(position){
this.pos = position;
}
/*获取当前位置节点的值*/
function getElement(){
return this.dataStore[this.pos];
}
二 Stack
栈是基于列表的。它的特点是后进先出。一张经典的教科书图来解释。
function stack(){
this.dataStore = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek;
this.length = length;
}
/*增加一个元素*/
/*根据栈的特点,增加就是压进一个元素,就是在栈顶push一个*/
function push(element){
this.dataStore[this.top++] = element;
}
/*减少一个元素*/
/*根据栈的特点,减少就是蹦出一个元素,就是在栈顶pop一个*/
function pop(element){
return this.dataStore[--this.top];
}
/*只返回栈顶元素,不删除元素*/
function peek(){
return this.dataStore[this.top-1]
}
/*返回栈的长度*/
/*根据栈的特点,返回栈顶的长度*/
function length(){
return this.top;
}
/*清空栈*/
/*让栈顶的位置为0*/
function clear(){
this.top = 0;
}
/*不同数制之间的相互转换*/
/*不断的除以想转换的数制,余数进栈,除数继续除,直至除数为0*/
function mulBase(num,base){
var s = new stack();
while(num > 0){
s.push(num%base);
num = Math.floor(num /= base);
}
var converd = "";
while(s.length()>0){
converd += s.pop();
}
return converd;
}
/*判断是否为回文字符串*/
/*将字符串的字母一个一个压进栈,在蹦出,如果两次一样,则为回文字符串*/
function isPalindrome(word){
var s = new stack();
for(var i=0;i<word.length;++i){
s.push(word[i]);
}
var rword="";
while(s.length()>0){
rword += s.pop();
}
if(word == rword){
return true;
}else{
return false;
}
}