数据结构(一)单向链表的的分析与python代码实现

版权声明:尊重原作者,转载请在文章头部注明网址。 https://blog.csdn.net/u013034226/article/details/85778074

概念

       单向链表也叫单链表,是链表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域。这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值。

结构

 代码实现




class Node(object):
    """实现单链表节点"""
    def __init__(self, item):
        # item存放数据元素
        self.item = item
        # next代表下一个节点
        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
        n = 0
        # 当未到达尾节点None时
        while cur is not None:
            n += 1
            # 将cur后移一个节点
            cur =cur.next

        return n

    # 遍历整个链表
    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对象
        node = Node(item)

        # 先让新增加的节点next指向原首节点
        node.next = self.head
        # 然后让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

        # 至此cur指向当前链表的尾节点
        # 2、让cur的next指向新节点
        node = Node(item)
        cur.next = node

    # 指定位置添加元素
    def insert(self, pos, item):
        # 需要对添加的位置进行分类讨论
        # 如果添加位置<=0,则直接在头部添加
        if pos <= 0:
            self.add(item)
        # 位置大于链表长度,则直接添加到尾部
        elif pos >= self.length():
            self.append(item)
        else:
            # 1 遍历查找待插入位置的前一个节点cur
            cur = self.head
            for i in range(pos-1):
                cur = cur.next
            # 至此,cur指向的就是待插入位置的前一个节点
            # 2 新节点的next指向cur的next
            node = Node(item)
            node.next = cur.next
            # 3 cur的next指向新节点
            cur.next = node

    # 删除节点
    def remove(self, item):
        # 思路:让待删节点的pre(前节点)next指向待删节点的next
        cur = self.head
        pre = None
        # 1 找到待删节点并记录前一个节点
        while cur is not None:
            # 待删节点不存在也就不用删了,若存在
            if cur.item == item:
                # 2 若有前一个节点,pre的next指向cur的next
                if pre is not None:
                    pre.next = cur.next
                # 否则(待删节点是第0个节点),head指向cur的next
                else:
                    self.head = 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__':
    sl = SingleLinkList()
    sl.add(1)
    sl.add(2)
    sl.add(3)
    sl.travel()
    # 结果3-2-1-
    sl.append("abc")
    sl.append("def")
    sl.append("ghi")
    sl.travel()
    # 结果3-2-1-abc-def-ghi-
    sl.insert(-1, "xx")
    sl.insert(99, "yy")
    sl.insert(3, "zz")
    sl.travel()
    # 结果xx-3-2-zz-1-abc-def-ghi-yy-
    sl.remove("xx")
    sl.remove("yy")
    sl.remove(1)
    sl.remove(90)
    sl.travel()
    # 结果3-2-zz-abc-def-ghi-
    print(sl.search(3))
    # 结果True
    print(sl.search("zz"))
    # 结果True
    print(sl.search(380))
    # 结果False

复杂度

操作 链表 顺序表
访问元素 O(n) O(1)
在头部插入/删除 O(1) O(n)
在尾部插入/删除 O(n) O(1)
在中间插入/删除 O(n) O(n)

猜你喜欢

转载自blog.csdn.net/u013034226/article/details/85778074
今日推荐