• Python3单链表简单操作


    单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。

    class Node():
        # 定义头结点
        def __init__(self, data):
            self.data = data
            # 头指针为空
            self.next = None
    
    
    # 头插法
    class SNode():
        def __init__(self):
            self.current_node = None
    
        def add_node(self, data):
            node = Node(data)
            node.next = self.current_node
            self.current_node = node
    
        def append_node(self, data):
            # 尾插法插入节点
            node = Node(data)
            cur = self.current_node
            # 遍历链表直到头节点处停止遍历
            while cur:
                if cur.next == None:
                    break
                cur = cur.next
            cur.next = node
    
        def travel(self):
            '''
            遍历链表
            :return:
            '''
            cur = self.current_node
            while cur:
                print(cur.data)
                cur = cur.next
    
        def is_empty(self):
            '''
            判断链表非空
            :return:
            '''
            return self.current_node == None
    
        def get_lenth(self):
            '''
            获取链表的长度
            :return:
            '''
            cur = self.current_node
            count = 0
            while cur:
                count += 1
                cur = cur.next
            return count
    
        def insert_node(self, index, data):
            '''
            指定位置插入节点
            :param index:
            :param data:
            :return:
            '''
            link_len = self.get_lenth()
            if index == 0:
                self.add_node(data)
            elif index >= link_len:
                self.append_node(data)
            else:
                cur = self.current_node
                for i in range(1, index):
                    cur = cur.next
                node = Node(data)
                node.next = cur.next
                cur.next = node
    
        def del_node(self, index):
            '''
            根据索引删除节点
            :param index:
            :return:
            '''
    
            # 找到前节点
            cur = self.current_node
            # 前驱节点
            pre = None
            count = 1
            len_num = self.get_lenth()
            while cur:
                if index == 1:
                    self.current_node = cur.next
                    break
                if count == index and count < len_num:
                    pre.next = cur.next
                    break
                if count >= len_num:
                    pre.next = None
                    break
                count += 1
                pre = cur
                cur = cur.next
    
    
    
    if __name__ == "__main__":
        test = SNode()
        list_data = [1, 2, 3]
        for i in list_data:
            test.add_node(i)
        test.travel()
        # print(test.is_empty())
        # print (test.get_lenth())
        # test.append_node(4)
        # test.insert_node(1, 4)
        # test.travel()
        # test.del_node(4)
        # test.travel()
    
        '''
        单链表的操作
    is_empty() 链表是否为空
    length() 链表长度
    travel() 遍历整个链表
    add(item) 链表头部添加元素
    append(item) 链表尾部添加元素
    insert(pos, item) 指定位置添加元素
    remove(item) 删除节点
    search(item) 查找节点是否存在
        '''
    

      

  • 相关阅读:
    腾讯会议API接入
    解决远程调用三方接口:javax.net.ssl.SSLHandshakeException报错
    iOS自动创建本地化文件
    数组转换
    2021MongoDB技术实践与应用案例征集活动获奖通知
    MongoDB按需物化视图介绍
    参会指南 | 2021MongoDB南京技术沙龙
    叮咚买菜自建MongoDB上云实践
    MongoDB技术实践与应用案例征集中
    使用WT工具恢复MongoDB数据
  • 原文地址:https://www.cnblogs.com/xiaotaiyangi/p/10574055.html
Copyright © 2020-2023  润新知