数据结构的javascript实现
栈
栈(stack)又名堆栈,是一种遵循后进先出(LIFO)原则的有序集合。新添加或待删除的元素都保存在栈的末尾,称作栈顶,另一端称作栈底。在栈里,新元素都靠近栈顶,旧元素都接近栈底。
function Stack() {
var data = [];
// 入栈:放在数组末尾
this.push = function(ele) {
data.push(ele);
};
// 出栈:弹出栈顶元素(数组末尾的元素)
this.pop = function() {
return data.pop();
};
// 查看栈顶元素
this.peek = function() {
return data[data.length - 1];
}
// 判断栈空:返回布尔
this.isAmpty = function() {
return data.length === 0
};
// 清空栈
this.clear = function() {
data = [];
};
// 返回栈的长度
this.size = function() {
return data.length;
};
// 以字符串显示栈中所有内容
this.print = function() {
console.log(data.toString());
};
}
var s = new Stack;
s.push("a");
s.push("b");
s.push("c");
s.push("d");
s.push("e");
s.print(); // a,b,c,d,e
队列
function print(x) {
console.log(x);
}
//////////////////////////////////
function Queue() {
this.data = []; // 队列用数组实现
this.enqueue = enqueue; // 入队
this.dequeue = dequeue; // 出队
this.front = front; // 求队头元素
this.back = back; // 求队尾元素
this.toString = toString;
this.empty = empty; // 判断队空
}
// 入队
function enqueue(ele) {
this.data.push(ele);
}
// 出队
function dequeue() {
return this.data.shift();
}
// 求队头元素
function front() {
return this.data[0];
}
// 求队尾元素
function back() {
return this.data[this.data.length - 1];
}
function toString() {
var retStr = "";
for (var i = 0; i < this.data.length; ++i) {
retStr += this.data[i] + "\n";
}
return retStr;
}
// 判断队空
function empty() {
if (this.data.length == 0) {
return true;
} else {
return false;
}
}
// 测试程序
var q = new Queue();
q.enqueue("Meredith");
q.enqueue("Cynthia");
q.enqueue("Jennifer");
print(q.toString());
// Meredith
// Cynthia
// Jennifer
q.dequeue();
print(q.toString());
// Cynthia
// Jennifer
print("Front of queue: " + q.front());
// Front of queue: Cynthia
print("Back of queue: " + q.back());
// Back of queue: Jennifer
单链表
function print(x) {
console.log(x);
}
//////////////////////////////////
// 结点类
function Node(element) {
this.element = element;
this.next = null;
}
// 方法类
function LList() {
this.head = new Node("head");
this.find = find;
this.insert = insert;
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后面插入newEle
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();
// Conway
// Russellville
// Carlisle
// Alma
print('\n')
cities.remove("Carlisle");
cities.display();
// Conway
// Russellville
// Alma
双向链表
// 结点类
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; // 在指定结点后面插入一个结点
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;
}
}
// 展示链表
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;
}
// 把新结点插入到指定结点后面
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('\n');
cities.remove("Carlisle");
cities.display();
print('\n');
cities.dispReverse();
打印结果:
二叉树(暂未调试)
// *模拟输出
function print(x) {
console.log(x);
}
function Node(data, left, right) {
this.data = data;
this.left = left;
this.right = right;
this.show = show;
}
function show() {
return this.data;
}
function BST() {
this.root = null;
this.insert = insert;
this.inOrder = inOrder;
}
function insert(data) {
var n = new Node(data, null, null);
if (this.root == null) {
this.root = n;
} else {
var current = this.root;
var parent;
while (true) {
parent = current;
if (data < current.data) {
current = current.left;
if (current == null) {
parent.left = n;
break;
}
} else {
current = current.right;
if (current == null) {
parent.right = n;
break;
}
}
}
}
}
function inOrder(node) {
if (!(node == null)) {
inOrder(node.left);
putstr(node.show() + " ");
inOrder(node.right);
}
}
function preOrder(node) {
if (!(node == null)) {
putstr(node.show() + " ");
preOrder(node.left);
preOrder(node.right);
}
}
function postOrder(node) {
if (!(node == null)) {
postOrder(node.left);
postOrder(node.right);
putstr(node.show() + " ");
}
}
function getMin() {
var current = this.root;
while (!(current.left == null)) {
current = current.left;
}
return current.data;
}
function getMax() {
var current = this.root;
while (!(current.right == null)) {
current = current.right;
}
return current.data;
}
function find(data) {
var current = this.root;
while (current != null) {
if (current.data == data) {
return current;
} else if (data < current.data) {
current = current.left;
} else {
current = current.right;
}
}
return null;
}
function remove(data) {
root = removeNode(this.root, data);
}
function removeNode(node, data) {
if (node == null) {
return null;
}
if (data == node.data) {
// 没有子节点的节点
if (node.left == null && node.right == null) {
return null;
}
// 没有左子节点的节点
if (node.left == null) {
return node.right;
}
// 没有右子节点的节点
if (node.right == null) {
return node.left;
}
// 有两个子节点的节点
var tempNode = getSmallest(node.right);
node.data = tempNode.data;
node.right = removeNode(node.right, tempNode.data);
return node;
} else if (data < node.data) {
node.left = removeNode(node.left, data);
return node;
} else {
node.right = removeNode(node.right, data);
return node;
}
}
数据结构的javascript实现的更多相关文章
- 常见数据结构之JavaScript实现
常见数据结构之JavaScript实现 随着前端技术的不断发展,投入到前端开发的人数也越来越多,招聘的前端职位也越来越火,大有前几年iOS开发那阵热潮.早两年,前端找工作很少问到关于数据结构和算法的, ...
- 8种常见数据结构及其Javascript实现
摘要: 面试常问的知识点啊... 原文:常见数据结构和Javascript实现总结 作者:MudOnTire Fundebug经授权转载,版权归原作者所有. 做前端的同学不少都是自学成才或者半路出家, ...
- 【数据结构的JavaScript版实现】data-struct-js的npm包初版作成
[数据结构的JavaScript版实现]data-struct-js的npm包初版作成 码路工人 CoderMonkey [数据结构的JavaScript版实现] 拖了这么久,终于趁着春节假期把初版( ...
- 前端学习总结(一)——常见数据结构的javascript实现
1.列表类 // 列表类 function List() { this.listSize = 0; // 列表的元素个数 this.pos = 0; // 列表的当前位置 this.dataStore ...
- 学习javascript数据结构(一)——栈和队列
前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...
- javascript数据结构-数组
github博客地址 简介 数组是最简单的数据结构,javascript语言也很早就原声支持了数组Array数据类型,和其他语言略微不同的是:js中的数组可以存储不同类型的值,看起来很厉害的样子,但是 ...
- javascript的基本语法、数据结构
本篇学习资料主要讲解javascript的基本语法.数据结构 无论是传统的编程语言,还是脚本语言,都具有数据类型.常量和变量.运算符.表达式.注释语句.流程控制语句等基本元素构成,这些基本元 ...
- javascript数据结构——队列
队列是一种先进先出的数据结.队列只能在队尾插入元素,在队首删除元素,这点和栈不一样.它用于存储顺序排列的数据.队列就像我们日常中的排队一样,排在最前面的第一个办理业务,新来的人只能在后面排队.队列这种 ...
- 为什么我要放弃javaScript数据结构与算法(第二章)—— 数组
第二章 数组 几乎所有的编程语言都原生支持数组类型,因为数组是最简单的内存数据结构.JavaScript里也有数组类型,虽然它的第一个版本并没有支持数组.本章将深入学习数组数据结构和它的能力. 为什么 ...
随机推荐
- Android TV listView焦点平滑移动
先上TV上效果图 Mark下思路: package com.test.ui; import java.lang.reflect.Method; import android.annotation.Su ...
- Mina源码阅读笔记(四)—Mina的连接IoConnector2
接着Mina源码阅读笔记(四)-Mina的连接IoConnector1,,我们继续: AbstractIoAcceptor: 001 package org.apache.mina.core.rewr ...
- 抛开rails使用ActiveRecord连接数据库
今天是大年三十,明天就正式进入羊年鸟,给所有程序猿(媛)同人拜个年吧!祝大家身体健康,事业有成,财源广进哦! 话归正题,以前都是在rails中使用数据库,或者在rails的console中使用:我们如 ...
- 恶补web之三:http学习
http是超文本传输协议的简称,该协议设计目的是保证客户机与服务器之间的通信.http的工作方式为客户机与服务器之间的请求-应答协议. 一般来说web浏览器是客户端,计算机上的网络应用程序可能作为服务 ...
- sudoku solver(数独)
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Spring中对象和属性的注入方式
一:Spring的bean管理 1.xml方式 bean实例化三种xml方式实现 第一种 使用类的无参数构造创建,首先类中得有无参构造器(重点) 第二种 使用静态工厂创建 (1)创建静态的方法,返回类 ...
- List,Set,Map三种接口的区别
set --其中的值不允许重复,无序的数据结构 list --其中的值允许重复,因为其为有序的数据结构 map--成对的数据结构,健值必须具有唯一性(键不能同,否则值替换) List按对象进 ...
- 一个resin启动bug的解决
这个bug的问题后来被确认为Resin所在目录层有中文目录名.--------------------------------------------------------------------- ...
- ubuntu 14.04 安装svn server (subversionedge )
ubuntu 14.04 安装subversionedge 请仔细阅读安装包自带的readme文件! 1.先去官网,找安装包: http://subversion.apache.org/ http:/ ...
- JSF-使用JSF标记
使用JSF标记 基于Facelets技术的JSF页面是一个 XHTML页面,文件扩展名为 .xhtml 1)JSF页面可用html标记,但必须满足: ①所有标记都必须闭合.如<p>开始,& ...