单链表

class Node(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):
        """链表长度"""
        # 定义count记录节点个数
        count = 0
        cur = self.__head
        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, end=" ")
            # 让游标往后移动
            cur = cur.next
        print()

    def add(self, item):
        """链表头部添加元素"""
        # 创建一个节点
        node = Node(item)
        # 1、让新节点指向老的头节点
        node.next = self.__head
        # 2、让head指向新节点
        self.__head = node

    def append(self, item):
        """链表尾部添加元素"""
        # 判断链表是否为空
        if self.is_empty():
            self.add(item)
            return
        # 1、遍历找到尾节点
        cur = self.__head
        while cur.next is not None:
            cur = cur.next
        # 当while循环结束,cur指向的是尾节点
        # 2、尾节点指向新节点
        node = Node(item)
        cur.next = node

    def insert(self, pos, item):
        """指定位置添加元素"""
        # 健壮性
        if pos <= 0:
            self.add(item)
        elif pos > (self.length()-1):
            self.append(item)
        else:
            # 1、获取pos前面的节点
            # 定义index记录当前游标的下标
            cur = self.__head
            index = 0
            while index < (pos - 1):
                cur = cur.next
                index += 1
            # while循环结束后,cur指向的是pos前面的节点
            # 2、插入,新节点指向pos位置的节点
            node = Node(item)
            node.next = cur.next
            # 3、让pos前面的节点指向新节点
            cur.next = node

    def remove(self, item):
        """删除节点"""
        cur = self.__head
        # 定义pre记录cur的前面的节点
        pre = None
        while cur is not None:
            if cur.item == item:
                # 当前节点需要删掉
                if pre is None:  # 如果pre为空,证明删除的是头节点
                    self.__head = cur.next
                else:
                    pre.next = cur.next
                return
            pre = cur
            cur = cur.next

    def search(self, item):
        """查找节点是否存在"""
        cur = self.__head
        while cur is not None:
            if cur.item == item:
                return True
            cur = cur.next
        return False


if __name__ == '__main__':
    sll = SingleLinkList()
    print(sll.is_empty())
    print(sll.length())
    sll.add(1)
    print(sll.is_empty())
    sll.add(2)
    sll.add(3)
    sll.travel()
    print(sll.length())
    sll.append(4)
    sll.travel()
    sll.insert(2, 5)
    sll.travel()
    sll.insert(-2, 6)
    sll.travel()
    sll.insert(10, 8)
    sll.travel()
    sll.remove(6)
    sll.travel()
    sll.remove(2)
    sll.travel()
    sll.remove(8)
    sll.travel()
    sll.remove(9)
    sll.travel()
    print(sll.search(3))
    print(sll.search(5))
    print(sll.search(4))
    print(sll.search(9))

你可能感兴趣的:(单链表)