链表

  链表是一种物理存储单元上非线性、非连续性的数据结构(它在数据逻辑上是线性的),它的每个节点由两个域组成:数据域和指针域。数据域中存储实际数据,指针域则存储着指针信息,指向链表中的下一个元素或者上一个元素。正是由于指针的存在,链表的存储在物理单元是非连续性的。

链表的优点和缺点同样明显。和线性表相比,链表在添加和删除节点上的效率更高,因为其只需要修改指针信息即可完成操作,而不像线性表(数组)那样需要移动元素。同样的,链表的长度在理论上也是无限的(在存储器容量范围内),并可以动态变化长度,相比线性表优势很大。 相应的,由于线性表无法随机访问节点,只能通过指针顺着链表进行遍历查询来访问,故其访问数据元素的效率比较低。

JS实现单链表

function LinkedList() {
let Node = function (ele) {
this.ele = ele;
this.next = null;
} let length = 0,
head = null; //头指针 //向尾部追加元素
this.append = function (ele) {
let node = new Node(ele),
temp; //临时指针 if (!head) {
head = node;
} else {
temp = head;
while (temp.next) {
temp = temp.next
}
temp.next = node;
}
length++;
return true;
} //插入到指定位置
this.insert = function (position, ele) {
if (position >= 0 && position < length) {
let node = new Node(ele),
temp = head,
index = 0,
previous; if (position == 0) {
node.next = temp;
head = node;
} else {
while (index++ < position) {
previous = temp;
temp = temp.next;
}
node.next = temp;
previous.next = node;
}
length++;
return true;
} else {
return false;
}
} //删除指定位置元素
this.removeAt = function (position) {
if (position > -1 && position < length) {
let temp = head,
previous,
index = 0; if (position == 0) {
head = head.next;
} else {
while (index++ < position) {
previous = temp;
temp = temp.next;
} previous.next = temp.next;
}
length--;
return temp.ele;
} else {
return null;
}
} //删除所有值为ele的元素
this.removeEle = function (ele) {
let temp = head,
previous,
num = 0;
if (ele == temp.ele) {
head = head.next;
length--;
num++;
} while (temp.next) {
previous = temp;
temp = temp.next;
if (temp.ele == ele) {
previous.next = temp.next;
length--;
num++;
}
}
return num;
} //删除最后一个元素
this.pop = function () {
let temp = head,
previous = temp;
if (length < 1) {
return false;
}
if (length == 1) {
head = null;
length--;
return temp.ele;
}
while (temp.next) {
previous = temp;
temp = temp.next;
}
previous.next = null;
length--;
return temp.ele;
} this.indexOf = function (ele) {
let temp = head,
index = 0; while (temp) {
if (temp.ele == ele) {
return index;
}
temp = temp.next;
index++;
}
return -1; } this.toString = function () {
let temp = head,
string = ''; while (temp) {
string += temp.ele + ' ';
temp = temp.next; }
return string;
}
this.length = function () {
return length;
}
this.isEmpty = function () {
return length === 0;
};
this.getHead = function () {
return head.ele;
}
} let mylist = new LinkedList();
mylist.append('A');
mylist.append('B');
mylist.append('C');
mylist.append('D');
mylist.append('C');
mylist.append('B');
mylist.append('A');
console.log(mylist.toString());
console.log(mylist.pop());
console.log(mylist.toString());
console.log('移除%d个C', mylist.removeEle('C'));
console.log(mylist.toString());
console.log(mylist.length());
console.log(mylist.getHead()); console.log(mylist.indexOf('C'))

JS实现单循环链表:

在单链表的基础上,将尾节点的指针指向头结点,就构成了一个循环链表。环形链表从任意一个节点开始,都可以遍历整个链表。

function CircularLinkedList(){
var Node = function(element){
this.element = element;
this.next = null;
} var length = 0,
head = null; this.append = function(element){
var node = new Node(element),
current; if (!head) {
head = node;
node.next = head;
}else{
current = head; while(current.next !== head){
current = current.next;
} current.next = node;
node.next = head;
}; length++;
return true;
}; this.insert = function(position, element){
if(position > -1 && position < length){
var node = new Node(element),
index = 0,
current = head,
previous; if (position === 0) { node.next = head;
head = node; }else{ while(index++ < position){
previous = current;
current = current.next;
} previous.next = node;
node.next = current; }; length++;
return true;
}else{
return false;
}
};
//移除指定位置元素
this.removeAt = function(position){
if(position > -1 && position < length){
var current = head,
previous,
index = 0; if (position === 0) { head = current.next; }else{ while (index++ < position){
previous = current;
current = current.next;
} previous.next = current.next;
}; length--;
return current.element;
}else{
return null;
}
};
//移除指定元素
this.remove = function (element){
var current = head,
previous,
indexCheck = 0; while(current && indexCheck < length){
if(current.element === element){
if(indexCheck == 0){
head = current.next;
length--;
return true;
}else{
previous.next = current.next;
length--;
return true;
}
}else{
previous = current;
current = current.next;
indexCheck++;
}
}
return false;
};
//移除最后一个元素
this.remove = function(){
if(length === 0){
return false;
} var current = head,
previous,
indexCheck = 0; if(length === 1){
head = null;
length--;
return current.element;
} while(indexCheck++ < length){
previous = current;
current = current.next;
}
previous.next = head;
length--;
return previous.element; //返回移除的元素
}; this.indexOf = function(element){
var current = head,
index = 0; while(current && index < length){
if(current.element === element){
return index;
}else{
index++;
current = current.next;
}
}
return false;
}; this.isEmpty = function(){
return length === 0;
}; this.size = function(){
return length;
}; this.toString = function(){
var current = head,
string = '',
indexCheck = 0; while(current && indexCheck < length){
string += current.element;
current = current.next;
indexCheck++;
} return string;
}; }
let mylist=new CircularLinkedList();
mylist.append('A');
mylist.append('B');
mylist.append('C');
mylist.append('D');
console.log(mylist.toString());
console.log(mylist.remove()); //D
console.log(mylist.toString());
 

