zoukankan      html  css  js  c++  java
  • 05-3_单链表的实现

    1. 单链表的操作

    • is_empty(): 链表是否为空
    • length(): 链表长度
    • travel(): 遍历整个链表
    • add(item): 链表头部添加元素
    • append(item): 链表尾部添加元素
    • insert(pos, item): 指定位置添加元素
    • remove(item): 删除节点
    • search(item): 查找节点是否存在

    2. 单链表的实现

    # 节点的实现, 节点类
    class SingleNode(object):
        """单链表节点"""
        def __init__(self, elem):
            """
            一个节点的初始状态
            :param elem:
            """
            self.elem = elem
            self.next = None
    # node = SingleNode(100)
    
    
    # 链表类
    class SingleLinkList(object):
        """
        单链表的实现
        """
        def __init__(self, node=None):
            self._head = node
    
        def is_empty(self):
            """链表是否为空"""
            return self._head is None
    
        def length(self):
            """链表长度"""
            cur = self._head    # cur游标,用来移动遍历节点, 初始化为指向头节点
            count = 0           # 计录数量
            while cur is not None:    # 循环数数, 条件为 不是 None
                count += 1      #
                cur = cur.next  # cur游标 移动到下一个节点
    
            # 返回链表的长度数量
            return count
    
        def travel(self):
            """遍历整个链表"""
            cur = self._head
            while cur is not None:
                print(cur.elem, end=' ')
                cur = cur.next
                # if cur is None:
                    # print('')      # 最后一个元素换行
            print('')
    
        def add(self, item):
            """链表头部添加元素"""
            node = SingleNode(item)
            node.next = self._head
            self._head = node   # 头节点
    
        def append(self, item):
            """链表尾部添加元素"""
            node = SingleNode(item)
            if self.is_empty():     # 判断链表是否为空
                self._head = node
            else:
                cur = self._head
                while cur.next is not None:
                    cur = cur.next
                cur.next = node
    
        def insert(self, index, item):
            """
            指定位置添加元素
            :param index 从0开始
            """
            if index <= 0:    # 如果index小于等于0, 则是从头部插入
                self.add(item)
            elif index > self.length()-1:   # 如果index大于单链表的最大索引, 则是 尾插入
                self.append(item)
            else:
                prev = self._head   # 上一个节点, 是self._head
                count = 0
                while count < (index-1):
                    count += 1
                    prev = prev.next
                # 当循环退出后, prev 指向 (index-1) 的位置
                node = SingleNode(item)
                node.next = prev.next
                prev.next = node
    
        def remove(self, item):
            """删除节点"""
            cur = self._head
            prev = None
            while cur is not None:
                if cur.elem == item:
                    # 先判断此节点是否是头节点
                    if cur == self._head:
                        # 是头节点
                        self._head = cur.next
                    else:
                        prev.next = cur.next
                    break   # 删除了就要退出
                else:
                    prev = cur
                    cur = cur.next
    
        def search(self, item):
            """查找节点是否存在"""
            cur = self._head
            while cur is not None:
                if cur.elem == item:
                    return True     # 找到了
                else:
                    cur = cur.next
            return False   # 没找到
    
    # 测试
    if __name__ == '__main__':
        single_link_list = SingleLinkList()     # 构建一个空的单链表对象
        print(single_link_list.is_empty())      # True,   该单链表是否为空,True为空,否则False
        
        single_link_list.append(2)
        
        single_link_list.add(88)    # 在头部添加 88
        
        single_link_list.insert(1, 20)   # 指定位置
        single_link_list.travel()     # 遍历这个单链表
    
        single_link_list.insert(0, 100)
        single_link_list.travel()     # 遍历这个单链表
    
        single_link_list.insert(20, 500)
        single_link_list.travel()     # 遍历这个单链表
        
        single_link_list.remove(100)
        single_link_list.travel()     # 遍历这个单链表
    
  • 相关阅读:
    编译器合成的拷贝构造函数
    WIN phone 8.1 SDK 坑遇到 Hyper-V
    JDBC编程步骤
    关闭safari浏览器button默认样式
    Codeforces Round #273 (Div. 2)
    android Activity之间数据传递 Parcelable和Serializable接口的使用
    如何删除JAVA集合中的元素
    Android自定义长按事件
    关于android多点触控
    Android Touch系统简介(二):实例详解onInterceptTouchEvent与onTouchEvent的调用过程
  • 原文地址:https://www.cnblogs.com/nichengshishaonian/p/11576102.html
Copyright © 2011-2022 走看看