【数据结构与算法】详解什么是链表,并用代码手动实现一个链表结构

本文未来讲解一下一种常见的线性数据结构—链表,由于链表和数组同样都是一种线性的数据结构,可是它俩的实现原理是彻底不一样的,因此在讲解链表以前,咱们来回顾一下数组结构。javascript

数组 几乎是每一种编程语言中都自带的一种经常使用的数据存储结构,咱们能够很方便的经过下标值来获取到咱们想要的数组中的元素。前端

可是数组也仍是有很大的缺点的,例如如今有一个长度为10的数组,那么数组中的每一个元素都对应着各自的下标值,范围为 0~9 ,如今咱们要往下标值为 2 的位置插入一个新的元素,咱们须要作的就是先把数组的长度变成11,而后将数组中本来下标值为 2 以及以后的全部元素都向后移动一个位置,即下标值 +1,最后再将咱们要插入的新元素放到下标值为 2 的位置。java

一样的,若是咱们要删除数组中的某个元素,也是同样的道理,先删除要删除的元素,而后把被删除元素后面的全部元素往前移动一个位置,即下标值 -1,最后把数组的长度 -1node

由上面两个例子能够看出,数组的 添加元素删除元素 很是消耗性能,可谓牵一发而动全身,这就是数组最大的缺点,因此 链表 的出现就弥补了数组的这一缺点。本文就来详细讲解一下链表的概念以及如何实现一个链表。python

公众号:Lpyexplore的编程小屋
关注我,天天更新,带你在python爬虫的过程当中学习前端,还有更多电子书和面试题等你来拿c++

在这里插入图片描述

1、什么是链表

上面刚提到,链表 弥补了数组的一些缺点,那么是如何作到的呢?链表究竟是什么样的呢?接下来咱们就用一个简单的例子来解释一下链表的概念web

假设在一个教室中,全部学生的座位都排成一列,这一列就能够当作是一个 链表,如图所示
在这里插入图片描述
每一个座位上坐着的学生就至关于链表中的一个 元素 。假如老师记性很差,学生记性也很差,因此他们就很难记住本身的座位究竟是哪个。所以老师想了一个好办法,老师就只记住座位的第一个是谁,而后每个学生都只要记住本身的后桌是谁就行了。面试

每次刚进教室准备找座位坐下,老师就告诉学生们说,小5 是坐在第一个的,而后 小5 记得他的后桌是 小1,因而 小1 也找到了座位坐下,由于 小1 记得他的后桌是 小8,因此 小8 也找到本身的座位了……以此类推,一直到坐在最后一个的 小2,就这样你们就均可以找到本身的座位了,如图:
在这里插入图片描述
有一天, 小8 同窗要转学了,因此座位有变更,为了保证座位不空着,小8 走了之后,把他的座位也搬走,以避免占用教室空间。此时要让其他同窗仍然能正确找到本身的座位的方式就很简单,那就是让本来 小8 的前桌记住,他如今的后桌是 小6了,再也不是 小8 了,因此如今的状况如图所示:
在这里插入图片描述
像这样的记忆本身座位的方式很是的好,由于一个同窗转学,只须要让一我的从新记忆本身的后桌是谁便可,而不须要让全部的同窗从新记忆。算法

同理,好比忽然有一天,有一个新同窗 小7 到了这个班级,老师安排她坐在 小5 后面,此时 小5 就须要记住,他的后桌换成了 小7,而这个新来的同窗 小7 也像其余同窗同样要记住本身的后桌为 小1,此时的状况是这样的,如图:
在这里插入图片描述
一样的,新来一个同窗形成的位置变更,只让一个同窗从新记忆本身的后桌是谁,还同时让新来的同窗记住本身的后桌是谁,而没有让全部的同窗所有从新记忆,这无疑是一个很是高效的方式。编程

