前端学习总结(一)——常见数据结构的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(). 他们的用途相同,都是在特定的作用域中 ...
随机推荐
- 解决os x下gdb不能调试的问题
在较新的os x中使用gdb调试程序会发生 Unable to find Mach task port 之类的错误,这是由于新的os x内核禁止未授权的程序随意控制其他进程,如果不用root权限的话, ...
- iPhone实现自定义多选列表
好久没更新博客了,今天写了一个自定义的多选列表,可以跟爱学习的各位进行分享,首先我们先来看一下效果图: 一般大家都是用UITableView自己的编辑模式来实现CheckBox的,这里我们用自定义Ce ...
- LeetCode(33)-Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- C# /VB.NET 插入、修改、删除Word批注
批注内容可以是对某段文字或内容的注释,也可以是对文段中心思想的概括提要,或者是对文章内容的评判.疑问,以及在阅读时给自己或他人起到提示作用.本篇文章中将介绍如何在C#中操作Word批注,主要包含以下要 ...
- tomcat 工作原理
Tomcat原理 分类: TOMCAT2009-05-17 22:25 4366人阅读 评论(3) 收藏 举报 tomcatexceptionsocketstringservernull Tomcat ...
- SpringBoot使用Maven插件打包部署
[问题] 之前一直用SpringBoot做一些小项目,想打包部署在环境上,总是少依赖包jar.百度下可以通过Spring Boot Maven plugin插件,把Maven配置的依赖包都打到项目包里 ...
- 自动红眼移除算法 附c++完整代码
说起红眼算法,这个话题非常古老了. 百度百科上的描述: "红眼"一般是指在人物摄影时,当闪光灯照射到人眼的时候,瞳孔放大而产生的视网膜泛红现象. 由于红眼现象的程度是根据拍摄对象色 ...
- 比较Fink, macports 跟 homebrew
http://www.myexception.cn/brew/412107.html 比较Fink, macports 和 homebrew 如果你有Linux/Unix背景,那么在Mac上你一定想安 ...
- Codeforces Round #479 (Div. 3) A. Wrong Subtraction
题目网址:http://codeforces.com/contest/977/problem/A 题解:给你一个数n,进行k次变换,从末尾开始-1,512变成511,511变成510,510会把0消掉 ...
- java监听器之实现在线人数显示
在码农的世界里只有bug才能让人成长,The more bugs you encounter, the more efficient you will be! java中的监听器分为三种:Servle ...