python实现单链表与双向链表

枫铃3年前 (2021-07-23)Python252

首先看单链表class Chain():

    def __init__(self):
        self.first = None
        self.length = 0
    def is_empty(self):
        """是否为空"""
        return self.first == None
    def add(self, val):
        """头部添加"""
        node = Node(val)
        temp = self.first
        node.next = temp
        self.first = node
        self.length += 1
    def append(self, val):
        """尾部添加"""
        node = Node(val)
        if self.first:
            temp = self.first
            mid = None
            while temp:
                mid = temp
                temp = temp.next
            mid.next = node
        else:
            self.first = node
        self.length += 1
    def __setitem__(self, item, val):
        """插入元素"""
        node = Node(val)
        temp, index = self.first, 0
        if item == 0:
            node.next, self.first = self.first, node
            self.length += 1
        else:
            while temp:
                if index+1 == item:
                    node.next, temp.next = temp.next, node
                    self.length += 1
                    break
                index += 1
                temp = temp.next
    def __len__(self):
        """链表长度"""
        return self.length
    @property
    def len_2(self):
        """链表长度(时间复杂度O(n))"""
        if not self.first:
            return 0
        else:
            temp = self.first
            length = 1
            while temp.next:
                length += 1
                temp = temp.next
            return length
    def pop(self):
        """删除尾部元素(有错误)"""
        temp = self.first
        mid = None
        while temp.next:
            mid, temp = temp, temp.next
        if mid:
            mid.next = None
            self.length -= 1
    def __delitem__(self, item):
        """删除某一位置元素"""
        temp, index = self.first, 0
        if item == 0:
            if self.first:
                self.first = self.first.next
                self.length -= 1
        while temp:
            if index + 1 == item:
                temp.next = temp.next.next
                self.length -= 1
            index += 1
            temp = temp.next
    def bianli(self):
        """遍历链表"""
        temp = self.first
        while temp:
            print(temp.value)
            temp = temp.next
   def reverse1(self):
      """反转链表"""              temp = self.first              prev = None              while temp:                    temp_next = temp.next                    temp.next = prev                    prev = temp                    temp = temp_next              self.first =  prev
   def reverse2(self):
      """反转链表"""      chain_list = []
      temp = self.first
      while temp:
        chain_list.append(temp.value)
        temp = temp.next
       temp = self.first
      while temp:
        temp.val = chain_list.pop()
        temp = temp.next

    def __iter__(self):
        pass
    def __next__(self):
        pass
class Node():
    def __init__(self, val):
        self.value = val
        self.next = None

在来看双向链表

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
class Node():
    def __init__(self, val):
        self.value = val
        self.prev = None
        self.next = None
class Chain():
    def __init__(self):
        self.first = None
    def is_empty(self):
        """是否为空"""
        return self.first == None
    def add(self, val):
        """头部添加"""
        node = Node(val)
        self.first.prev = node
        temp = self.first
        node.next = temp
        self.first = node
    def append(self, val):
        """尾部添加"""
        node = Node(val)
        temp = self.first
        if not temp:
            self.first = node
        else:
            while temp.next:
                temp = temp.next
            node.prev = temp
            temp.next = node
    def __delitem__(self, item):
        """删除元素"""
        temp, index = self.first, 0
        while temp:
            if index == item:
                if temp.next:
                    temp.next.prev, temp.prev.next = temp.prev, temp.next
                else:
                    temp.prev.next = None
            index += 1
            temp = temp.next
    def travel(self):
        temp = self.first
        while temp:
            print(temp.value)
            temp = temp.next

相关文章

python中的内部函数和闭包函数

python中的内部函数和闭包函数

内部函数 在函数内部声...

使用 Python 在 Linux 上实现一键回归测试

从代码库迁出代码 —- pexpect 的使用 测试人员从代码库(例如 CVS )迁出代码的过程中,需要手动输入访问...

Python 中,字符串"连接"效率最高的方式是?一定出乎你的意料

Python 中,字符串"连接"效率最高的方式是?一定出乎你的意料

网上很多文章人云亦云,字符串连接应该使用「join」方法而不要用「+」操作。说前者效率更高,它以更少的代价创建新字符串&#...

python max和min函数的高级用法

首先我们先看看正常max和min 的普通使用方法: students={'zs':18,'ls':21...

四种高性能数据类型,Python collections助你优化代码、简洁任务

四种高性能数据类型,Python collections助你优化代码、简洁任务

在这篇文章中,机器学习工程师 George Seif 介绍了 Python collections 模块最受欢迎的四种数据类型以及它们各自的使用...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。