例子讲完了,接下来进入正题,链表 的实现就像咱们刚才讲的那个例子。在链表中,每个元素都包含两个属性,即 该元素的值item下一个元素next,其中,item 就像咱们刚才例子中的同窗;next 就像同窗记住的他们的后桌是谁。同时链表中有一个指针 head 指向链表第一个元素,这个 head 就比如老师记住的坐在第一个的是谁。

接下来咱们来看一下链表的结构
在这里插入图片描述
这总体看上去就像一个链子,因此叫作链表,每个 itemnext 组合起来称做是一个元素,再补充一下,最后一个元素指向 null,就至关于例子中坐在最后一个的同窗说他没有后桌同样。

此时咱们要删除第二个元素,链表结构就会变成这个样子
在这里插入图片描述
你们能够根据这个结构比对一下刚刚我举得例子,好好理解一下链表的结构形式和实现原理。

2、链表的方法

由于链表是弥补数组缺点而造成的一种数据结构,因此链表也有像数组同样多的方法,这里咱们就来看一下链表都有哪些方法吧

方法 含义
append() 向链表尾部追加元素
insert() 在链表的某个位置插入元素
get() 获取链表对应位置的元素
indexOf() 获取某元素在链表中的索引
update() 修改链表中某个位置上的元素的值
removeAt() 移除链表中某位置上的某元素
remove() 移除链表中的某元素
isEmpty() 判断链表内是否为空
size() 返回链表内元素个数
toString() 以字符串的形式展现链表内的全部元素

3、用代码实现链表

根据上面对链表的讲解,咱们知道,咱们不管是要删除元素仍是添加元素,每次都是要从 head 开始,依次日后查找元素,因此对于链表的每个方法,咱们每次作的就是从 head 开始遍历,并经过 next 找到每个元素的下一个元素是谁。

(1)建立一个构造函数

首先建立一个大的构造函数,用于存放链表的一些属性和方法。

function LinkedList() {
    //属性
    this.head = null
    this.length = 0
}

其中定义了属性 head,并赋值了一个 null,表示如今链表内没有元素;而属性 length则是为了储存链表内的元素个数,这是为了提升链表一些方法的性能,例如以后要写的 size()方法,咱们每次调用该方法时,都要遍历整个链表,而后统计元素个数,那么还不若有一个属性记录着元素个数,只须要在添加元素的方法里给 length + 1,在删除元素的方法里给 length - 1

(2)建立内部构造函数

链表的每个元素都有两个属性,即 itemnext,分别表示存储着该元素的值和该元素的后一个元素是谁。因此咱们就在链表的构造函数内部建立一个内部构造函数用于以后建立元素的实例对象

function LinkedList() {
    //属性
    this.head = null
    this.length = 0

    //每一个元素的定义类
    function Node(item) {
        this.item = item
        this.next = null
    }
}

以后要若是要添加新元素,咱们只须要 new Node(item),并把元素的值传入就能够建立一个元素的实例对象了,这里默认是给每一个新建立的元素实例的 next 属性设置为 null,是由于刚开始咱们并不知道它被添加到链表里之后,后一个元素是谁,固然,咱们只须要在知道了后一个元素是谁的状况下设置一下这个属性便可。

(3)实现append()方法

append()方法就是将元素添加到链表的最后一个。

实现思路:

  1. 建立新的元素实例对象 node
  2. 判断 length 是否为0,若为0,则直接将 head 指向 node
  3. length 不为0,则根据每一个元素的 next 属性遍历链表
  4. 若元素的 next 的值不等于 null,继续遍历
  5. 若元素的 next 的值等于 null,则表示已经查找到链表的最后一个元素,因此直接将该元素的 next 值设置成 node 便可
  6. 属性 length + 1

为了方便大家理解这个实现思路,我作了个动图展现一下思路
在这里插入图片描述
在这里插入图片描述
思路讲完了,咱们直接来看代码