JS实现单链表、单循环链表的更多相关文章

  1. 数据结构篇(2) ts实现单循环链表

    JS的class可以通过extends关键字实现类似其他语言的继承效果,比起使用一个extends关键字,在es5中实现继承要复杂一些,可以通过修改原型链的方法实现继承,让一个原型对象等于另一个类型的 ...

  2. (续)顺序表之单循环链表(C语言实现)

    单循环链表和单链表的唯一区别在于单循环链表的最后一个节点的指针域指向第一个节点, 使得整个链表形成一个环. C实现代码如下: #include<stdio.h> typedef struc ...

  3. 数据结构与算法之PHP实现链表类(单链表/双链表/循环链表)

    链表是由一组节点组成的集合.每个节点都使用一个对象的引用指向它的后继.指向另一个节点的引用叫做链表. 链表分为单链表.双链表.循环链表.   一.单链表 插入:链表中插入一个节点的效率很高.向链表中插 ...

  4. 数据结构学习--单循环链表(python)

    概念 将单链表的终端节点的指针由原来的空指针改为指向头节点, 就是整个单链表形成一个环, 这种首尾相接的单链表称为单循环链表. 实现 class Node: """ 节点 ...

  5. java数据结构-04单循环链表

    单循环链表与单链表的不同是,单循环链表尾结点的next指向第一个结点(或头结点)  代码: 无头结点: public class SingleCircleLinkedList<E> ext ...

  6. C# 数据结构 - 单链表 双链表 环形链表

    链表特点(单链表 双链表) 优点:插入和删除非常快.因为单链表只需要修改Next指向的节点,双链表只需要指向Next和Prev的节点就可以完成插入和删除操作. 缺点:当需要查找某一个节点的时候就需要一 ...

  7. 链表用途&&数组效率&&链表效率&&链表优缺点

    三大数据结构的实现方式 数据结构 实现方式 栈  数组/单链表 队列  数组/双端链表 优先级队列 数组/堆/有序链表 双端队列 双向链表 数组与链表实现方式的比较 数组与链表都很快 如果能精确预测栈 ...

  8. JS表单验证-12个常用的JS表单验证

    JS表单验证-12个常用的JS表单验证 最近有个项目用到了表单验证,小编在项目完结后的这段时间把常用的JS表单验证demo整理了一下,和大家一起分享~~~ 1. 长度限制 <p>1. 长度 ...

  9. js 表单验证控制代码大全

    js表单验证控制代码大全 关键字:js验证表单大全,用JS控制表单提交 ,javascript提交表单:目录:1:js 字符串长度限制.判断字符长度 .js限制输入.限制不能输入.textarea 长 ...

随机推荐

  1. leecode刷题(12)-- 整数反转

    leecode刷题(12)-- 整数反转 整数反转 描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: - ...

  2. MySql数据库,对varchar类型字段str进行where str=0条件查询时,查询结果是什么

    在用MySQL查询数据的时候,遇到了一个奇怪的问题.用一个varchar类型的字符串str,作为条件与0比较时,会查str不为0的数据. 比如:SELECT id, idnumber from hr_ ...

  3. [ActionScript 3.0] 亮度、对比度、饱和度、色相的调整

    import fl.motion.ColorMatrix; import flash.filters.ColorMatrixFilter; //**调整亮度**// var ld_Matrix:Col ...

  4. jxl操作excel单个单元格换行和获取换行

    excel中同表格换行: a+"\n"+b 1.读取 String str = sheet.getCell(c, r).getContents(); String[] split ...

  5. 浅谈Ionic2

    http://www.cnblogs.com/zhouming-web/p/6226323.html 前言: 不要用angular的语法去写angular2,有人说二者就像Java和JavaScrip ...

  6. c#中的classes和objects一些知识【1】

    首先我们需要知道面向对象语言(Object-oriented language)的三大特点:封装(Encapulation),继承(Inheritance),多态(Polymorphism). 引言: ...

  7. 解决mysql最大允许传输包不足的问题

    一.报错提示内容和原因 在执行“数据传输”或者“运行SQL文件”时报错误:Got a packet bigger than 'max_allowed_packet' bytes With,表明当前所传 ...

  8. [原创]Heroku简单部署指南

    目录 1. 设置 1.1 环境依赖 1.2 Heroku 客户端安装 1.3 登录 2. 应用 2.1 创建 2.2 查看日志 2.3 附加组件 2.4 交互式 Shell 2.5 定义 配置变量 2 ...

  9. ubuntu 16.04 安装opencv 2.4.13

    ubuntu 16.04 安装opencv 2.4.13 https://blog.csdn.net/u011557212/article/details/54706966?utm_source=it ...

  10. 分布式文件系统之FastDFS

    环境引入: 在一个大型的教育官网,会拥有大量优质的视频教程,并且免费提供给用户去下载,文件太多如果高效存储?用户访问量大如何保证下载速度?分布式文件系统是解决这些问题的有效方法之一 一.什么是文件系统 ...