使用JavaScript实现单向链表
一、实现功能
1、链表元素头部插入 this.unShift = function(data) {}
2、链表元素尾部插入 this.append= function(data) {} //返回boolean
3、链表元素按位置插入 this.insert= function(position, data) {} //返回boolean
4、链表元素头部删除 this.shift= function() {} //返回boolean
5、链表元素尾部删除 this.pop = function() {} //返回boolean
6、链表按元素删除 this.removeData = function(data) {} //返回boolean
7、链表按下标删除 this.removePos = function(position) {} //返回boolean
8、链表是否为空 this.isEmpty= function() {} //返回boolean
9、链表元素组成的字符串 this.toString= function() {} //返回 string
10、链表的长度 this.length= function() {} //返回number
11、链表查找元素 this.searchData= function(data) {} //返回obj={index:,data:}
二、节点声明
function Node(data) {
this.data = data //数据
this.next = null //指向
}
三、完整功能代码
function List() {
let length = 0
let head = null
this.append = (data) => { // 向链表末尾添加元素
let node = new Node(data)
let current
if(head === null) {
head = node
}
else {
current = head
while(current.next) {
current = current.next
}
current.next = node
}
length ++
return true
}
this.insert = (position, data) => { // 向链表指定位置添加元素
if (position >= 0 && position <= length) { //检查是否越界
let node = new Node(data),
current = head,
previous,
index = 0
if (position === 0) { //在第一个位置添加
node.next = current
head = node
}else {
while (index ++ < position) {
previous = current
current = current.next
}
//通过改变指针,将node链接在previous和current之间
node.next = current
previous.next = node
}
length ++
return true
}else {
return false
}
}
this.removeData = (data) => { // 通过数据删除链表元素
let previous,
current,
index = 0
if (head === null) {
return false
}
current = head
while(index ++ < length && current.data !== data) {
previous = current
current = current.next
}
if(index > length) {
return false
}else {
current.next = current.next
length --
return true
}
}
this.removePos = (position) => { // 通过位置删除链表元素
if( position >= -1 && position < length) {
let previous,
current = head,
index = 0
if (position === 0) {
head = current.next
}else {
while(index ++ < position) {
previous = current
current = current.next
}
previous.next = current.next
}
length --
return true
}else {
return false
}
}
this.unShift = (data) => { //向链表首插入元素
let node = new Node(data),
current = head
node.next = current
head = node
length ++
return true
}
this.shift = () => { //删除链表首元素
let current = head
if(head === null) {
return false
}
else {
head = current.next
length --
return true
}
}
this.pop = () => { // 删除链表尾元素
let current,
previous
if (head === null){
return false
}
current = head
while(current) {
previous = current
current = current.next
}
previous = null
length --
return true
}
this.isEmpty = () => {
return length === 0
}
this.length = () => {
return length
}
this.searchData = (data) => { //查找元素
let current,
index = 0,
obj = {}
if( head === null ) {
return false
}
current = head
while(index ++ < length && current.data !== data) {
current = current.next
}
obj = {
index: index,
data: current.data
}
return obj
}
this.toString = () => { //输出数据组成的字符串
let resultStr = "",
current,
index = length
if (head === null) {
return ""
}
current = head
while(index -- > 0) {
resultStr += "," + current.data
current = current.next
}
return resultStr.slice(1)
}
}
四、测试代码及结果
1、测试代码
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script type="text/javascript" src="./singleList.js"></script>
</head>
<body>
<script type="text/javascript">
let singleList = new List()
console.log(singleList.append("1"))
console.log(singleList.toString())
console.log(singleList.append("2"))
console.log(singleList.toString())
console.log(singleList.append("3"))
console.log(singleList.toString())
console.log(singleList.unShift(0))
console.log(singleList.toString())
console.log(singleList.insert(2,"4"))
console.log(singleList.toString())
console.log(singleList.shift())
console.log(singleList.toString())
console.log(singleList.pop())
console.log(singleList.toString())
console.log(singleList.searchData("1"))
console.log(singleList.removePos(1))
console.log(singleList.toString())
console.log(singleList.removeData("1"))
console.log(singleList.toString())
console.log(singleList.isEmpty())
</script>
</body>
</html>
2、测试结果