function LinkedList() {
    //属性
    this.head = null
    this.length = 0

    //每一个元素的定义类
    function Node(item) {
        this.item = item
        this.next = null
    }
	
	//在链表尾部追加元素
	LinkedList.prototype.append = function (item) {
		// 1.建立新的元素实例对象
        let node = new Node(item)
        
        // 2.判断链表是否为空
        if(this.length === 0) {
            this.head = node
        }
        else {
            let current = this.head
            // 3.遍历链表,找到最后一个元素
            while(current.next) {
                current = current.next
            }
            // 4.将咱们要添加的元素赋值给本来链表中的最后一个元素
            current.next = node
        }
		
		//链表元素+1
        this.length ++
    }
}

咱们来使用一下该方法

刚开始的链表是这个样子的
在这里插入图片描述
而后咱们使用一下 append()方法

let linkedlist = new LinkedList()

linkedlist.append('javascript')
linkedlist.append('python')

此时的链表是这样的
在这里插入图片描述

(4)实现insert()方法

insert()方法就是在指定的索引位置插入元素。一共须要传入两个参数,第一个是 position,表示须要插入元素的位置;第二个参数是 item,表示元素的值

实现思路:

  1. 建立新的元素实例对象 node
  2. 判断指定的索引位置 position 是否越界,便是否小于0,或者大于链表的长度。若越界了,则直接返回false
  3. 判断 position 是否为0。若为0,则直接将链表本来的第一个元素,也就是 head所对应的元素赋值给 nodenext属性,而后将 node赋值给 head,表示如今链表的第一个元素为 node
  4. position 不为0,则遍历链表,同时记录遍历的索引 index 和遍历的上一个元素 prev,当 index == position时,将链表在 index索引位置上的元素赋值给 nodenext属性,再将 node赋值给 prevnext属性
  5. 属性 length + 1

为了方便大家理解这个实现思路,我作了个动图展现一下思路
在这里插入图片描述
在这里插入图片描述
思路讲完了,咱们直接来看代码

function LinkedList() {
    //属性
    this.head = null
    this.length = 0

    //每一个元素的定义类
    function Node(item) {
        this.item = item
        this.next = null
    }
	
	//在某个位置插入新的元素
    LinkedList.prototype.insert = function (position, item) {
    	// 1.建立新的元素实例对象
        let node = new Node(item)
        
		// 2.判断是否越界
        if(position < 0 || position > this.length) return false
        
        // 3.判断插入的位置是否为 0
        if(position === 0) {
            node.next = this.head
            this.head = node
        }
        else {
        	// 4.遍历链表,找到索引等于position的元素
            let current = this.head
            let prev = null
            let index = 0
            while (index++ < position) {
                prev = current
                current = current.next
            }
    		// 5.插入元素
            prev.next = node
            node.next = current
    
        }
        // 6.链表元素 +1
        this.length ++
        return true
        
    }
}

咱们来使用一下该方法

let linkedlist = new LinkedList()

linkedlist.append('javascript')
linkedlist.append('python')
linkedlist.append('java')

linkedlist.insert(1, 'c++')

此时的链表是这样的
在这里插入图片描述

(5)实现get()方法

get()方法就是获取对应位置上的元素。须要传入一个参数,即 position,表示须要获取元素的索引

实现思路:

  1. 判断 position 是否越界。若越界,则直接返回false
  2. 遍历链表,同时记录当前索引 index,当 index == position时,返回当前位置上的元素

接下来咱们来单独实现一下该方法

function LinkedList() {
    //属性
    this.head = null
    this.length = 0

    //每一个元素的定义类
    function Node(item) {
        this.item = item
        this.next = null
    }
	
	//获取对应位置的元素
    LinkedList.prototype.get = function (position) {
    	// 1.判断是否越界
        if(position < 0 || position >= this.length) return false
        
	        let current = this.head
	        let index = 0
	        // 2.遍历链表,直到 index == position
	        while (index++ < position) {
	            current = current.next
	        }
	        // 3.返回当前索引位置的元素
	        return current.item
        
    }
}

