node 获取表单数据 为空_Python 实现·数据结构与算法之双向链表

v2-27e5b2324842522701576a55df4f96df_1440w.jpg?source=172ae18b

双向链表定义

双向链表(Double Linked List)是一种更复杂的链表,每个节点除了包含元素域,还包含两个链接:一个指向前一个节点,当此节点为第一个节点时,指向空值;另一个指向下一个节点,当此节点为最后一个节点时,指向空值。

节点示意图

v2-954f4cdb05fc0bb20c3235cb7bbfc31e_b.jpg
  • 表元素域elem用来存放具体的数据。
  • 链接域prev用来存放上一个节点的位置(python中的标识)
  • 链接域next用来存放下一个节点的位置(python中的标识)

双向链表示意图

v2-126d97abe121b2042f7f9f14560a2d6a_b.png

双向链表的基本操作

  • is_empty() 判断链表是否为空
  • length 链表长度
  • travel() 遍历整个链表,打印元素
  • add(item) 在链表头部添加元素
  • append(item) 在链表尾部添加元素
  • insert(pos, item) 在指定位置插入元素
  • remove(item) 删除元素
  • clear() 清空链表
  • is_contain(item) 判断元素是否存在

Python 代码实现

# 节点代码实现

class Node(object):
    """双向链表节点"""
    def __init__(self, item):
        self.item = item
        self.next = None
        self.prev = None

# 双向链表代码实现

class DoubleLinkList(object):
    """双向链表"""
    def __init__(self):
        self._head = None

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

    @property
    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.item)
            cur = cur.next
        print("")

    def add(self, item):
        """头部插入元素"""
        node = Node(item)
        if self.is_empty():
            # 如果是空链表,将_head指向node
            self._head = node
        else:
            # 将node的next指向_head的头节点
            node.next = self._head
            # 将_head的头节点的prev指向node
            self._head.prev = node
            # 将_head 指向node
            self._head = node

    def append(self, item):
        """尾部插入元素"""
        node = Node(item)
        if self.is_empty():
            # 如果是空链表,将_head指向node
            self._head = node
        else:
            # 移动到链表尾部
            cur = self._head
            while cur.next is not None:
                cur = cur.next
            # 将尾节点cur的next指向node
            cur.next = node
            # 将node的prev指向cur
            node.prev = cur

    def is_contain(self, item):
        """查找元素是否存在"""
        cur = self._head
        while cur is not None:
            if cur.item == item:
                return True
            cur = cur.next
        return False
    
    def insert(self, pos, item):
        """在指定位置添加节点"""
        if pos <= 0:
            self.add(item)
        elif pos > (self.length-1):
            self.append(item)
        else:
            node = Node(item)
            cur = self._head
            count = 0
            # 移动到指定位置的前一个位置
            while count < (pos-1):
                count += 1
                cur = cur.next
            # 将node的prev指向cur
            node.prev = cur
            # 将node的next指向cur的下一个节点
            node.next = cur.next
            # 将cur的下一个节点的prev指向node
            cur.next.prev = node
            # 将cur的next指向node
            cur.next = node
              
    def remove(self, item):
        """删除元素"""
        if self.is_empty():
            return
        else:
            cur = self._head
            if cur.item == item:
                # 如果首节点的元素即是要删除的元素
                if cur.next is None:
                    # 如果链表只有这一个节点
                    self._head = None
                else:
                    # 将第二个节点的prev设置为None
                    cur.next.prev = None
                    # 将_head指向第二个节点
                    self._head = cur.next
                return
            while cur is not None:
                if cur.item == item:
                    # 将cur的前一个节点的next指向cur的后一个节点
                    cur.prev.next = cur.next
                    # 将cur的后一个节点的prev指向cur的前一个节点
                    cur.next.prev = cur.prev
                    break
                cur = cur.next
                
    def clear(self):
        """清空链表"""
        self._head = None
    
    def __len__(self):
        """可以用len()方法获取链表长度"""
        return self.length
    
    def __iter__(self):
        """可以使用循环遍历链表"""
        cur = self._head
        while cur is not None:
            value = cur.item
            cur = cur.next
            yield value
            
    def __contains__(self, item):
        """可以用in判断元素是否在链表中"""
        cur = self._head
        while cur is not None:
            if cur.item == item:
                return True
            cur = cur.next
        return False

# 测试数据

if __name__ == "__main__":
    print("------创建链表------")
    dl_list = DoubleLinkList()
    dl_list.add(1)
    dl_list.add(2)
    dl_list.append(3)
    dl_list.insert(2, 4)
    dl_list.insert(4, 5)
    dl_list.insert(0, 6)
    print("length:",len(dl_list))
    dl_list.travel()
    print(dl_list.is_contain(3))
    print(dl_list.is_contain(8))
    print(3 in dl_list)
    print(8 in dl_list)
    dl_list.remove(1)
    print("length:",len(dl_list))
    dl_list.travel()
    print("------循环遍历------")
    for i in dl_list:
        print(i)

# 输出结果

------创建链表------
length: 6
6
2
1
4
3
5

True
False
True
False
length: 5
6
2
4
3
5

------循环遍历------
6
2
4
3
5

算法分析

v2-9de47dcb9e45cc987f23beabfc742826_b.jpg

联系我们

个人博客网站:

首页_bling博客系统​www.bling2.cn
v2-045c846dfff05dc0651cf2dbf2849cd1_180x120.jpg

Github地址:

lb971216008/Use-Python-to-Achieve​github.com
v2-68ec00aa0ca3f624bc254719dd2369b6_ipico.jpg

知乎专栏:

Python实现​zhuanlan.zhihu.com
v2-e7d5efc69ecb3dbe5181f09206e90f4b_ipico.jpg

小专栏:

Python实现 - 小专栏​xiaozhuanlan.com
v2-e7d5efc69ecb3dbe5181f09206e90f4b_ipico.jpg

博客园:

南风以南 - 博客园​www.cnblogs.com

v2-5cec46a2d3f7d2c31fb9849697076658_b.gif
扫码关注公众号【不灵兔】,获取更多免费资源,生活不易,拜托了。。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值