py002 - 单链表python代码实现

删除回忆录丶 提交于 2019-11-27 05:34:20

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:

在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!