咱们来使用一下该方法

let linkedlist = new LinkedList()

linkedlist.append('javascript')
linkedlist.append('python')
linkedlist.append('java')

linkedlist.get(2)            //返回 java

(6)实现indexOf()方法

indexOf()方法就跟数组的同样,获取某元素在链表中的索引值,若链表中不存在该元素,则返回 -1

由于比较简单,这里就不讲解思路了,咱们直接用代码来实现这个方法

function LinkedList() {
    //属性
    this.head = null
    this.length = 0

    //每一个元素的定义类
    function Node(item) {
        this.item = item
        this.next = null
    }
	
	//获取元素的索引
    LinkedList.prototype.indexOf = function (item) {
        let current = this.head
        let index = 0
        while (index < this.length) {
            if(current.item === item) {
                return index
            }
            else {
                current = current.next
                index ++
            }
        }
        return -1
    }
}

咱们来使用一下该方法

let linkedlist = new LinkedList()

linkedlist.append('javascript')
linkedlist.append('python')
linkedlist.append('java')

linkedlist.indexOf('python')    //返回 1
linkedlist.indexOf('c++')       //返回 -1

(7)实现update()方法

update()方法就是用于修改链表中某位置上的元素的值。所以该方法须要传入两个参数,第一个参数是 position,表示须要修改的元素的索引;第二个参数是 NewItem,表示修改后的值

这里就简单讲下思路吧,首先要先判断 position 是否越界,若越界直接返回 false表示修改失败,若没有越界就遍历链表,同时记录当前索引 index,当 index == position时,就将当前索引位置上的元素的值 item修改为 NewItem

接下来咱们来单独实现一下该方法

function LinkedList() {
    //属性
    this.head = null
    this.length = 0

    //每一个元素的定义类
    function Node(item) {
        this.item = item
        this.next = null
    }
	
	//修改某位置上的元素
    LinkedList.prototype.update = function (position, NewItem) {
    	// 1.判断是否越界
        if(position < 0 || position >= this.length) return false
        else {
            let current = this.head
            let index = 0
            
            // 2.遍历链表,找到索引等于position的元素对象
            while (index < position) {
                current = current.next
                index ++
            }
            
            // 3.将索引等于position的元素的值改成NewItem
            current.item = NewItem
            return true
        }
    }
}

咱们来使用一下该方法

let linkedlist = new LinkedList()

linkedlist.append('javascript')
linkedlist.append('python')
linkedlist.append('java')

linkedlist.update(2, 'c++')

此时的链表是这样的
在这里插入图片描述

(8)实现removeAt()方法

removeAt()方法就是用于移除链表中某位置上的某元素。该方法只须要传入一个参数 position,表示须要移除元素的索引

实现思路:

  1. 判断 position 是否越界,若越界,则直接返回 false 表示移除元素失败
  2. 若没有越界,判断 position 是否等于 0,若等于 0,则直接将链表第一个元素的 next 值赋值给 head,而后 length - 1
  3. position 不等于 0,则遍历链表,同时记录当前索引 index,遍历的当前元素 currentcurrent的上一个元素 prev
  4. index === position时,则将 currentnext 值赋值给 prevnext 值便可,同时 length - 1

为了让你们更好地理解该方法的实现思路,我制做了一个动图来帮助你们理解,如图
在这里插入图片描述
在这里插入图片描述
思路讲完了,咱们直接来看代码

function LinkedList() {
    //属性
    this.head = null
    this.length = 0

    //每一个元素的定义类
    function Node(item) {
        this.item = item
        this.next = null
    }
	
	//移除某位置上的元素
    LinkedList.prototype.removeAt = function (position) {
    	// 1.判断是否越界
        if(position < 0 || position >= this.length) return false
        
        let current = this.head
        let prev = null
        let index = 0
        
        // 2.判断position是否等于 0
        if(position === 0) {
            this.head = current.next
        }
        else {
        	// 3.遍历链表
            while (index < position) {
                prev = current
                current = current.next
                index ++
            }
            // 4.移除对应元素
            prev.next = current.next
        }
		
		// 5.链表元素 -1
        this.length --
        return true
        
    }
}

