IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 单链表常规操作 -> 正文阅读

[数据结构与算法]单链表常规操作

?

class Node(object): #定义节点结构
    def __init__(self, val=None):
        self.val = val
        self.next = None

class SingleLinkList(object):
    def __init__(self, node=None):
        self.__head = node # 定义这个链的起始地址

    def is_empty(self): #判空
        return self.__head is None

    def get_length(self): #求长度
        count, cur = 0, self.__head
        while cur is not None:
            count += 1
            cur = cur.next
        return count

    def ergodic(self): #遍历
        cur = self.__head
        while cur is not None:
            print(cur.val, end=' ')
            cur = cur.next

    def find_val(self, x): #查找
        cur = self.__head
        while cur is not None:
            if cur.val == x:
                return True
            else:
                cur = cur.next
        return False

    def head_insert(self, x): #头插法
        node = Node(x)
        if self.__head is None:
            self.__head = node
            return self.__head
        else:
            node.next = self.__head
            self.__head = node
            return self.__head

    def tail_insert(self, x): #尾插法
        node = Node(x)
        cur = self.__head
        if cur is None:
            self.__head = node
            return self.__head
        while cur.next is not None:
            cur = cur.next
        cur.next = node
        return self.__head

    def insert(self, pos, x): #普通插入
        node = Node(x)
        cur = self.__head
        if pos <= 0:
            self.head_insert(x)
            return self.__head
        if pos >= self.get_length():
            self.tail_insert(x)
            return self.__head
        for i in range(pos-1):
            cur = cur.next
        node.next = cur.next
        cur.next = node
        return self.__head

    def remove(self, x): #按值删除元素
        cur = self.__head
        while cur.next is not None:
            if self.__head.val == x:
                self.__head = self.__head.next
                return self.__head
            if cur.next.val == x:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return -1

    def removeAllVal(self, x): #LeetCode题目,
        #删除链表中所有值为x的节点
        cur = self.__head
        if cur.val == x:
            self.__head = self.__head.next
        while cur.next is not None:
            if cur.next.val == x:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return self.__head

    def removeRepeatVal(self): # LeetCode题目
        # 删除排序链表中的重复元素,只保留没有重复出现的数字
        pass


if __name__ == "__main__":
    sll = SingleLinkList()
    print('链表sll是否为空', sll.is_empty())
    sll.head_insert(200)
    print("链表长度为:", sll.get_length())
    sll.tail_insert(2)
    sll.tail_insert(4)
    sll.tail_insert(1)
    sll.tail_insert(3)
    print('链表sll是否为空', sll.is_empty())
    print("链表长度为:",sll.get_length())
    print("是否查到元素", sll.find_val(21))
    sll.head_insert(39)
    sll.ergodic()
    print()
    sll.insert(3, 100)
    sll.insert(-100, 100)
    sll.tail_insert(100)
    sll.ergodic()
    sll.removeAllVal(100)
    sll.remove(2)
    print()
    sll.ergodic()

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-08-01 16:14:50  更:2021-08-01 16:14:56 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/14 6:36:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码