澳门威利斯人_威利斯人娱乐「手机版」

来自 澳门威利斯人 2019-08-31 17:47 的文章
当前位置: 澳门威利斯人 > 澳门威利斯人 > 正文

JS使用单链表统计英语单词出现次数,js环境下

(function(){
 "use strict";

 function Node(element){
  this.element = element;
  this.next = null;
 }

 module.exports = Node;
})();

Node.js情形下JavaScript实现单链表与双链表结构,node.js单链

单链表(LinkedList)的javascript实现 npmjs相关库:
complex-list、smart-list、singly-linked-list
编制程序思路:

  • add方法用于将成分追加到链表尾巴部分,借由insert方法来兑现;
  • 留神种种函数的境界条件管理。

团结的落到实处:

SingleNode.js

(function(){
 "use strict";

 function Node(element){
  this.element = element;
  this.next = null;
 }

 module.exports = Node;
})();

LinkedList.js

(function(){
 "use strict";

 var Node = require("./lib/SingleNode");

 function LinkedList(){
  this._head = new Node("This is Head Node.");
  this._size = 0;
 }

 LinkedList.prototype.isEmpty = function(){
  return this._size === 0;
 };

 LinkedList.prototype.size = function(){
  return this._size;
 };

 LinkedList.prototype.getHead = function(){
  return this._head;
 };

 LinkedList.prototype.display = function(){
  var currNode = this.getHead().next;
  while(currNode){
   console.log(currNode.element);
   currNode = currNode.next;
  }
 };

 LinkedList.prototype.remove = function(item){
  if(item) {
   var preNode = this.findPre(item);
   if(preNode == null)
    return ;
   if (preNode.next !== null) {
    preNode.next = preNode.next.next;
    this._size--;
   }
  }
 };

 LinkedList.prototype.add = function(item){
  this.insert(item);
 };

 LinkedList.prototype.insert = function(newElement, item){
  var newNode = new Node(newElement);
  var finder = item ? this.find(item) : null;
  if(!finder){
   var last = this.findLast();
   last.next = newNode;
  }
  else{
   newNode.next = finder.next;
   finder.next = newNode;
  }
  this._size  ;
 };

 /*********************** Utility Functions ********************************/

 LinkedList.prototype.findLast = function(){
  var currNode = this.getHead();
  while(currNode.next){
   currNode = currNode.next;
  }
  return currNode;
 };

 LinkedList.prototype.findPre = function(item){
  var currNode = this.getHead();
  while(currNode.next !== null && currNode.next.element !== item){
   currNode = currNode.next;
  }
  return currNode;
 };

 LinkedList.prototype.find = function(item){
  if(item == null)
   return null;
  var currNode = this.getHead();
  while(currNode && currNode.element !== item){
   currNode = currNode.next;
  }
  return currNode;
 };

 module.exports = LinkedList;
})();

双链表(DoubleLinkedList)的javascript实现 npmjs相关库:
complex-list、smart-list
编制程序思路:

  • 双链表多了一个针对前趋的指针,故单链表中的帮忙函数findPre就无需了;
  • 追加了反向输出方法;
  • 留心边界条件的管理。

谐和的落实
DoubleNode.js

(function(){
 "use strict";

 function Node(element){
  this.element = element;
  this.next = null;
  this.previous = null;
 }

 module.exports = Node;
})();

DoubleLinkedList.js

