http://www.cnblogs.com/skywang12345/p/3603935.html

c/c++/java什么的太麻烦。我用JavaScript实现了一部分。

1. suanfa.js   2.排序.html 3.bfs-dfs.html

1. suanfa.js

/// 数据结构
/// 算法//单链表 节点对象
var Node = function(newData){this.next = null;this.data = newData;
}
function linkList(){this.head = null;this.size =  0;
}
linkList.prototype.Add = function(newData){this.size += 1;var newNode = new Node(newData);var tempNode = null;if(this.head == null){this.head = newNode;}else{tempNode = this.head;while(tempNode.next != null){tempNode = tempNode.next;}tempNode.next = newNode;}
}
linkList.prototype.AddAt = function(pos, newData){var tempNode = this.head;var t = new Node(newData);var p = null;if(pos > this.size || pos < 0){return null;}this.size += 1;if(pos == 0){t.next = this.head;this.head = t;return null;}for(var i=pos-1;i > 0;i--){tempNode = tempNode.next;}if(tempNode.next != null){t.next = tempNode.next;}tempNode.next = t;
}
linkList.prototype.GetData = function(pos){var tempNode = this.head;if(pos >= this.size || pos < 0){return null;}else{for(i=pos;i > 0;i--){if(tempNode.next != null){tempNode = tempNode.next;}else{return null;}}return tempNode.data;}
}
linkList.prototype.RemoveAt = function(pos){var tempNode = this.head;if(pos >= this.size || pos < 0){return null;}this.size -= 1;if( pos == 0 ){this.head = this.head.next;return this.head;}for(var i = pos; i > 0;i--){tempNode = tempNode.next;}if(tempNode.next != null){tempNode.next = tempNode.next.next;}else{tempNode.next = null;}return tempNode.next;
}
linkList.prototype.Print = function(){var tempNode = this.head;while(tempNode != null){console.log(tempNode.data);tempNode = tempNode.next;}console.log("end!");
}
linkList.prototype.Reverse = function(){var first = null, second =null, third =null;if(this.head == null || this.head.next == null){return null;}first = this.head;second = first.next;while(null != second){third = second.next;second.next = first;first = second;second = third;}this.head.next = null;this.head = first;
}
linkList.prototype.Destroy = function(){var tempNode = this.head;var t = null;if(tempNode == null){return null;}while(tempNode.next !=null){this.size--;t = tempNode.next;tempNode = tempNode.next;t = null;}this.head = null;this.size--;
}//深度优先搜索 广度优先搜索
//栈类
function Stack(){this.st = [];
}
Stack.prototype.push = function(i){this.st.push(i);
}
Stack.prototype.pop = function(){return this.st.pop();
}
Stack.prototype.peek = function(){return this.st[this.st.length-1];
}
Stack.prototype.isEmpty = function(){return this.st.length==0;
}
//队列类 先进先出 FIFO
function Queue(){this.queueArr = [];
}
Queue.prototype.insert = function(i){this.queueArr.push(i);
}
Queue.prototype.remove = function(){return this.queueArr.shift();
}
Queue.prototype.isEmpty = function(){return this.queueArr.length==0;
}
//顶点类
function TopPoint(o){this.label = o;this.isVisited = false;
}
//图类
function Graph(){this.MAX_TopPoint = 20;this.TopPoint = [];//TopPoint[]this.adjMat = [[]];this.nTopPoint = 0;this.Stack = new Stack();this.Queue = new Queue();
}
//增加一个顶点
Graph.prototype.addTopPoint = function(o){this.TopPoint.push(new TopPoint(o));this.nTopPoint++;this.adjMat.push([]);for(var i=0;i < this.adjMat.length;i++){this.adjMat[i].push([]);}
}
//增加一条边
Graph.prototype.addEdge = function(start,end){this.adjMat[start][end]=1;this.adjMat[end][start]=1;
}
Graph.prototype.print = function(i){console.log(this.TopPoint[i]);
}
Graph.prototype.getAdjUnVisitedPoint = function (i){for(var j=0;j < this.nTopPoint;j++){if(this.adjMat[i][j]==1 && this.TopPoint[j].isVisited==false){return j;}}return -1;
}
//深度优先搜索
Graph.prototype.dfs = function (){var v = null;this.TopPoint[0].isVisited = true;this.print(0);this.Stack.push(0);while(!this.Stack.isEmpty()){v = this.getAdjUnVisitedPoint(this.Stack.peek());if(v==-1){this.Stack.pop();}else{this.TopPoint[v].isVisited = true;this.print(v);this.Stack.push(v);}}for(var i=0;i < this.nTopPoint;i++){this.TopPoint[i].isVisited = false;}
}
//广度优先搜索
Graph.prototype.bfs = function (){var v1 = null, v2 =null;this.TopPoint[0].isVisited = true;this.print(0);this.Queue.insert(0);while(!this.Queue.isEmpty()){v1 = this.Queue.remove();while((v2=this.getAdjUnVisitedPoint(v1))!=-1){this.TopPoint[v2].isVisited = true;this.print(v2);this.Queue.insert(v2);}}for(var j=0;j < this.nTopPoint;j++){this.TopPoint[j].isVisited = false;}
}//冒泡排序
Array.prototype.BubbleSort = function(){var n = this.length, temp = null,  i=0, j=0;for(i=0;i < n;i++){for(j=i;j < n;j++){if(this[i] > this[j]){temp = this[i];this[i]=this[j];this[j]=temp;}}}
}
//快速排序
Array.prototype.QuickSort = function(left,right){var key = this[left],low = left,high= right;if(left < right){while(low < high){while(low < high && this[high] > key){high--;}this[low] = this[high];while(low < high && this[low] < key){low++;}this[high] = this[low];}this[low] = key;this.QuickSort(left,low-1);this.QuickSort(low+1,right);}
}
//简单选择排序
Array.prototype.SelectSort = function(){var n = this.length, i=0, j=0, temp=0, minPos = 0;for(;i < n-1;++i){minPos = i;//查找最小值for(j=i+1;j < n; j++){if(this[j] < this[minPos]) minPos = j;}if(minPos!=i){temp = this[i];this[i]=this[minPos];this[minPos]=temp;}}
}
//直接插入排序
Array.prototype.InsertSort = function(){var n = this.length, temp = 0, j = 0, i=0;for(i=1;i < n;i++){temp = this[i];for(j=i-1; j >=0 && temp < this[j];j--){this[j+1] = this[j];}this[j+1]=temp;}
}
//希尔排序
Array.prototype.ShellSort = function(){var n = this.length, i, j, temp, d;d = n/2;//分成n/2组 1 2 3 4 5 6  n=3while(d >= 1){//对每组进行直接插入排序for(i=d;i < n;i++){temp = this[i];for(j=i-d;j >= 0 && temp < this[j];j-=d){this[j+d]=this[j];}this[j+d]=temp;}d= Math.floor(d/2);}
}
Array.prototype.print = function(){for(var i=0;i < this.length;i++){console.log(this[i]);}
}
//堆排序
Array.prototype.HeapAdjust = function(root, n){var child = 2*root + 1, rightChild = 0, temp = 0;//左孩子if(child <= n - 1){//有左孩子rightChild = child + 1;if(rightChild <= n -1){//有右孩子if(this[child] < this[rightChild]) child = rightChild;}if(this[root] < this[child]){temp = this[child];this[child] = this[root];this[root] = temp;this.HeapAdjust(child, n);}}
}
Array.prototype.HeapSort = function(){var n = this.length, i = 0, temp = 0;for(i = Math.floor(n/2) - 1;i >= 0;i--){this.HeapAdjust(i, n);}for(i = n -1; i > 0; i--){temp = this[0];this[0] = this[i];this[i] = temp;this.HeapAdjust(0, i);}
}

排序.html

<!DOCTYPE html>
<html>
<head><title>test</title><meta charset="UTF-8" /><meta name="Author" content="阮家友"><meta name="Keywords" content="HTML,model,test"><meta name="Description" content="special effect"><meta name="time" content="2015-9-27 10:41:48"><link rel="stylesheet" href="css/reset.css" type="text/css"/><style type="text/css">#center {width:800px;margin:0 auto;padding:50px;}</style>
</head>
<body><div id="center"><p>模板</p><p>用于测试</p></div>
<script type="text/javascript" src="suanfa.js"></script>
<script type="text/javascript">
var arr1 = [6,7,2,3,8];//arr1.BubbleSort();//ok
//arr1.print();//arr1.SelectSort();//ok
//arr1.print();//arr1.InsertSort();//ok
//arr1.print();//arr1.ShellSort(); // failed d/=2 d是增量要取整
//arr1.print();//arr1.QuickSort(0,arr1.length-1);//ok
//arr1.print();arr1.HeapSort();
arr1.print();
</script>
</body>
</html>

3. bfs-dfs.html

<!DOCTYPE html>
<html>
<head><title>test</title><meta charset="UTF-8" /><meta name="Author" content="阮家友"><meta name="Keywords" content="HTML,model,test"><meta name="Description" content="special effect"><meta name="time" content="2016-4-12 23:17:22"><style type="text/css">#center {width:800px;margin:0 auto;padding:50px;}</style>
</head>
<body><div id="center"></div>
<script type="text/javascript" src="suanfa.js"></script>
<script type="text/javascript">
var g1 = new Graph();
/*-->b-->f-->h
a-->c   -->d-->g-->i-->e
*/
g1.addTopPoint("a");
g1.addTopPoint("b");
g1.addTopPoint("c");
g1.addTopPoint("d");
g1.addTopPoint("e");
g1.addTopPoint("f");
g1.addTopPoint("g");
g1.addTopPoint("h");
g1.addTopPoint("i");
g1.addEdge(0,1);g1.addEdge(1,5);g1.addEdge(5,7);
g1.addEdge(0,2);
g1.addEdge(0,3);g1.addEdge(3,6);g1.addEdge(6,8);
g1.addEdge(0,4);
//深度优先搜索
//g1.dfs();
//广度优先搜索
g1.bfs();
</script>
</body>
</html>

数据结构和算法目录表相关推荐

  1. 数据结构与算法-2-链表的基本操作-查找

    数据结构与算法-2-链表的基本操作-查找(c语言) 本文是单链表的C语言实现方法,包括单链表的创建.插入.删除.修改.查找等基本操作. 链表结点的类型定义 /*链式存储结构的头结点*/ typedef ...

  2. 数据结构与算法-4-链表的基本操作-增

    数据结构与算法-4-链表的基本操作-增 注意:以下为顺序存储结构实现 相关的头文件 /*以下为头文件SqList.h是用于定义相关函数的头文件*/ #pragma once #define LIST_ ...

  3. 数据结构于算法—线性表

    目录 前言 线性表基本架构 顺序表 插入 删除 其他操作 链表 基本结构 插入 带头节点与不带头节点 插入尾 删除 其他 代码实现 顺序表 链表 测试与结果 总结 原创公众号:bigsai 文章收藏在 ...

  4. 数据结构与算法 | 线性表 —— 链表

    原文链接:wangwei.one/posts/java-- 链表 定义 逻辑结构上一个挨一个的数据,在实际存储时,并没有像顺序表那样也相互紧挨着.恰恰相反,数据随机分布在内存中的各个位置,这种存储结构 ...

  5. 数据结构与算法 / 跳表

    一.诞生原因 解决链表查询时耗时过长的问题. 二.基本信息 英文全称:Skip List . 链表 + 多级索引(链表) = 跳表 三.原理说明 顾名思义,跳表的查询是在多个链表之间跳跃查询的,其路线 ...

  6. 数据结构及算法 -- 目录

    排序算法 -- 目录 啦啦啦 转载于:https://www.cnblogs.com/ClassNotFoundException/p/7122848.html

  7. Java版数据结构与算法——线性表

    *************************************优雅的分割线 ********************************** 分享一波:程序员赚外快-必看的巅峰干货 如 ...

  8. 高级数据结构与算法 | 跳跃表(Skip List)

    文章目录 区间查询时链表与顺序表的局限 跳表=链表+索引 跳表的原理 晋升 插入 删除 跳表的实现 跳表VS红黑树 区间查询时链表与顺序表的局限 假设有这样一个情景, 此时需要设计一个拍卖系统,对于商 ...

  9. 数据结构与算法顺序表数组版

    博主还在学校,写网络编程特别是后面的线程和多路I/O实在是太费精力,所以博主先把数据结构多跟新一点,也正好把学校的C语言数据结构的作业做了,正好一举两得 这个内容比较简单,就不再细说. #includ ...

最新文章

  1. json-lib 常用功能
  2. 确定浏览器是否支持某些DOM模块
  3. 数据结构与算法 | 栈
  4. 16.04编译android 7.0,ubuntu16.04 编译Android5.1报错
  5. 关于ISA WPAD的深入探讨
  6. Python自动化之Django中间件
  7. 以太坊 solidity 教程
  8. C# Excel 删除指定的工作表
  9. matlab最小二乘法拟合直线
  10. 大数据平台需求调研大纲模板
  11. [渝粤教育] 南开大学 面向对象程序设计 参考 资料
  12. html5 window.game,releasing html5 games for windows 8
  13. linux 命令是什么的缩写,Linux一部分命令解释(命令缩写代表什么意思)
  14. Mac OS X 内核Rootkit开发指南
  15. lt18i android 2.3.4典藏版,索尼LT18i一键ROOT教程工具 2.3.4已亲测成功
  16. python自动办公 pdf_别再问如何用 Python 提取 PDF 内容了!
  17. 前端漂亮的字体 font-family
  18. 腾讯云域名证书下载_备案域名证书获取
  19. spring cloud NetFlix 学习笔记
  20. 红旗Linux的特点和应用范围,以红旗Linux和Ubuntu为例评点Linux发行版的优点

热门文章

  1. Python实现视频运动目标检测——帧差法
  2. 微信小程序通过数据绑定获取用户名和头像
  3. iWatch:可叫板Google Glass的智能手表
  4. Xorm 使用手册,面向工作学习
  5. 如何在Intellij IDEA中管理svn并进行分支的合并
  6. java 记事本 新建_java记事本开发
  7. C#中如何获取上个月第一天和最后一天
  8. 一个你所不知道的暗黑游戏圈
  9. Day 30:HTML和CSS在Java项目中常用语法
  10. [附源码]Python计算机毕业设计SSM化妆品网上商城数据分析系统(程序+LW)