• 数据结构与算法-python描述-双向链表


    # coding:utf-8
    
    # 双向链表的相关操作:
    # is_empty() 链表是否为空
    # length() 链表长度
    # travel() 遍历链表
    # add(item) 链表头部添加
    # append(item) 链表尾部添加
    # insert(pos, item) 指定位置添加
    # remove(item) 删除节点
    # search(item) 查找节点是否存在
    
    
    class Node(object):
        """节点"""
    
        def __init__(self, item):
            self.elem = item
            self.prev = None
            self.next = None
    
    
    class DoubleLinkedList(object):
        """双向链表"""
    
        def __init__(self, node=None):
            self.__head = node
    
        def is_empty(self):
            """链表是否为空"""
            return self.__head is None
    
        def length(self):
            """链表长度"""
            cur = self.__head
            count = 0
            while cur is not None:
                count += 1
                cur = cur.next
            return count
    
        def travel(self):
            """遍历链表"""
            cur = self.__head
    
            while cur is not None:
                print(cur.elem, end=" ")
                cur = cur.next
    
            print("")
    
        def add(self, item):
            """链表头部添加,头插法"""
            node = Node(item)
    
            # 注意处理链表为空的情况
            if self.is_empty():
                self.__head = node
            else:
                node.next = self.__head
                self.__head = node
                node.next.prev = node
    
        def append(self, item):
            """链表尾部添加,尾插法"""
            node = Node(item)
    
            if self.is_empty():
                self.__head = node
            else:
                cur = self.__head
    
                while cur.next is not None:
                    cur = cur.next
    
                # cur当前指向最后一个节点
                cur.next = node
                node.prev = cur
    
        def insert(self, pos, item):
            """指定位置添加"""
            node = Node(item)
    
            # 头插法
            if pos <= 0:
                self.add(item)
            # 尾插法
            elif pos > (self.length() - 1):
                self.append(item)
            # 中间插入
            else:
                cur = self.__head
                count = 0
    
                while count < pos:
                    count += 1
                    cur = cur.next
    
                # 循环结束,cur指向要插入的位置
                node.next = cur
                node.prev = cur.prev
                cur.prev.next = node
                cur.prev = node
    
        def remove(self, item):
            """删除节点"""
            # 先考虑一般情况,再看一般情况下的代码是否适合特殊情况
            cur = self.__head
    
            while cur is not None:
                if cur.elem == item:
                    # 判断此节点是否是头节点
                    if cur == self.__head:
                        self.__head = cur.next
                        # 判断链表中是否只有一个节点
                        if cur.next is not None:
                            cur.next.prev = None
                    else:
                        cur.prev.next = cur.next
                        # 判断是否是尾节点
                        if cur.next:
                            cur.next.prev = cur.prev
                    break
                else:
                    cur = cur.next
    
        def search(self, item):
            """查找节点是否存在"""
            cur = self.__head
    
            while cur is not None:
                if cur.elem == item:
                    return True
                else:
                    cur = cur.next
    
            return False
    
    
    if __name__ == "__main__":
        dll = DoubleLinkedList()
    
        print("before initialized:", dll.is_empty())
        print("before initialized:", dll.length())
    
        dll.add(1)
        dll.add(2)
        dll.add(3)
        dll.add(4)
        dll.add(5)
        dll.add(6)
        dll.travel()
    
        dll.append(7)
        dll.travel()
    
        dll.insert(3, 100)
        dll.travel()
    
        dll.remove(3)
        dll.travel()
    
    
  • 相关阅读:
    2015-05-27 用正则把oracle时间转化到mysql时间
    linux版idea14界面美观和windows,mac基本一致
    ubuntu 下自定义快捷键,,用着舒服
    ubuntu 方便好用的截图软件
    Integer 包装器类 大小比较
    win7、ubuntu双系统,遇到分区不可用问题,和卸载ubuntu后win7开不了机
    巧妙小思想
    读取16进制文件和校验图片格式的问题。 文件名后缀
    旧电脑变废为宝!
    Win10打开Autodesk软件时提示“管理员已阻止你运行此应用”
  • 原文地址:https://www.cnblogs.com/coderwjq/p/7308659.html
Copyright © 2020-2023  润新知