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. Volley网络框架完全解析(实战篇)

    好了,今天就通过一个瀑布流demo,来使用Volley框架请求网络图片. 前言: 我们使用NetworkImageView显示图片: 1.因为该控件可以自动的管理好请求的生命周期,当与父控件detac ...

  2. hbase thrift 定义

    /*  * Licensed to the Apache Software Foundation (ASF) under one  * or more contributor license agre ...

  3. iOS监听模式系列之对APNs的认知与理解

    前言: APNs 协议在近两年的 WWDC 上改过两次, 15 年 12 月 17 日更是推出了革命性的新特性.但在国内传播的博客.面试题里关于 APNs 的答案全都是旧的.错的. 导航: 对 APN ...

  4. 使用微服务架构思想,设计部署OAuth2.0授权认证框架

    1,授权认证与微服务架构 1.1,由不同团队合作引发的授权认证问题 去年的时候,公司开发一款新产品,但人手不够,将B/S系统的Web开发外包,外包团队使用Vue.js框架,调用我们的WebAPI,但是 ...

  5. miniUI input设置默认值,js获取年月注意事项,数据库nvl函数使用

    2017-6-5周一,今天碰到的一个需求是:两税附征模块进入页面筛选时间默认值为当前月的上一个月,并根据筛选结果显示数据,我们用的框架为miniUI. 坑1: 默认值设置,刚刚接触miniUI,对里面 ...

  6. SVN服务器搭建和配置使用详解

    SVN服务器搭建和使用(一) Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上 ...

  7. javaScript(3)---语法、关键保留字及变量

    学习要点: 1.语法构成 2.关键字保留字 3.变量 任何语言的核心都必然会描述这门语言最基本的工作原理.而JavaScript的语言核心就是ECMAScript 一.语法构成 区分大小写:ECMAS ...

  8. win8 JDK环境变量不生效

    执行where java  看一下路径对不对,如果对的话就把system32下面的3个java相关的exe删了即可,如果路径不对就修改环境变量.

  9. SQL解决"双重职位的查询"

    双重身份问题: create table role_tab ( person char(5) not null, role  char(1) not null ) insert into role_t ...

  10. 关于mysql保存数据的时候报问题分析  普通的字符串或者表情都是占位3个字节,所以utf8足够用了,但是移动端的表情符号占位是4个字节,普通的utf8就不够用了,为了应对无线互联网的机遇和挑战、避免 emoji 表情符号带来的问题、涉及无线相关的 MySQL 数据库建议都提前采用 utstring value:'\xF0\x9F\x98\x82\xF0\x9F...' for ...

    问题分析 普通的字符串或者表情都是占位3个字节,所以utf8足够用了,但是移动端的表情符号占位是4个字节,普通的utf8就不够用了,为了应对无线互联网的机遇和挑战.避免 emoji 表情符号带来的问题 ...