链表

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

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

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. 【Apache系列】Windows下作为应用程序运行Apache

    步骤一 Cmd打开命令行窗口,切换到apache安装目录下 cd  C:\MAS\TRSMAS\win31\apache\bin 步骤二 安装apache服务器 installed Apache se ...

  2. 927. Three Equal Parts

    Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these parts ...

  3. OCP2018最新题库,052新题库及答案整理-25题

    25.Which is true about logical and physical database structures? (Choose the best answer) A. An undo ...

  4. LoadRunner12_脚本中运行JavaScript

    版权声明:本文为博主原创文章,未经博主允许不得转载. [系统及软件配置] LR版本:12.53 JDK版本:1.8 函数:web_js_run,该函数仅在LR12版本提供支持,LR11不支持JavaS ...

  5. ArchLinux 下文件描述符

    stderr -> /proc/self/fd/2 标准错误:2 stdin -> /proc/self/fd/0 标准输入:0 stdout -> /proc/self/fd/1 ...

  6. jqury动画,循环

    一.动画 效果就是定义一个小盒子,让这个小盒子以动画的形式变化尺寸, <!DOCTYPE html> <html lang="en"> <head&g ...

  7. Chart.js入门教程

    Chart.js是一个简单.面向对象.为设计者和开发者准备的图表绘制工具库. 相信大部分人都一样,看到一大筐用文本或者表格形式呈现的数据就头疼.因为这种呈现方式也太无聊了吧...而且这对于我们处理原始 ...

  8. Python导模块问题

    我们在import一个模块的时候,有没有想过如果我重复的import会怎么样?导入时都做了些什么?让我们来看看 1.首先我们新建一个demo,py,里面只有一句输出语句 2.多次导入demo,运行之后 ...

  9. ipython 导入模块 非法ascii字符问题

    SyntaxError: Non-ASCII character '\xe2‘ ipython  使用了 python2    加入 #coding=utf-8 ipython3 使用 python3

  10. css加载顺序

    最近发现个有意思的事情,印象中的是css中class后面会覆盖前面的, 于是写了代码 div{ width: 100px; height: 100px; } .red{ background-colo ...