(function(){
 "use strict";
 var Node = require("./lib/DoubleNode");

 function DoubleLinkedList(){
  this._head = new Node("This is Head Node.");
  this._size = 0;
 }

 DoubleLinkedList.prototype.getHead = function(){
  return this._head;
 };

 DoubleLinkedList.prototype.isEmpty = function(){
  return this._size === 0;
 };

 DoubleLinkedList.prototype.size = function(){
  return this._size;
 };

 DoubleLinkedList.prototype.findLast = function(){
  var currNode = this.getHead();
  while(currNode.next){
   currNode = currNode.next;
  }
  return currNode;
 };

 DoubleLinkedList.prototype.add = function(item){
  if(item == null)
   return null;
  this.insert(item);
 };

 DoubleLinkedList.prototype.remove = function(item){
  if(item) {
   var node = this.find(item);
   if(node == null)
    return ;
   if (node.next === null) {
    node.previous.next = null;
    node.previous = null;
   } else{
    node.previous.next = node.next;
    node.next.previous = node.previous;
    node.next = null;
    node.previous = null;
   }
   this._size--;
  }
 };

 DoubleLinkedList.prototype.find = function(item){
  if(item == null)
   return null;
  var currNode = this.getHead();
  while(currNode && currNode.element !== item){
   currNode = currNode.next;
  }
  return currNode;
 };

 DoubleLinkedList.prototype.insert = function(newElement, item){
  var newNode = new Node(newElement);
  var finder = item ? this.find(item) : null;
  if(!finder){
   var last = this.findLast();
   newNode.previous = last;
   last.next = newNode;
  }
  else{
   newNode.next = finder.next;
   newNode.previous = finder;
   finder.next.previous = newNode;
   finder.next = newNode;
  }
  this._size  ;
 };

 DoubleLinkedList.prototype.dispReverse = function(){
  var currNode = this.findLast();
  while(currNode != this.getHead()){
   console.log(currNode.element);
   currNode = currNode.previous;
  }
 };

 DoubleLinkedList.prototype.display = function(){
  var currNode = this.getHead().next;
  while(currNode){
   console.log(currNode.element);
   currNode = currNode.next;
  }
 };

 module.exports = DoubleLinkedList;
})();

单链表(LinkedList)的javascript实现 npmjs相关库: complex-list、smart-list、singly-linked...

效果:

您大概感兴趣的篇章:

  • javascript写的一个链表完结代码
  • js单向链表的现实性完成实例
  • JavaScript数据结构之链表的实现
  • JavaScript贯彻的链表数据结构实例
  • javascript循环链表之Joseph环的达成格局
  • JS使用单链表总结土耳其共和国(The Republic of Turkey)语单词出现次数
  • JavaScript数据结构之单链表和循环链表

 图片 1

  • 双链表多了四个针对性前趋的指针,故单链表中的协助函数findPre就不须要了;
  • 日增了反向输出方法;
  • 瞩目边界条件的拍卖。

如上正是本文的全体内容,希望对大家的就学抱有援助,也指望我们多多支持脚本之家。

DoubleLinkedList.js

您只怕感兴趣的篇章:

  • javascript写的二个链表达成代码
  • js单向链表的切实落到实处实例
  • JavaScript数据结构之链表的完成
  • JavaScript贯彻的链表数据结构实例
  • Node.js意况下JavaScript达成单链表与双链表结构
  • javascript循环链表之Joseph环的落到实处格局
  • JavaScript数据结构之单链表和循环链表
(function(){
 "use strict";

 function Node(element){
  this.element = element;
  this.next = null;
  this.previous = null;
 }

 module.exports = Node;
})();
<html>
<head>
 <title>Linked List</title>
 <meta charset="utf-8">
</head>
<body>
<script type="text/javascript">

function Node(data) { 
  this.data = data; 
  this.frequency =1;
  this.next = null; 
   }   
var SList =function SList() { 
  this.head = new Node("Dummy"); 
   } 
SList.prototype.insertLast =function(data) { 
  var p = this.head; 
  while (p.next!=null) 
  p = p.next;
  p.next=new Node(data); 
  } 
SList.prototype.insertFirst =function(data) { 
  var p=new Node(data); 
  p.next = this.head.next; 
  this.head.next=p;
  } 
SList.prototype.traversal=function (){
  var p=this.head;
  while (p.next != null){
  document.write( p.next.data   "(" p.next.frequency "), ");
  p = p.next;
  }
 }

 SList.prototype.orderInsert =function(data) { 
 var k = this.search( data );
 if (k) k.frequency  ;
 else {
  var p = new Node(data); 
  var q = this.head; 
  while (q.next!=null && q.next.data<data) 
  q = q.next;
  p.next=q.next;
  q.next=p;
  } 
 }
 SList.prototype.search= function (data) { 
  var p = this.head; 
  while (p.data != data && p.next!=null) 
  p = p.next;
  if (p.data !=data)
  return null; 
  else
  return p; 
  } 

 var Slist = new SList();
 var s=new Array("earthquake","prediction","geology","physics",
 "chemistry","biology","mathematics","computer","earth_science",
 "chemistry","biology","mathematics","computer","paleomagnetism",
 "topology","biology","mathematics","computer","earthquake");
 for (var i=0; i<s.length; i  )
 Slist.orderInsert(s[i]);
 Slist.traversal(); 
</script>
</body>
</html>

本文由澳门威利斯人发布于澳门威利斯人,转载请注明出处:JS使用单链表统计英语单词出现次数,js环境下

关键词: 澳门威利斯人