栈
var Stack = function(){
var items = [] //私有属性
this.push = function(element){
items.push(element)
}
this.pop = function(){
return items.pop();
}
this.peek = function(){//栈顶元素
return items[items.length-1];
}
this.isEmpty = function(){//判断为空
return items.length == 0
}
this.clean = function(){//清除栈
items=[];
}
this.size = function(){//返回长度
return items.length
}
}
var zhan = new Stack;
队列
var Queue = function(){//队列
var item = [];
//入队列
this.enqueue = function(element){
item.push(element);
}
//出队列
this.dequeue = function(){
return item.shift();
}
//查看队列头部
this.front = function(){
return item[0];
}
//检查队列是否为空
this.isEmpty = function(){
return item.length === 0;
}
//队列大小
this.Size = function(){
return item.length;
}
}
集合
var Set = function(){
var item = {};
var length = 0;
this.has=function(value){
if(item.hasOwnproperty(value)){
return true;
}else{
return false;
}
}
this.add = function(value){
if(this.has(value)){
return false
}else{
item[value]=value;
return value;
length++;
}
}
this.remove = function(value){
if(this.has(value)){
delete item[value];
return true;
length--;
}else{
return false;
}
}
this.getItem= function(){
return item;
}
this.clear = function(){
item={};
}
this.getSize = function(){
return length;
}
}
var set = new Set;//实例化集合
链表
var LinkList = function (){
var head = null;//头部
var length =0;
var Node = function(element){
this.element=element;
this.next=null
}
this.append = function(element){
var node = new Node(element);
if(head==null){
head = node
}else{
var current = head;
while(current.next){
current = current.next
}
//while循环之后current就是链表的最后一项
current.next = node;
}
}
this.getHead = function(){
return head;//返回链表头
}
this.insert = function(element,position){//插入元素
if(position > -1 && position < length){
var node = new Node(element);
if(position == 0){
var current = head;
head = node;
head.next=current
}else{
var index = 0;
var current = head;
var previous = null;
while(index<position){
previous = current;
current = current.next;
index++
}
previous.next = node;
node.next = current;
}
}
}
this.removeAt = function(position){
if(position>-1&&position<length){
if(position==0){
var current=head;
head=current.next;
length--;
}else{
var current = this.head;
for(let i=0;i<position-1;i++){
current=current.next;
}
current=current.next;
length--;
return current;
}
} {
return null;
}
}
this.indexof=function(element){
var current = head;
var index = 0;
while(current.next){
if(current.element===element){
return index
}else{
current = current.next;
index++;
}
}
return null;
}
}
var linklist1 = new LinkList;//实例化
哈希表
哈希冲突
var HashCode = function(){
var items= [];
var loseHashCode = function(key){//散列函数
var hash = 0;
for(var i=0;i<key.length;i++){
hash+=key[i].charCodeAt();
}
return hash%37;
}
this.put = function(key,value){
var position =loseHashCode(key);
items[position]=value;
}
this.getItems =function(){
return items;
}
this.get = function(key){
return items[loseHashCode(key)];
}
this.remove = function(key){
items[loseHashCode(key)] = undefined;
}
}
树
var Tree = function(){
var Node =function(value){
this.value=value;
this.left=null;
this.right=null;
}
var root = null;
this.insertNode = function(node,newNode){
if(newnode.value >= node.value){
if(node.right==null){
node.right = newnode
}else{
insertNode(node.right,newnode);
}
}else if(newnode.value < node.value){
if(node.left==null){
node.left = newnode;
}else{
insertNode(node.left,newnode);
}
}
}
this.insert = function(value){
var newnode = new Node(value);
if(root==null){
root=newnode;
}else{
insertNode(root,newNode);
}
}
this.search = function(value){
if(root>value){
} else{
}
}
var traverNode = function(node,callback){
if(node==null) return
// callback(node.value);//前序遍历
traverNode(node.left,callback);
// callback(node.value); //中序遍历
traverNode(node.right,callback);
callback(node.value);//后序遍历
}
this.traver = function(callback){
traverNode(root,callback);
}
this.getRoot = function(){
return root;
}
this.getMax = function(){
if(root==null) return null
while(node.right && node){
node=node.right
}
return node;
}
this.getMin = function(){
if(root==null) return null;
while(node.left && node){
node=node.left
}
return node;
}
var findMinRight = function(node){
if(node==null) return null
getMin(node.right);
}
var removeNode = function(node,value){
if(root==null) return null;
if(value > node.value){
//向右查找
node.right= removeNode(node.right,value);
return node;
}else if(value < node.value){
//向左查找
node.left= removeNode(node.left,value);
return node;
}else{
//叶子节点
if(node.left == null && node.right == null){
return null;
}
//只有一个子节点
if(node.left == null && node.right){
return node.right;
}else if(node.right ==null &&node.left){
return node.left;
}
//有两个子节点
var tempnode = findMinRight(node);
node.value = tempnode.value;
node.right = removeNode(node.right,tempnode.value);
return node;
}
}
this.remove = function(key){
root = removeNode(root,key);
}
}
var tree = new Tree;//实例化树
图
var Queue = function(){//队列
var item = [];
//入队列
this.enqueue = function(element){
item.push(element);
}
//出队列
this.dequeue = function(){
return item.shift();
}
//查看队列头部
this.front = function(){
return item[0];
}
//检查队列是否为空
this.isEmpty = function(){
return item.length === 0;
}
//队列大小
this.Size = function(){
return item.length;
}
}
var Graph = function(){
var vertical = [];//顶点
var adjlist = {};//连接的边
this.addVercial = function(value){
vertical.push(value);
adjlist[value] = [];
}
this.addAdjlist = function(dot1,dot2){
adjlist[dot1].push(dot2);
adjlist[dot2].push(dot1);
}
this.print = function(){
var n ='\n';
for(var i=0;i<vertical.length;i++){
var dot = vertical[i];
n +=dot+'=>';
var bian = adjlist[dot];
for(var j=0;j<bian.length;j++){
s+=bian[j];
}
s+='\n';
}
console.log(s);
}
//white 未发现 grey 已发现 black 已探索
var initColor = function(){
var color = {};
for(var i=0;i<vertical.length;i++){
color[vertical[i]]='white'
}
return color;
}
this.gdyx = function(v,callback){
//初始化广度优先
var color = initColor();//初始
var queue = new Queue;
queue.enqueue(v);
var distance = {};
var pred = {};
for(let j=0;j<vertical.length;j++){
distance[vertical[j]]=0;
pred[vertical[j]]=null;//初始化
}
while(!queue.isEmpty()){
var now = queue.dequeue()
var bian = adjlist[now]
for(var i=0;i<bian.length;i++){
var next = bian[i];
if(color[next]==='white'){
//未发现的全部入列,并标识为 已发现
color[next]= 'grey';
//设置回溯点
pred[next] = now;
distance[next] = distance[now]+1;
queue.enqueue(next);
}
}
color[now]= 'black';
if(callback){
callback(now);
}
}
return{
pred : pred,
distance : distance
}
}
var sdyxTu = function(u,color,callback){
color[u] = 'grey';
var n = adjlist[u];
for(var i=0;i<n.length;i++){
var w = n[i];
if(color[w]== 'white'){
sdyxTu(w,color,callback);
}
}
color[u] = 'black';
if(callback){
callback(u);
}
}
this.sdyx = function(v,callback){//深度优先遍历
var color = initColor();//初始
sdyxTu(v,color,callback);
}
}