本文实例为大家分享了JS 列出所有单词及其出现次数的实现代码,JS统计英语单词出现次数,可以调用LinkedList 类的方法orderInsert(), 以字母大小的顺序储存 英文字符串,同时记录英文单词出现的次数,供大家参考,具体内容如下

<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统计单词出现次数,js列出所有单词,js统计次数,js单链表统计

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“JS使用单链表统计英语单词出现次数”

暂无“JS使用单链表统计英语单词出现次数”评论...