06-单链表

概念图

image.png
  • 单链表是由一些具体的结点组成
  • 每个结点都是一个对象,有直自己的标识
  • 结点之间通过指针单项链接而成

表示链表结束,用结点的指针域指向空

表头添加数据

首先将新结点指向第一个结点,在将头指针指向新结点

image.png

一般情况添加数据

将新结点指针区域指向当前结点的next区域,将当前结点的next区域指向新节点


image.png

删除结点

image.png

python实现单链表

单链表结点构造

class Node:
    """
    单链表结点
    """
    def __init__(self, value):
        self.data = value
        self.next = None

单链表具体实现

class SingleLinkList:
    def __init__(self):
        self.__head = None

    def is_empty(self):
        """
        判断链表是否为空
        :return:
        """
        return self.__head is None

    def length(self):
        """计算链表的长度"""
        cur = self.__head
        i = 0
        while cur is not None:
            cur = cur.next
            i += 1
        return i

    def append(self, value):
        """在链表尾部添加"""
        node = Node(value)
        if self.is_empty():
            self.__head = node
        else:
            cur = self.__head
            while cur.next is not None:
                cur = cur.next
            cur.next = node

    def add(self, value):
        """在链表头部添加数据"""
        node = Node(value)
        if self.is_empty():
            self.__head = node
        else:
            cur = self.__head
            node.next = cur
            self.__head = node

    def insert(self, index, value):
        """指定位置插入数据"""
        node = Node(value)
        cur = self.__head
        if index <= 0 or cur is None:
            self.add(value)
        elif index > self.length() - 1:
            self.append(value)
        else:
            count = 0
            while cur.next is not None and count < index - 1:
                count += 1
                cur = cur.next
            node.next = cur.next
            cur.next = node

    def pop(self, index=0):
        """指定位置删除,默认删除第一个元素"""
        if not self.is_empty():
            if index == 0:
                cur = self.__head
                self.__head = cur.next
                return cur.data
            else:
                cur = self.__head
                pre = None
                count = 0
                if index > self.length()-1:
                    raise IndexError("索引超出范围")
                while cur.next is not None and count != index:
                    count += 1
                    pre = cur
                    cur = cur.next
                pre.next = cur.next
                return cur.data

    def remove(self, value):
        """指定数据第一个删除"""
        cur = self.__head
        pre = None
        while cur is not None:
            if cur.data == value:
                if not pre:
                    self.__head = cur.next
                else:
                    pre.next = cur.next
                break
            else:
                pre = cur
                cur = cur.next

    def search(self, value):
        """查询数据是否在链表中,返回第一个数据的索引, 没有则返回 -1"""
        if self.is_empty():
            return -1
        else:
            cur = self.__head
            count = 0
            while cur is not None:
                if cur.data == value:
                    return count
                count += 1
                cur = cur.next
            return -1

    def __repr__(self):
        data = []
        cur = self.__head
        while cur is not None:
            data.append(str(cur.data))
            cur = cur.next
        return "->".join(data)


if __name__ == '__main__':
    link_list = SingleLinkList()
    link_list.append(10)
    link_list.append(1)
    link_list.append(0)
    link_list.add(2)
    link_list.insert(2, 16)
    print(link_list.length())
    print(link_list)
    print(link_list.search(3))


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 目录 1、属性 2、链表和数组的区别 2.1、数组概述 2.2、数组和链表优缺点 2.3、链表和数组的比较 3、单...
    我哈啊哈啊哈阅读 2,925评论 1 41
  • 基本概念 链表的含义: 链表是一种用于存储数据集合的数据结构,具有以下属性 相邻元素之间通过指针相连 最后一个元素...
    古剑诛仙阅读 1,053评论 0 3
  • 本文来自本人著作《趣学数据结构》 链表是线性表的链式存储方式,逻辑上相邻的数据在计算机内的存储位置不一定相邻,那么...
    rainchxy阅读 3,874评论 6 20
  • 1. 链表的定义 线性表的链式存储结构的特点是用一组任意的存储单元存储线性表的数据元素,这组存储单元可以存在内存中...
    Eric_Hunter阅读 1,061评论 0 2
  • 一百万字梦想只要坚持,自己坚信一定会实现,当我写到快二十万字时,我空了会不由自主的翻看以前的作品,感觉就是记录就是...
    在听Yellow阅读 925评论 14 43

友情链接更多精彩内容