前端学习总结(一)——常见数据结构的javascript实现
1.列表类
// 列表类
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.length = length; // 返回列表中元素的个数
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 insertPos = this.find(after);
if (insertPos > -1) {
this.dataStore.splice(insertPos+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];
}
2.栈
// 构造函数
function Stack() {
this.dataStore = []; //数据结构为数组
this.top = 0; // 指向栈顶元素
this.push = push; // 向栈顶添加一个元素
this.pop = pop; // 删除栈顶元素
this.peek = peek; // 返回栈顶元素
this.length = length; // 返回栈的长度
this.clear = clear; // 清空栈
}
// 向栈顶添加元素
function push(element) {
this.dataStore[this.top++] = element;
}
// 删除栈顶元素
function pop() {
return this.dataStore[--this.top];
}
// 返回栈顶元素
function peek() {
return this.dataStore[this.top-1];
}
// 返回栈的长度
function length(){
return this.top;
}
// 清空栈
function clear() {
this.top = 0;
}
// 测试代码:
var s = new Stack();
s.push("David");
s.push("Raymond");
s.push("Bryan");
print("length: " + s.length());
print(s.peek());
var popped = s.pop();
print("The popped element is: " + popped);
print(s.peek());
s.push("Cynthia");
print(s.peek());
s.clear();
print("length: " + s.length());
print(s.peek());
s.push("Clayton");
print(s.peek());
// 输出为:
length: 3
Bryan
The popped element is: Bryan
Raymond
Cynthia
length: 0
undefined
Clayton
3.队列
function Queue() {
this.dataStore = []; // 利用数组实现队列
this.enqueue = enqueue; // 入队
this.dequeue = dequeue; // 出队
this.front = front; // 取队首元素
this.back = back; // 取队尾元素
this.toString = toString; // 显示队列内的所有元素
this.empty = empty; // 判断队空
}
// 入队
function enqueue(element) {
this.dataStore.push(element);
}
// 出队
function dequeue() {
return this.dataStore.shift();
}
// 取队首元素
function front() {
return this.dataStore[0];
}
// 取队尾元素
function back() {
return this.dataStore[this.dataStore.length-1];
}
// 显示队列内的所有元素
function queueToString() {
var retStr = "";
for (var i = 0; i < this.dataStore.length; ++i) {
retStr += this.dataStore[i] + "\n";
}
return retStr;
}
// 判断队空
function empty() {
if (this.dataStore.length == 0) {
return true;
}
else {
return false;
}
}
// 测试代码
var q = new Queue();
q.enqueue("Meredith");
q.enqueue("Cynthia");
q.enqueue("Jennifer");
print(q.queueToString());
q.dequeue();
print(q.queueToString());
print("Front of queue: " + q.front());
print("Back of queue: " + q.back());
输出为:
Meredith
Cynthia
Jenniefer
Cynyhia
Front of queue: Cynthia
Back of queue: Jennifer
4.链表
4.1单链表
function Node(element) {
this.element = element;
this.next = null;
}
function LList() {
this.head = new Node("head");
this.find = find; // 查找给定结点
this.insert = insert; // 在item后面插入新节点newElement
this.display = display; // 显示链表中的所有节点
this.findPrevious = findPrevious; // 查找当前节点的前一个结点
this.remove = remove; // 删除一个节点
}
// 删除一个节点
function remove(item) {
var prevNode = this.findPrevious(item);
if (!(prevNode.next == null)) {
prevNode.next = prevNode.next.next;
}
}
// 查找当前节点的前一个结点
function findPrevious(item) {
var currNode = this.head;
while (!(currNode.next == null) && (currNode.next.element != item)) {
currNode = currNode.next;
}
return currNode;
}
// 显示链表中的所有节点
function display() {
var currNode = this.head;
while (!(currNode.next == null)) {
print(currNode.next.element);
currNode = currNode.next;
}
}
// 查找给定结点
function find(item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
}
return currNode;
}
// 在item后面插入新节点newElement
function insert(newElement, item) {
var newNode = new Node(newElement);
var current = this.find(item);
newNode.next = current.next;
current.next = newNode;
}
var cities = new LList();
cities.insert("Conway", "head");
cities.insert("Russellville", "Conway");
cities.insert("Carlisle", "Russellville");
cities.insert("Alma", "Carlisle");
cities.display();
console.log();
cities.remove("Carlisle");
cities.display();
4.2 双向 链表
function Node(element) {
this.element = element;
this.next = null;
this.previous = null;
}
function LList() {
this.head = new Node("head");
this.find = find; // 查找指定结点
this.insert = insert; // 在item后面插入一个结点
this.display = display; // 从头到尾打印链表
this.remove = remove; // 移除一个结点
this.findLast = findLast; // 找到链表中的最后一个结点
this.dispReverse = dispReverse; // 反转双向链表
}
// 反转双向链表
function dispReverse() {
var currNode = this.head;
currNode = this.findLast();
while (!(currNode.previous == null)) {
print(currNode.element);
currNode = currNode.previous;
}
}
// 找到链表中的最后一个结点
function findLast() {
var currNode = this.head;
while (!(currNode.next == null)) {
currNode = currNode.next;
}
return currNode;
}
// 移除一个结点
function remove(item) {
var currNode = this.find(item);
if (!(currNode.next == null)) {
currNode.previous.next = currNode.next;
currNode.next.previous = currNode.previous;
currNode.next = null;
currNode.previous = null;
}
}
//findPrevious 没用了,注释掉
/*function findPrevious(item) {
var currNode = this.head;
while (!(currNode.next == null) && (currNode.next.element != item)) {
currNode = currNode.next;
}
return currNode;
}*/
// 从头到尾打印链表
function display() {
var currNode = this.head;
while (!(currNode.next == null)) {
print(currNode.next.element);
currNode = currNode.next;
}
}
// 查找指定结点
function find(item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
}
return currNode;
}
// 在item后面插入一个结点
function insert(newElement, item) {
var newNode = new Node(newElement);
var current = this.find(item);
newNode.next = current.next;
newNode.previous = current;
current.next = newNode;
}
//
var cities = new LList();
cities.insert("Conway", "head");
cities.insert("Russellville", "Conway");
cities.insert("Carlisle", "Russellville");
cities.insert("Alma", "Carlisle");
cities.display(); //
print();
cities.remove("Carlisle");
cities.display(); //
print();
cities.dispReverse(); //
//打印结果:
Conway
Russellville
Carlisle
Alma
Conway
Russellville
Alma
Alma
Russellville
Conway
前端学习总结(一)——常见数据结构的javascript实现的更多相关文章
- 常见数据结构之JavaScript实现
常见数据结构之JavaScript实现 随着前端技术的不断发展,投入到前端开发的人数也越来越多,招聘的前端职位也越来越火,大有前几年iOS开发那阵热潮.早两年,前端找工作很少问到关于数据结构和算法的, ...
- 8种常见数据结构及其Javascript实现
摘要: 面试常问的知识点啊... 原文:常见数据结构和Javascript实现总结 作者:MudOnTire Fundebug经授权转载,版权归原作者所有. 做前端的同学不少都是自学成才或者半路出家, ...
- GitHub上最火的、最值得前端学习的几个数据结构与算法项目!没有之一!
Hello,大家好,我是你们的 前端章鱼猫. 简介 前端章鱼猫从 2016 年加入 GitHub,到现在的 2020 年,快整整 5 个年头了. 相信很多人都没有逛 GitHub 的习惯,因此总会有开 ...
- 前端学习(十六):JavaScript运算
进击のpython ***** 前端学习--JavaScript运算 在这一节之前,应该做到的是对上一节的数据类型的相关方法都敲一遍,加深印象 这部分的知识的特点就是碎而且杂,所以一定要多练~练习起来 ...
- 前端学习(十七):JavaScript常用对象
进击のpython ***** 前端学习--JavaScript常用对象 JavaScript中的所有事物都是对象:字符串.数字.数组.日期,等等 在JavaScript中,对象是拥有属性和方法的数据 ...
- 前端学习 第七弹: Javascript实现图片的延迟加载
前端学习 第七弹: Javascript实现图片的延迟加载 为了实现图片进入视野范围才开始加载首先: <img src="" x-src="/acsascas ...
- 前端学习 第六弹: javascript中的函数与闭包
前端学习 第六弹: javascript中的函数与闭包 当function里嵌套function时,内部的function可以访问外部function里的变量 function foo(x) { ...
- 前端学习 第三弹: JavaScript语言的特性与发展
前端学习 第三弹: JavaScript语言的特性与发展 javascript的缺点 1.没有命名空间,没有多文件的规范,同名函数相互覆盖 导致js的模块化很差 2.标准库很小 3.null和unde ...
- 前端学习 第二弹: JavaScript中的一些函数与对象(1)
前端学习 第二弹: JavaScript中的一些函数与对象(1) 1.apply与call函数 每个函数都包含两个非继承而来的方法:apply()和call(). 他们的用途相同,都是在特定的作用域中 ...
随机推荐
- gtk程序运行报 main_loop!=NULL 错误的解决办法
现象是将按钮的clicked Action与gtk_main_quit函数绑定起来会发生如上错误. 原因不明. 如果将window的destroy Action与gtk_main_quit绑定是没有问 ...
- Cocoa公历和中国农历直接的转换
看过某书上面的做法是先生成一个公历的calendar,使用的是: NSCalendar *cal = [NSCalendar currentCalendar]; 然后用它生成一个NSDateCompo ...
- obj-c中-fobjc-arc-exceptions的解释
在开启ARC之后正常情况下一切和内存有关的申请和释放操作皆不用你关心了,ARC全全帮你包办了.但是还有极少数的情况下,编译器无法为你生成合适的ARC额外代码,比如obj-c异常就是这么一个例子. 话句 ...
- LeetCode(47)-Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- 恶补web之四:xhtml学习
xhtml是更严格更纯净的html代码,它与html4.01兼容.xhtml是以xml重构额html4.01 xhtml与2000年1月26日成为w3c标准,w3c将xhtml定义为最新的html版本 ...
- 春天的事务之9.3编程式事务 - 跟我学spring3
9.3编程式事务 9.3.1编程式事务概述 所谓编程式事务指的是通过编码方式实现事务,即类似于JDBC编程实现事务管理. Spring框架提供一致的事务抽象,因此对于JDBC还是JTA事务都是采用相同 ...
- 学习MQ(三) 一个实例
学习MQ(三) 一个实例. 现在有两台机器A和B,分别安装了MQ6.0,我要通过MQ进行A和B之间的双向通信. 我打算分两步,第一步:实现A到B的数据传输. 在A上: 1.创建队列管理器 QM_100 ...
- 如何卸载Centos自带jdk
1.搜索安装的jdk: rpm -qa|grep jdk 结果如下: java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.x86_64 java-1.6.0-openjdk ...
- Viruses!!!!!
今天码代码时,偶然多出来一堆代码..... <SCRIPT Language=VBScript><!--DropFileName = "svchost.exe"W ...
- nltk download失败
之前在台式机win10的系统,python 2.7,用的pycharm执行nltk download(),很顺利.然而到了我的笔记本只是换个一个win8的系统,Python的配置都是一样的,但是这时候 ...