03 单向链表

03 单向链表

# encoding: utf-8

class SingleNode(object):
    """单链表的节点"""
    def __init__(self, item):
        self.item = item
        self.next = None

class SingleLinkList(object):
    """单链表"""
    def __init__(self):
        # 头部节点 私有属性
        self._head = None

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

    def length(self):
        """链表长度"""
        # cur 游标 用来移动遍历节点
        cur = self._head
        count = 0
        while cur != None:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        """遍历整个链表"""
        # cur 游标 用来移动遍历节点
        cur = self._head
        while cur != None:
            print(cur.item)
            cur = cur.next

    def add(self, item):
        """链表头部添加元素"""

        # 1.正常 2.空

        node = SingleNode(item)

        # node的next指向头节点
        node.next = self._head
        # 头节点指向node
        self._head = node

    def append(self, item):
        """链表尾部添加元素"""

        # 1.正常 2.空

        node = SingleNode(item)

        if self.is_empty():
            self._head = node
        else:
            # cur 用来指向尾部节点
            cur = self._head
            while cur.next != None:
                cur = cur.next

            # 尾部节点的next指向node
            cur.next = node

    def insert(self, pos, item):
        """链表指定位置添加元素"""
        # 从0开始

        # 1.指定头部 2.指定尾部 3.指定中间

        if pos <= 0:
            self.add(item)
        elif pos > (self.length() - 1):
            self.append(item)
        else:
            node = SingleNode(item)

            # pre 指向pos的前一个位置pos-1,从头节点移动到指定位置
            pre = self._head
            # num 表示pre当前的位置
            num = 0

            while num < (pos - 1):
                pre = pre.next
                num += 1

            # node的next指向pre的next
            node.next = pre.next
            # pre的next指向node
            pre.next = node

    def remove(self, item):
        """删除元素"""

        # 1.找到了 2.没找到
        # 1.1 在头部 1.2 不在头部

        # cur 表示当前遍历的节点
        cur = self._head
        # pre表示cur的前一个节点
        pre = None

        while cur != None:

            # 找到了
            if cur.item == item:

                # 找到的是头节点
                if pre is None:
                    self._head = cur.next

                # 不是头节点
                else:
                    # 前一个节点的next 指向 当前节点的next, 即删除当前节点
                    pre.next = cur.next

                # 找到了 删除之后 退出循环
                break

            # 没找到, 往后寻找            
            else:
                pre = cur
                cur = cur.next



    def search(self, item):
        """查找元素是否存在"""
        cur = self._head
        while cur != None:
            if cur.item == item:
                return True
            else:
                cur = cur.next
        return False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值