py002 - 单链表python代码实现

Code:

class Node(object):
    """结点"""
    def __init__(self, elem):
        self.elem = elem
        self.next = None


class SingleLinklist(object):
    """单链表"""

    def __init__(self, node = None):
        self.__head = node
        
    def is_empty(self):
        """判断单链表是否为空"""
        return self.__head == None

    def length(self):
        """单链表长度"""
        cur = self.__head  # 设置游标cur
        count = 0  # 设置计数器
        while cur != None:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        """遍历单链表"""
        cur = self.__head  # 设置游标cur
        while cur != None:
            print(cur.elem, end=" ")
            cur = cur.next
        print()

    def add(self, elem):
        """在表头增加一个元素"""
        node = Node(elem)
        node.next = self.__head
        self.__head = node

    def append(self, elem):
        """在表尾增加元素"""
        node = Node(elem)
        if self.is_empty():
            self.__head = node
        else:
            cur = self.__head  # 设置游标
            while cur.next != None:
                cur = cur.next
            cur.next = node

    def insert(self, pos, elem):
        """在指定处增加元素
        :param pos 从0开始 
        """
        if pos <= 0:
            self.add(elem)
        elif pos >= (self.length()-1):
            self.append(elem)
        else:
            node = Node(elem)
            i = 0
            cur = self.__head
            while i < (pos-1):
                cur = cur.next
                i += 1
            node.next = cur.next
            cur.next = node

    def remove(self, elem):
        """删除某一元素"""
        if self.is_empty():
            print("SingleLinklist is none!")
            return False
        cur = self.__head
        pre = None
        mark = False
        while cur != None:
            if cur.elem != elem:
                pre = cur
                cur = cur.next
            # 头节点要特殊处理
            else:
                mark = True
                if cur == self.__head:
                    self.__head = self.__head.next    
                    cur = self.__head
                else: 
                    pre.next = cur.next 
                    cur = cur.next
        print(mark)  # 显示是否删除成功

    def search(self, elem):
        """查找某一元素"""
        if self.is_empty():
            return False
        cur = self.__head
        count = 0
        while cur != None:
            if cur.elem == elem:
                return count 
            cur = cur.next
            count += 1
        return False

if __name__ == "__main__":
    li = SingleLinklist()
    print("添加与遍历测试:")
    print(li.is_empty())
    li.append(1)
    print(li.is_empty())
    li.append(2)
    li.append(3)
    li.append(4)
    li.append(5)
    li.append(6)
    li.travel()
    print(li.length())
    print()
    li.add(0)
    li.append(8)
    li.travel()
    li.insert(3, 666)
    li.travel()
    li.insert(13, 666)
    li.insert(-3, 1000)
    li.travel()
    print("-" * 10)
    print("查找测试:")
    print(li.search(1000))
    print(li.search(7))
    print(li.search(8))
    print("-" * 10)
    print("删除测试")
    li.insert(3, 666)
    li.insert(3, 666)
    li.insert(3, 666)
    li.travel()
    li.remove(666)
    li.remove(666)
    li.travel()
    li.remove(6)
    li.travel()
    li.remove(1000)
    li.travel()

OUTPUT:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值