概念

链表(linked_list)是物理存储单元上非连续的、非顺序的存储结构,数据元素的逻辑顺序
是通过链表的指针地址实现,每个元素包含两个结点,一个是存储元素的数据域 (内存空间)
,另一个是指向下一个结点地址的指针域。根据指针的指向,链表能形成不同的结构,例如
单链表,双向链表,循环链表等.
链表通过将链点 i 与其邻居链点 i+1 通过指针相关联,从索引 0 到索引 N-1 对链点进
行排序.

实现

class Node:
    """
    链点
    """

    def __init__(self, data):
        self.data = data    # 数据域
        self.next = None    # 指针域


class SingleLinkedList:
    """
    单链表
    """

    def __init__(self, head=None):
        self.head = Node(head)    # 链表头部元素
        self._length = None

    # 向链表中添加新链点
    def append(self, value):
        self._length = None
        new_node = Node(value)
        # 将头部节点指向临时变量
        current = self.head
        # 当头部节点存在时
        if self.head:
            # 循环遍历到链表的最后一个节点
            while current.next:
                current = current.next
            current.next = new_node
        # 当头部节点不存在时
        else:
            self.head = new_node

    # 判断链表是否为空
    def is_empty(self):
        return bool(self.head)

    # 向链表任意位置插入元素
    def insert(self, position, value):
        self._length = None
        new_node = Node(value)
        # 判断插入位置是否在链表的索引范围内
        if position < 0 or position > self.get_length():
            raise IndexError('Out of linked list range.')

        # 当插入位置为头节点时
        if position == 0:
            new_node.next, self.head = self.head, new_node
            return
        # 当插入位置不在头节点时,遍历链表找到插入位置,插入新节点
        current = self.head
        i = 0
        while i < position:
            pre, current = current, current.next
            i += 1
        pre.next, new_node.next = new_node, current

    # 删除指定位置的节点
    def remove(self, position):
        self._length = None
        # 判断删除位置是否在链表的索引范围内
        if position < 0 or position > self.get_length() - 1:
            raise IndexError('Position out of linked list range.')

        i = 0
        current = self.head
        # 当删除位置为头节点时
        if position == i:
            self.head, current.next = self.head.next, None
            return

        # 当删除位置不是头节点时,遍历链表找到删除位置
        i += 1
        prev, current = current, current.next
        while i != position:
            prev, current = current, current.next
            i += 1
        prev.next, current.next = current.next, None

    # 获取链表长度
    def get_length(self):
        if self._length is not None:
            return self._length
        current = self.head
        length = 0
        while current is not None:
            length += 1
            current = current.next
        self._length = length
        return length

    # 遍历链表,并依次打印节点数据
    def print_list(self):
        print('linked_list:')
        current = self.head
        while current is not None:
            print(current.data)
            current = current.next

    # 将链表反转
    def reverse(self):
        prev = None
        current = self.head
        while current is not None:
            # next_node = current.next
            # current.next = prev
            # prev = current
            # current = next_node
            next_node, current.next = current.next, prev
            prev, current = current, next_node
        self.head = prev

    # 将列表转换为链表
    def init_list(self, data_list):
        self.head = Node(data_list[0])
        current = self.head
        for item in data_list[1:]:
            node = Node(item)
            current.next, current = node, node

    # 替换指定位置的节点
    def replace(self, position, value):
        # 判断替换位置是否在链表索引范围内
        if position < 0 or position > self.get_length() - 1:
            raise IndexError("Position out of linked-list range.")
        _node = Node(value)
        _current = self.head
        i = 0
        if position == 0:
            _node.next, self.head = self.head.next, _node
            _current.next = None
        else:
            i += 1
            _prev, _current = _current, _current.next
            while i != position:
                i += 1
                _prev, _current = _current, _current.next
            _prev.next, _node.next = _node, _current.next
            _current.next = None

    # 返回给定值的第一个节点索引
    def index(self, value):
        _current = self.head
        i = 0
        while _current is not None:
            if _current.data == value:
                return i
            i += 1
            _current = _current.next
        return -1

    def __getitem__(self, position):
        if position < 0 or position > self.get_length() - 1:
            raise IndexError("Position out of linked-list range.")
        _current = self.head
        i = 0
        while i != position:
            _current = _current.next
            i += 1
        return _current.data
posted on 2019-12-13 13:10  雷子-LL  阅读(306)  评论(0编辑  收藏  举报