咱们来使用一下该方法

let linkedlist = new LinkedList()

linkedlist.append('javascript')
linkedlist.append('python')
linkedlist.append('java')

linkedlist.removeAt(2)    //返回 true
linkedlist.removeAt(3)    //返回 false,表示删除失败

此时的链表是这样的
在这里插入图片描述

(9)实现remove()方法

remove()方法就是用于移除链表中的某元素,并返回被删除元素所在的索引位置,若链表中没有对应元素,则返回 false 。该方法须要传入一个参数 data用于查找链表中对应的元素

实现思路:

  1. 利用上面封装的 indexOf()方法,将 data 做为参数传入,获取到 data 在链表中的索引 index
  2. 再利用上面封装的 removeAt()方法,将 index 做为参数传入,就能够实现 remove()方法的功能了。

咱们简单来写一下代码实现该方法

function LinkedList() {
    //属性
    this.head = null
    this.length = 0

    //每一个元素的定义类
    function Node(item) {
        this.item = item
        this.next = null
    }
	
	//移除某元素
    LinkedList.prototype.remove = function (data) {
    	// 1.获取data在链表中的索引
        let index = this.indexOf(data)
		
		// 2.利用removeAt()方法删除链表中的data
        this.removeAt(index)
		
		// 3.返回被删除元素data在链表中的索引
		return index
    }
}

咱们来使用一下该方法

let linkedlist = new LinkedList()

linkedlist.append('javascript')
linkedlist.append('python')
linkedlist.append('java')

linkedlist.remove('javascript')    //返回 0

此时链表是这个样子的
在这里插入图片描述

(10)实现isEmpty()方法

isEmpty()方法就是判断链表中是否有元素。如有元素,则返回 false;反之,返回 true

该方法的实现思路很简单,直接判断属性 length 是否等于 0 就能够了。

话很少说,咱们赶忙写代码实现一下该方法

function LinkedList() {
    //属性
    this.head = null
    this.length = 0

    //每一个元素的定义类
    function Node(item) {
        this.item = item
        this.next = null
    }
	
	//判断链表是否为空
    LinkedList.prototype.isEmpty = function () {
        if(this.length === 0) {
            return true
        }
        else {
            return false
        }
    }
}

咱们来使用一下该方法

let linkedlist = new LinkedList()

linkedlist.isEmpty()          //返回 true,此时链表中无元素

linkedlist.append('javascript')
linkedlist.append('python')
linkedlist.append('java')

linkedlist.inEmpty()         //返回 false,此时链表中有三个元素

(11)实现size()方法

szie()方法就是返回链表内的元素个数

咱们来实现一下该方法

function LinkedList() {
    //属性
    this.head = null
    this.length = 0

    //每一个元素的定义类
    function Node(item) {
        this.item = item
        this.next = null
    }
	
	//返回链表的元素个数
    LinkedList.prototype.size = function () {
        return this.length
    }
}

咱们来使用一下该方法

let linkedlist = new LinkedList()

linkedlist.size()          //返回 0,此时链表中无元素

linkedlist.append('javascript')
linkedlist.append('python')
linkedlist.append('java')

linkedlist.size()         //返回 3,此时链表中有三个元素

(12)实现toString()方法

toString()方法就是以字符串的形式展现链表内的全部元素

实现思路很简单,就是遍历链表中的每个元素,并将每一个元素以字符串的形式链接起来便可

咱们来实现一下该方法

