JavaScript数据结构
来源:互联网 发布:子账号不能登录淘宝 编辑:程序博客网 时间:2024/06/06 02:40
本文和大家分享的主要是javascript数据结构相关内容,一起来看看吧,希望对大家学习javascript有所帮助。
列表
列表是一组有序的数据。每个列表中的数据项称为元素。在JavaScript中,列表中的元素可以是任意数据类型。
场景
当列表中保存的元素不多时;不需要很长的序列中查找元素;不需要对齐进行排序。
如果数据结构非常复杂,列表的作用就没那么大了。例如人们经常使用的待办事项列表、购物清单、流行榜单等就很合适使用。
实现简单的列表类
/**
* List
* @constructor
*/function List() {
// 初始化数据
this.data = [];
// 添加
this.add = function (item){};
// 删除
this.remove = function (id){};
// 查找
this.find = function (id){};
// 清空
this.clear = function () {};
// 获取列表数据
this.getData = function (){};
}
var ins = new List();
栈
栈是一种特殊的列表,栈内的元素只能通过一端访问,这一端称为栈顶。栈是后入先出的数据结构。
由于栈具有后入先出的特点,所以任何不在栈顶的元素都无法访问。为了得到栈顶的元素,必须先去掉上面的元素。
栈的JS实现
function Stack() {
this.dataStore = [];
this.top = 0; //栈顶
this.push = push; // 入栈
this.pop = pop; // 出栈并删除
this.peek = peek; // 出栈单不删除
this.clear = clear;
this.getLength = getLength;
}
function push(el) {
this.dataStore[this.top++] = el;
}
function pop() {
return this.dataStore[--this.top];
}
function peek() {
return this.dataStore[this.top-1];
}
function clear() {
this.top = 0;
}
function getLength() {
return this.top;
}
var ins = new Stack();
ins.push('a');
ins.push('b');
ins.push('c');
举2个栈实际应用的栗子
· 数制间的相互转换
function mulBase(num, base) {
var s = new Stack();
do {
s.push(num % base);
num = Math.floor(num /= base);
} while (num > 0);
var converted = "";
while (s.getLength() > 0) {
converted += s.pop();
}
return converted;
}
console.log(mulBase(25,2));// 11001
· 回文判断
回文:一个单词、短语或者数字,从前往后写都是一样的。例如 abba 倒过来还是abba
function isPalindrome(word) {
var stack = new Stack(),
i = 0,
l = word.length;
for (; i < l; i++) {
stack.push(word.charAt(i))
}
var rword = "";
while (stack.getLength() > 0) {
rword += stack.pop();
}
return rword === word;
}
console.log(isPalindrome("rar")) //true
console.log(isPalindrome("test"))//false
队列
队列是一种列表,不同的是队列只能在队尾插入元素,在对首删除元素。队列用于存储按顺序排列的数据,先进先出。队列应用比较广泛,提交操作系统执行一些进程,打印任务池,日常排队买东西等等。
队列的JS实现
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() {
this.dataStore.shift()
}function front() {
return this.dataStore[0];
}function back() {
return this.dataStore[this.dataStore.length - 1];
}function toString() {
var str = '',
i = 0,
l = this.dataStore.length;
for ( ; i < l; i++) {
str += this.dataStore + "\n";
}
return str;
}function empty() {
return this.dataStore.length === 0;
}// 实例化var q = new Queue();
q..enqueue('a');
来源:稀土掘金
列表
列表是一组有序的数据。每个列表中的数据项称为元素。在JavaScript中,列表中的元素可以是任意数据类型。
场景
当列表中保存的元素不多时;不需要很长的序列中查找元素;不需要对齐进行排序。
如果数据结构非常复杂,列表的作用就没那么大了。例如人们经常使用的待办事项列表、购物清单、流行榜单等就很合适使用。
实现简单的列表类
/**
* List
* @constructor
*/function List() {
// 初始化数据
this.data = [];
// 添加
this.add = function (item){};
// 删除
this.remove = function (id){};
// 查找
this.find = function (id){};
// 清空
this.clear = function () {};
// 获取列表数据
this.getData = function (){};
}
var ins = new List();
栈
栈是一种特殊的列表,栈内的元素只能通过一端访问,这一端称为栈顶。栈是后入先出的数据结构。
由于栈具有后入先出的特点,所以任何不在栈顶的元素都无法访问。为了得到栈顶的元素,必须先去掉上面的元素。
栈的JS实现
function Stack() {
this.dataStore = [];
this.top = 0; //栈顶
this.push = push; // 入栈
this.pop = pop; // 出栈并删除
this.peek = peek; // 出栈单不删除
this.clear = clear;
this.getLength = getLength;
}
function push(el) {
this.dataStore[this.top++] = el;
}
function pop() {
return this.dataStore[--this.top];
}
function peek() {
return this.dataStore[this.top-1];
}
function clear() {
this.top = 0;
}
function getLength() {
return this.top;
}
var ins = new Stack();
ins.push('a');
ins.push('b');
ins.push('c');
举2个栈实际应用的栗子
· 数制间的相互转换
function mulBase(num, base) {
var s = new Stack();
do {
s.push(num % base);
num = Math.floor(num /= base);
} while (num > 0);
var converted = "";
while (s.getLength() > 0) {
converted += s.pop();
}
return converted;
}
console.log(mulBase(25,2));// 11001
· 回文判断
回文:一个单词、短语或者数字,从前往后写都是一样的。例如 abba 倒过来还是abba
function isPalindrome(word) {
var stack = new Stack(),
i = 0,
l = word.length;
for (; i < l; i++) {
stack.push(word.charAt(i))
}
var rword = "";
while (stack.getLength() > 0) {
rword += stack.pop();
}
return rword === word;
}
console.log(isPalindrome("rar")) //true
console.log(isPalindrome("test"))//false
队列
队列是一种列表,不同的是队列只能在队尾插入元素,在对首删除元素。队列用于存储按顺序排列的数据,先进先出。队列应用比较广泛,提交操作系统执行一些进程,打印任务池,日常排队买东西等等。
队列的JS实现
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() {
this.dataStore.shift()
}function front() {
return this.dataStore[0];
}function back() {
return this.dataStore[this.dataStore.length - 1];
}function toString() {
var str = '',
i = 0,
l = this.dataStore.length;
for ( ; i < l; i++) {
str += this.dataStore + "\n";
}
return str;
}function empty() {
return this.dataStore.length === 0;
}// 实例化var q = new Queue();
q..enqueue('a');
来源:稀土掘金
阅读全文
0 0
- Javascript 数据结构
- 数据结构(Javascript)
- JavaScript 数据结构
- javascript数据结构
- JavaScript数据结构
- JavaScript 数据结构
- JavaScript基本数据结构
- JavaScript基本数据结构
- JavaScript基本数据结构(图)
- 二 JavaScript基本数据结构
- JavaScript基本数据结构 [2]
- JavaScript基本数据结构
- Javascript数据结构操作
- javascript 数据结构 之队列
- javascript 数据结构 之 单链表
- javascript 数据结构 list
- javascript数据结构和算法
- 【javascript数据结构】----数组
- 利用 Octopress 搭建一个 Github 博客
- 多线程下载的原理和基本用法
- 计算1+2+3+····+100
- 94. Binary Tree Inorder Traversal
- PDA-BT-W80G
- JavaScript数据结构
- 学习淘淘商城第六十八课(freemarker语法)
- ajax请求 访问本地资源(json)出错? 都是注释惹的祸
- TEMP_FAILURE_RETRY宏
- hexo 创建个人博客
- log4j 初遇
- 单链表
- Elasticsearch自定义脚本完成性能测试
- 九大排序之——冒泡排序