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实现的更多相关文章

  1. 常见数据结构之JavaScript实现

    常见数据结构之JavaScript实现 随着前端技术的不断发展,投入到前端开发的人数也越来越多,招聘的前端职位也越来越火,大有前几年iOS开发那阵热潮.早两年,前端找工作很少问到关于数据结构和算法的, ...

  2. 8种常见数据结构及其Javascript实现

    摘要: 面试常问的知识点啊... 原文:常见数据结构和Javascript实现总结 作者:MudOnTire Fundebug经授权转载,版权归原作者所有. 做前端的同学不少都是自学成才或者半路出家, ...

  3. GitHub上最火的、最值得前端学习的几个数据结构与算法项目!没有之一!

    Hello,大家好,我是你们的 前端章鱼猫. 简介 前端章鱼猫从 2016 年加入 GitHub,到现在的 2020 年,快整整 5 个年头了. 相信很多人都没有逛 GitHub 的习惯,因此总会有开 ...

  4. 前端学习(十六):JavaScript运算

    进击のpython ***** 前端学习--JavaScript运算 在这一节之前,应该做到的是对上一节的数据类型的相关方法都敲一遍,加深印象 这部分的知识的特点就是碎而且杂,所以一定要多练~练习起来 ...

  5. 前端学习(十七):JavaScript常用对象

    进击のpython ***** 前端学习--JavaScript常用对象 JavaScript中的所有事物都是对象:字符串.数字.数组.日期,等等 在JavaScript中,对象是拥有属性和方法的数据 ...

  6. 前端学习 第七弹: Javascript实现图片的延迟加载

    前端学习 第七弹: Javascript实现图片的延迟加载 为了实现图片进入视野范围才开始加载首先: <img    src="" x-src="/acsascas ...

  7. 前端学习 第六弹: javascript中的函数与闭包

    前端学习 第六弹:  javascript中的函数与闭包 当function里嵌套function时,内部的function可以访问外部function里的变量 function foo(x) {   ...

  8. 前端学习 第三弹: JavaScript语言的特性与发展

    前端学习 第三弹: JavaScript语言的特性与发展 javascript的缺点 1.没有命名空间,没有多文件的规范,同名函数相互覆盖 导致js的模块化很差 2.标准库很小 3.null和unde ...

  9. 前端学习 第二弹: JavaScript中的一些函数与对象(1)

    前端学习 第二弹: JavaScript中的一些函数与对象(1) 1.apply与call函数 每个函数都包含两个非继承而来的方法:apply()和call(). 他们的用途相同,都是在特定的作用域中 ...

随机推荐

  1. SharePoint 开发TimerJob 介绍

    项目需要写TimerJob,以前也大概知道原理,不过,开发过程中,还是遇到一些问题,网上看了好多博客,也有写的灰常好的,不过,自己还是想再写一下,也算是给自己一个总结,也算给大家多一个参考吧. Tim ...

  2. SharePoint 2010 -- .Net托管客户端模型简单示例

    .Net托管客户端模型,是SharePoint2010推出的三种客户端模型".NET托管"."ECMAScript"."Sliverlight&quo ...

  3. mac os x下Dreamweaver如何还原初始配置

    上次在mac下修改Dreamweaver(以下简称dw)时,不知动了哪里,导致打开html文档时设计按钮变灰不能使用!这个太蛋疼了,只能在浏览器中查看效果,live按钮更不用说也是灰化状态. 于是使用 ...

  4. centos 7下安装python 3.6笔记

    每次在centos上安装python 3都需要重新查资料,这次索性自己记下笔记. 首先安装gcc yum -y install gccyum install zlib-devel./configure ...

  5. PLSQL 创建自定义函数注意事项

    2017-6-8周四,今天遇到的需求是,从数据库中查找出某张表的某些数据,并将这些数据做简单的加减运算再得到结果集,没有思路,后来问辉哥,给我的建议是给这些运算封装成一个SQL函数,select选择字 ...

  6. WebService技术简介

    今天继续阅读<.Net 大局观>时看到一段关于WebService支持技术的论述,真是简明扼要: Web services的另一个重要应用是B2B整合,一般来说它也依赖Internet,将 ...

  7. 编码与Python的基础

    编码 在linux 系统或者Python2版本中要用Python这门语言呢,就需要在开头加上 # -*- coding:utf8 -*- 这个语句是说呀,当机器编译你写的程序的时候是用utf-8这种编 ...

  8. CSS 弹性容器

    该文章为英文原文译文及一些自己的拙见墙裂推荐读原文浏览原文请戳这里 : CSS-STRICKS 弹性布局 (Flexbox Layout) 什么是弹性布局 Flexbox Layout 模块旨在提供一 ...

  9. C#WebService 出现No 'Access-Control-Allow-Origin' header is present on the requested resource

    C#WebService 出现No 'Access-Control-Allow-Origin' header is present on the requested resource 解决办法: 在c ...

  10. Flipping Parentheses~Gym 100803G

    Description A string consisting only of parentheses '(' and ')' is called balanced if it is one of t ...