栈(stack)又名堆栈,是一种遵循后进先出(LIFO)原则的有序集合。新添加或待删除的元素都保存在栈的末尾,称作栈顶,另一端称作栈底。在栈里,新元素都靠近栈顶,旧元素都接近栈底。

  1. function Stack() {
  2. var data = [];
  3. // 入栈:放在数组末尾
  4. this.push = function(ele) {
  5. data.push(ele);
  6. };
  7. // 出栈:弹出栈顶元素(数组末尾的元素)
  8. this.pop = function() {
  9. return data.pop();
  10. };
  11. // 查看栈顶元素
  12. this.peek = function() {
  13. return data[data.length - 1];
  14. }
  15. // 判断栈空:返回布尔
  16. this.isAmpty = function() {
  17. return data.length === 0
  18. };
  19. // 清空栈
  20. this.clear = function() {
  21. data = [];
  22. };
  23. // 返回栈的长度
  24. this.size = function() {
  25. return data.length;
  26. };
  27. // 以字符串显示栈中所有内容
  28. this.print = function() {
  29. console.log(data.toString());
  30. };
  31. }
  32. var s = new Stack;
  33. s.push("a");
  34. s.push("b");
  35. s.push("c");
  36. s.push("d");
  37. s.push("e");
  38. s.print(); // a,b,c,d,e