function LinkedList() {
    //属性
    this.head = null
    this.length = 0

    //每一个元素的定义类
    function Node(item) {
        this.item = item
        this.next = null
    }
	
	//展现整个链表
    LinkedList.prototype.toString = function () {
        let string = ''
        let current = this.head
        while (current) {
            string += `${current.item} `
            current = current.next
        }
        return string
    }
}

咱们来使用一下该方法

let linkedlist = new LinkedList()

linkedlist.append('javascript')
linkedlist.append('python')
linkedlist.append('java')

linkedlist.toString()           //返回 javascript python java

(13)完整的代码

function LinkedList() {
    //属性
    this.head = null
    this.length = 0

    //每一个元素的定义类
    function Node(item) {
        this.item = item
        this.next = null
    }

    //方法
    //在链表尾部追加元素
    LinkedList.prototype.append = function (item) {
        let node = new Node(item)
        if(this.length === 0) {
            this.head = node
        }
        else {
            let current = this.head
            while(current.next) {
                current = current.next
            }
            current.next = node
        }

        this.length ++
    }

    //展现整个链表
    LinkedList.prototype.toString = function () {
        let string = ''
        let current = this.head
        while (current) {
            string += `${current.item} `
            current = current.next
        }
        return string
    }

    //在某个位置插入新的元素
    LinkedList.prototype.insert = function (position, item) {
        let node = new Node(item)

        if(position < 0 || position > this.length) return false

        if(position === 0) {
            node.next = this.head
            this.head = node
        }
        else {
            let current = this.head
            let prev = null
            let index = 0
            while (index++ < position) {
                prev = current
                current = current.next
            }

            prev.next = node
            node.next = current

        }
        this.length ++
        return true

    }

    //获取对应位置的元素
    LinkedList.prototype.get = function (position) {
        if(position < 0 || position >= this.length) return false

        let current = this.head
        let index = 0
        while (index++ < position) {
            current = current.next
        }
        return current.item


    }

    //获取元素的索引
    LinkedList.prototype.indexOf = function (item) {
        let current = this.head
        let index = 0
        while (index < this.length) {
            if(current.item === item) {
                return index
            }
            else {
                current = current.next
                index ++
            }
        }
        return -1
    }

    //修改某位置上的元素
    LinkedList.prototype.update = function (position, NewItem) {
        if(position < 0 || position >= this.length) return false
        else {
            let current = this.head
            let index = 0
            while (index < position) {
                current = current.next
                index ++
            }
            current.item = NewItem
            return true
        }
    }

    //移除某位置上的元素
    LinkedList.prototype.removeAt = function (position) {
        if(position < 0 || position >= this.length) return false

        let current = this.head
        let prev = null
        let index = 0
        if(position === 0) {
            this.head = current.next
        }
        else {
            while (index < position) {
                prev = current
                current = current.next
                index ++
            }
            prev.next = current.next
        }

        this.length --
        return true

    }

    //移除某元素
    LinkedList.prototype.remove = function (data) {
        let index = this.indexOf(data)

        this.removeAt(index)
        return index
    }

    //判断链表是否为空
    LinkedList.prototype.isEmpty = function () {
        if(this.length === 0) {
            return true
        }
        else {
            return false
        }
    }

    //返回链表的元素个数
    LinkedList.prototype.size = function () {
        return this.length
    }
}

4、总结

链表结构的讲解就到这里了,但愿你们对链表有了更深一层的理解。下一篇文章我将讲解一下另外一种链表结构,叫作 双向链表

你们能够关注我,以后我还会一直更新别的数据结构与算法的文章来供你们学习,而且我会把这些文章放到【数据结构与算法】这个专栏里,供你们学习使用。

而后你们能够关注一下个人微信公众号:Lpyexplore的编程小屋,等这个专栏的文章完结之后,我会把每种数据结构和算法的笔记放到公众号上,你们能够去那获取。

我是Lpyexplore,创做不易,喜欢的加个关注,点个收藏,给个赞~ 带大家在Python爬虫的过程当中学习Web前端