使用JavaScript实现单向链表的更多相关文章
- JavaScript实现单向链表
JavaScript 本身提供了十分好用的数据类型,以满足大家的日常使用.单靠 Array 和 Object 也的确足够应付日常的绝大部分需求,这也导致了很多前端er对数据结构这一块不是十分的了解. ...
- JavaScript实现单向链表结构
参考资料 一.什么是链表结构? 1.1.简介 链表和数组一样, 可以用于存储一系列的元素, 但是链表和数组的实现机制完全不同,链表中的元素在内存不是连续的空间,链表的每个元素由一个存储元素本身(数据) ...
- 学习javascript数据结构(二)——链表
前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...
- Reverse Linked List II 单向链表逆序(部分逆序)
0 问题描述 原题点击这里. 将单向链表第m个位置到第n个位置倒序连接.例如, 原链表:1->2->3->4->5, m=2, n =4 新链表:1->4->3-& ...
- 【编程题目】输入一个单向链表,输出该链表中倒数第 k 个结点
第 13 题(链表):题目:输入一个单向链表,输出该链表中倒数第 k 个结点.链表的倒数第 0 个结点为链表的尾指针.链表结点定义如下: struct ListNode {int m_nKey;Lis ...
- 输出单向链表中倒数第k个结点
描述 输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第0个结点为链表的尾指针. 链表结点定义如下: struct ListNode { int m_nKey; ListNode* ...
- Linus:利用二级指针删除单向链表
Linus大神在slashdot上回答一些编程爱好者的提问,其中一个人问他什么样的代码是他所喜好的,大婶表述了自己一些观点之后,举了一个指针的例子,解释了什么才是core low-level codi ...
- 【转】Linus:利用二级指针删除单向链表
原文作者:陈皓 原文链接:http://coolshell.cn/articles/8990.html 感谢网友full_of_bull投递此文(注:此文最初发表在这个这里,我对原文后半段修改了许多, ...
- C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)
#include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student / ...
随机推荐
- kvm虚拟化1
计算机的五大组成部分: 运算器,控制器,存储器,输入,输出 虚拟化是对cpu ,内存,,磁盘, 网络,IO 的虚拟 cpu的虚拟 以时间分片形式进行,这样使得cpu可以运行多个进程 内存进行了空间 ...
- 手动部署 kubernetes HA 集群
前言 关于kubernetes HA集群部署的方式有很多种(这里的HA指的是master apiserver的高可用),比如通过keepalived vip漂移的方式.haproxy/nginx负载均 ...
- 反素数ant HYSBZ - 1053(数学+dfs)
对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4.如果某个正整数x满足:g(x)>g(i) 0<i<x ,则称x为反质数.例如,整数1,2,4,6等都是反质 ...
- 2019-2-14SQLserver中function函数和存储过程、触发器、CURSOR
Sqlserver 自定义函数 Function使用介绍 前言: 在SQL server中不仅可以可以使用系统自带的函数(时间函数.聚合函数.字符串函数等等),还可以根据需要自定义函数 ...
- [CQOI2015]网络吞吐量
Description: 给你一个图,每个点可以被经过\(a_i\)次,求有多少个人可以走最短路到n点 Hint: \(n \le 500\) Solution: 极其水的一道题,就当做复习最短路板子 ...
- Do-Now—团队Scrum 冲刺博客二
各个成员今日完成的任务 侯泽洋:完成奖励页面设计,完成奖励从云端拉取到本地:完成奖励从云端拉取到本地 周亚杰:完成个人中心页面设计,登录界面美化:注册登录界面美化 王志伟:完成倒计时功能,并对页面进行 ...
- Telephone Lines [POJ3662] [二分答案]
Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...
- linux(debian) arm-linux-g++ v4.5.1交叉编译 embedded arm 版本的QtWebkit (browser) 使用qt 4.8.6 版本 以及x64上编译qt
最近需要做一个项目 在arm 架构的linux下 没有桌面环境的情况下拉起 有界面的浏览器使用. 考虑用qt 的界面和 qtwebikt 的库去实现这一系列操作. 本文参考: Qt移植到ARM Lin ...
- CMake入门
CMake入门 CMake是一个跨平台的安装编译工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似 ...
- node.js官方文档解析 02—buffer 缓冲器
Buffer 类的实例类似于整数数组,但 Buffer 的大小是固定的.且在 V8 堆外分配物理内存.Buffer 的大小在被创建时确定,且无法调整. Buffer 类在 Node.js 中是一个全局 ...