队列

  1. function print(x) {
  2. console.log(x);
  3. }
  4. //////////////////////////////////
  5. function Queue() {
  6. this.data = []; // 队列用数组实现
  7. this.enqueue = enqueue; // 入队
  8. this.dequeue = dequeue; // 出队
  9. this.front = front; // 求队头元素
  10. this.back = back; // 求队尾元素
  11. this.toString = toString;
  12. this.empty = empty; // 判断队空
  13. }
  14. // 入队
  15. function enqueue(ele) {
  16. this.data.push(ele);
  17. }
  18. // 出队
  19. function dequeue() {
  20. return this.data.shift();
  21. }
  22. // 求队头元素
  23. function front() {
  24. return this.data[0];
  25. }
  26. // 求队尾元素
  27. function back() {
  28. return this.data[this.data.length - 1];
  29. }
  30. function toString() {
  31. var retStr = "";
  32. for (var i = 0; i < this.data.length; ++i) {
  33. retStr += this.data[i] + "\n";
  34. }
  35. return retStr;
  36. }
  37. // 判断队空
  38. function empty() {
  39. if (this.data.length == 0) {
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. }
  45. // 测试程序
  46. var q = new Queue();
  47. q.enqueue("Meredith");
  48. q.enqueue("Cynthia");
  49. q.enqueue("Jennifer");
  50. print(q.toString());
  51. // Meredith
  52. // Cynthia
  53. // Jennifer
  54. q.dequeue();
  55. print(q.toString());
  56. // Cynthia
  57. // Jennifer
  58. print("Front of queue: " + q.front());
  59. // Front of queue: Cynthia
  60. print("Back of queue: " + q.back());
  61. // Back of queue: Jennifer

单链表

  1. function print(x) {
  2. console.log(x);
  3. }
  4. //////////////////////////////////
  5. // 结点类
  6. function Node(element) {
  7. this.element = element;
  8. this.next = null;
  9. }
  10. // 方法类
  11. function LList() {
  12. this.head = new Node("head");
  13. this.find = find;
  14. this.insert = insert;
  15. this.display = display;
  16. this.findPrevious = findPrevious;
  17. this.remove = remove;
  18. }
  19. // 移除一个结点
  20. function remove(item) {
  21. var prevNode = this.findPrevious(item);
  22. if (!(prevNode.next == null)) {
  23. prevNode.next = prevNode.next.next;
  24. }
  25. }
  26. // 找到当前结点的前一个结点
  27. function findPrevious(item) {
  28. var currNode = this.head;
  29. while (!(currNode.next == null) &&
  30. (currNode.next.element != item)) {
  31. currNode = currNode.next;
  32. }
  33. return currNode;
  34. }
  35. // 展示所有结点
  36. function display() {
  37. var currNode = this.head;
  38. while (!(currNode.next == null)) {
  39. print(currNode.next.element);
  40. currNode = currNode.next;
  41. }
  42. }
  43. // 查找指定结点
  44. function find(item) {
  45. var currNode = this.head;
  46. while (currNode.element != item) {
  47. currNode = currNode.next;
  48. }
  49. return currNode;
  50. }
  51. // 在item后面插入newEle
  52. function insert(newElement, item) {
  53. var newNode = new Node(newElement);
  54. var current = this.find(item);
  55. newNode.next = current.next;
  56. current.next = newNode;
  57. }
  58. var cities = new LList();
  59. cities.insert("Conway", "head");
  60. cities.insert("Russellville", "Conway");
  61. cities.insert("Carlisle", "Russellville");
  62. cities.insert("Alma", "Carlisle");
  63. cities.display();
  64. // Conway
  65. // Russellville
  66. // Carlisle
  67. // Alma
  68. print('\n')
  69. cities.remove("Carlisle");
  70. cities.display();
  71. // Conway
  72. // Russellville
  73. // Alma

双向链表

  1. // 结点类
  2. function Node(element) {
  3. this.element = element;
  4. this.next = null;
  5. this.previous = null;
  6. }
  7. // 方法类
  8. function LList() {
  9. this.head = new Node("head");
  10. this.find = find; // 查找给定数据
  11. this.insert = insert; // 在指定结点后面插入一个结点
  12. this.display = display; // 展示链表
  13. this.remove = remove; // 移除一个结点
  14. this.findLast = findLast; // 找到最后一个结点
  15. this.dispReverse = dispReverse; // 反序展示双向链表
  16. }
  17. // 反序展示链表
  18. function dispReverse() {
  19. var currNode = this.head;
  20. currNode = this.findLast();
  21. while (!(currNode.previous == null)) {
  22. print(currNode.element);
  23. currNode = currNode.previous;
  24. }
  25. }
  26. // 找到最后一个结点
  27. function findLast() {
  28. var currNode = this.head;
  29. while (!(currNode.next == null)) {
  30. currNode = currNode.next;
  31. }
  32. return currNode;
  33. }
  34. // 移除一个结点
  35. function remove(item) {
  36. var currNode = this.find(item);
  37. if (!(currNode.next == null)) {
  38. currNode.previous.next = currNode.next;
  39. currNode.next.previous = currNode.previous;
  40. currNode.next = null;
  41. currNode.previous = null;
  42. }
  43. }
  44. // 展示链表
  45. function display() {
  46. var currNode = this.head;
  47. while (!(currNode.next == null)) {
  48. print(currNode.next.element);
  49. currNode = currNode.next;
  50. }
  51. }
  52. // 找到指定结点
  53. function find(item) {
  54. var currNode = this.head;
  55. while (currNode.element != item) {
  56. currNode = currNode.next;
  57. }
  58. return currNode;
  59. }
  60. // 把新结点插入到指定结点后面
  61. function insert(newElement, item) {
  62. var newNode = new Node(newElement);
  63. var current = this.find(item);
  64. newNode.next = current.next;
  65. newNode.previous = current;
  66. current.next = newNode;
  67. }
  68. var cities = new LList();
  69. cities.insert("Conway", "head");
  70. cities.insert("Russellville", "Conway");
  71. cities.insert("Carlisle", "Russellville");
  72. cities.insert("Alma", "Carlisle");
  73. cities.display();
  74. print('\n');
  75. cities.remove("Carlisle");
  76. cities.display();
  77. print('\n');
  78. cities.dispReverse();

打印结果:

二叉树(暂未调试)

  1. // *模拟输出
  2. function print(x) {
  3. console.log(x);
  4. }
  5. function Node(data, left, right) {
  6. this.data = data;
  7. this.left = left;
  8. this.right = right;
  9. this.show = show;
  10. }
  11. function show() {
  12. return this.data;
  13. }
  14. function BST() {
  15. this.root = null;
  16. this.insert = insert;
  17. this.inOrder = inOrder;
  18. }
  19. function insert(data) {
  20. var n = new Node(data, null, null);
  21. if (this.root == null) {
  22. this.root = n;
  23. } else {
  24. var current = this.root;
  25. var parent;
  26. while (true) {
  27. parent = current;
  28. if (data < current.data) {
  29. current = current.left;
  30. if (current == null) {
  31. parent.left = n;
  32. break;
  33. }
  34. } else {
  35. current = current.right;
  36. if (current == null) {
  37. parent.right = n;
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. }
  44. function inOrder(node) {
  45. if (!(node == null)) {
  46. inOrder(node.left);
  47. putstr(node.show() + " ");
  48. inOrder(node.right);
  49. }
  50. }
  51. function preOrder(node) {
  52. if (!(node == null)) {
  53. putstr(node.show() + " ");
  54. preOrder(node.left);
  55. preOrder(node.right);
  56. }
  57. }
  58. function postOrder(node) {
  59. if (!(node == null)) {
  60. postOrder(node.left);
  61. postOrder(node.right);
  62. putstr(node.show() + " ");
  63. }
  64. }
  65. function getMin() {
  66. var current = this.root;
  67. while (!(current.left == null)) {
  68. current = current.left;
  69. }
  70. return current.data;
  71. }
  72. function getMax() {
  73. var current = this.root;
  74. while (!(current.right == null)) {
  75. current = current.right;
  76. }
  77. return current.data;
  78. }
  79. function find(data) {
  80. var current = this.root;
  81. while (current != null) {
  82. if (current.data == data) {
  83. return current;
  84. } else if (data < current.data) {
  85. current = current.left;
  86. } else {
  87. current = current.right;
  88. }
  89. }
  90. return null;
  91. }
  92. function remove(data) {
  93. root = removeNode(this.root, data);
  94. }
  95. function removeNode(node, data) {
  96. if (node == null) {
  97. return null;
  98. }
  99. if (data == node.data) {
  100. // 没有子节点的节点
  101. if (node.left == null && node.right == null) {
  102. return null;
  103. }
  104. // 没有左子节点的节点
  105. if (node.left == null) {
  106. return node.right;
  107. }
  108. // 没有右子节点的节点
  109. if (node.right == null) {
  110. return node.left;
  111. }
  112. // 有两个子节点的节点
  113. var tempNode = getSmallest(node.right);
  114. node.data = tempNode.data;
  115. node.right = removeNode(node.right, tempNode.data);
  116. return node;
  117. } else if (data < node.data) {
  118. node.left = removeNode(node.left, data);
  119. return node;
  120. } else {
  121. node.right = removeNode(node.right, data);
  122. return node;
  123. }
  124. }

数据结构的javascript实现的更多相关文章

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

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

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

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

  3. 【数据结构的JavaScript版实现】data-struct-js的npm包初版作成

    [数据结构的JavaScript版实现]data-struct-js的npm包初版作成 码路工人 CoderMonkey [数据结构的JavaScript版实现] 拖了这么久,终于趁着春节假期把初版( ...

  4. 前端学习总结(一)——常见数据结构的javascript实现

    1.列表类 // 列表类 function List() { this.listSize = 0; // 列表的元素个数 this.pos = 0; // 列表的当前位置 this.dataStore ...

  5. 学习javascript数据结构(一)——栈和队列

    前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...

  6. javascript数据结构-数组

    github博客地址 简介 数组是最简单的数据结构,javascript语言也很早就原声支持了数组Array数据类型,和其他语言略微不同的是:js中的数组可以存储不同类型的值,看起来很厉害的样子,但是 ...

  7. javascript的基本语法、数据结构

    本篇学习资料主要讲解javascript的基本语法.数据结构      无论是传统的编程语言,还是脚本语言,都具有数据类型.常量和变量.运算符.表达式.注释语句.流程控制语句等基本元素构成,这些基本元 ...

  8. javascript数据结构——队列

    队列是一种先进先出的数据结.队列只能在队尾插入元素,在队首删除元素,这点和栈不一样.它用于存储顺序排列的数据.队列就像我们日常中的排队一样,排在最前面的第一个办理业务,新来的人只能在后面排队.队列这种 ...

  9. 为什么我要放弃javaScript数据结构与算法(第二章)—— 数组

    第二章 数组 几乎所有的编程语言都原生支持数组类型,因为数组是最简单的内存数据结构.JavaScript里也有数组类型,虽然它的第一个版本并没有支持数组.本章将深入学习数组数据结构和它的能力. 为什么 ...

随机推荐

  1. Android特效专辑(五)——自定义圆形头像和仿MIUI卸载动画—粒子爆炸

    Android特效专辑(五)--自定义圆形头像和仿MIUI卸载动画-粒子爆炸 好的,各位亲爱的朋友,今天讲的特效还是比较炫的,首先,我们会讲一个自定义圆形的imageView,接着,我们会来实现粒子爆 ...

  2. objective-c 2.0的字面量Literals

    obj-c 2.0增加了许多核心对象字面量的简单语法,向ruby学习吗? 直接上代码: #import <Foundation/Foundation.h> int main(void){ ...

  3. left join 原理分析

    left join 原理分析 [转贴 2006-11-15 16:19:50]     字号:大 中 小 案例分析 user表:  id   | name --------- 1   | libk   ...

  4. Oracle100w数据大表割接

    [现网问题] 最近在给咪咕做视频后台管理,移动那边希望页面上,码流字段可以支持1位小数,如8.0.自己查看数据库,发现码流字段是Number整型,也就是要换类型,打算直接换成varchar2.因为自己 ...

  5. R实战 第七篇:网格(grid)

    grid包是R底层的图形系统,可以绘制几乎所有的图形.除了绘制图形之外,grid包还能对图形进行布局.在绘图时,有时候会遇到这样一种情景,客户想把多个代表不同KPI的图形分布到同一个画布(Page)上 ...

  6. ubuntu11.04安装nginx+php+mysql

    先列参考内容,后面我再补充点东西: http://www.4wei.cn/archives/1001436 http://www.gidot.net/blog/article.asp?id=322 上 ...

  7. Spring Security简明实践及相关国际化处理

    别人的都是最佳实践,因为我目前的设置没有按照参考文档推荐,还是采用DelegatingFilterProxy,所以我只能说简明实践.先贴我的applicationContext-security.xm ...

  8. Socket.io文字直播聊天室的简单代码

    直接上代码吧,被注释掉的主要是调试代码,和技术选型的测试代码 var app = require('express')(); var server = require('http').Server(a ...

  9. Pycharm安装教程

    1.下载PyQt 官方网站:http://www.riverbankcomputing.com/software/pyqt/download5 我的操作系统是64位的,安装的是Python3.4.3, ...

  10. Quartz学习-- quartz基本介绍和 Cron表达式

    Quartz学习 一. Quartz 大致简介 Quartz 是完全由 java 开发的一个开源的任务日程管理系统 ​任务日程管理系统 换句话说就是: ​ 一个预先确定的日程时间到达时,负责执行任务的 ...