zoukankan      html  css  js  c++  java
  • python基础下的数据结构与算法之链表

    一、链表的定义

      用链接关系显式表示元素之间顺序关系的线性表称为链接表或链表。

    二、单链表的python实现

    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
    # 定义计数
    count = 0
    while cur != None:
    count += 1
    cur = cur.next
    return count

    def travel(self):
    """遍历整个链表"""
    cur = self.__head
    while cur != None:
    print(cur.elem,end=" ")
    cur = cur.next
    print("")

    def add(self, item):
    """链表头部添加元素,头插法"""
    node = Node(item)
    node.next = self.__head
    self.__head = node

    def append1(self, item):
    """链表尾部添加元素,尾插法"""
    node = Node(item)
    # if self.__head == None:
    if self.is_empty():
    self.__head = node
    return
    else:
    cur = self.__head
    while cur.next != None:
    cur = cur.next
    cur.next = node

    def insert(self, pos, item):
    """指定位置添加元素
    pos 从0开始
    """
    if pos < 0 :
    # 输入值小于0,默认为头插法
    return self.add(item)
    elif pos > self.length()-1:
    # 输入值大于列表长度,默认为尾插法
    self.append1(item)
    else:
    node = Node(item)
    cur = self.__head
    count = 0
    while count < (pos-1):
    count += 1
    cur = cur.next
    # 当循环退出时,cur指向pos-1的位置
    node.next = cur.next
    cur.next = node

    def remove(self, item):
    """删除节点,只删除第一个找到的数据"""
    cur = self.__head
    pre = None
    while cur != None:
    if cur.elem == item:
    # 先判断此节点是否是头节点
    # if pre == None:
    if cur == self.__head:
    self.__head = cur.next
    # 删除节点
    else:
    pre.next = cur.next
    break
    else:
    pre = cur
    cur = cur.next
    return


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

    三、单循环链表的python实现:

    class Node(object):
    """定义节点"""

    def __init__(self, elem):
    self.elem = elem
    self.next = None


    class SingleCycleList(object):
    """单向循环链表操作"""

    def __init__(self, node=None):
    """初始化"""
    self.__head = node
    if node:
    node.next = node

    def is_empty(self):
    """链表是否为空"""
    return self.__head == None

    def length(self):
    """链表长度"""
    # 定义游标,用来移动遍历节点
    if self.is_empty():
    return 0
    cur = self.__head
    # 定义计数
    count = 1
    while cur.next != self.__head:
    count += 1
    cur = cur.next
    return count

    def travel(self):
    """遍历整个链表"""
    if self.is_empty():
    return 0
    cur = self.__head
    while cur.next != self.__head:
    print(cur.elem, end=" ")
    cur = cur.next
    # 退出循环时,cur指向尾节点,但未打印尾节点元素
    print(cur.elem)

    def add(self, item):
    """链表头部添加元素,头插法"""
    node = Node(item)
    if self.is_empty():
    self.__head = node
    node.next = node
    return
    cur = self.__head
    while cur.next != self.__head:
    cur = cur.next
    # 退出循环时,cur指向尾节点
    node.next = self.__head
    self.__head = node
    cur.next = node

    def append1(self, item):
    """链表尾部添加元素,尾插法"""
    node = Node(item)
    # if self.__head == None:
    if self.is_empty():
    self.__head = node
    node.next = node
    else:
    cur = self.__head
    while cur.next != self.__head:
    cur = cur.next
    cur.next = node
    node.next = self.__head

    def insert(self, pos, item):
    """指定位置添加元素
    pos 从0开始
    """
    if pos < 0 :
    # 输入值小于0,默认为头插法
    return self.add(item)
    elif pos > self.length()-1:
    # 输入值大于列表长度,默认为尾插法
    self.append1(item)
    else:
    node = Node(item)
    cur = self.__head
    count = 0
    while count < (pos-1):
    count += 1
    cur = cur.next
    # 当循环退出时,cur指向pos-1的位置
    node.next = cur.next
    cur.next = node

    def remove(self, item):
    """删除节点,只删除第一个找到的数据"""
    if self.is_empty():
    return
    cur = self.__head
    pre = None
    while cur.next != self.__head:
    if cur.elem == item:
    # 先判断此节点是否是头节点
    # if pre == None:
    if cur == self.__head:
    # 头结点的情况,找尾节点
    rear = self.__head
    while rear.next != self.__head:
    rear = rear.next
    self.__head = cur.next
    rear.next = self.__head
    # 删除节点
    else:
    # 中间节点
    pre.next = cur.next
    return
    else:
    pre = cur
    cur = cur.next
    # 退出循环时,cur指向尾节点
    if cur.elem == item:
    if cur == self.__head:
    # 链表只有一个节点
    self.__head = None
    else:
    pre.next = cur.next

    def search(self, item):
    """查找节点是否存在"""
    if self.is_empty():
    return False
    cur = self.__head
    while cur.next != self.__head:
    if cur.elem == item:
    return True
    else:
    cur = cur.next
    # 退出循环时,cur指向尾节点
    if cur.elem == item:
    return True
    return False

    四、双循环链表的python实现:

    class Node(object):
    """定义节点"""

    def __init__(self, item):
    self.elem = item
    self.next = None
    self.prev = None


    class DoubleLinkList(object):
    """双链表操作"""

    def __init__(self, node=None):
    """初始化,构造"""
    self.__head = node

    def is_empty(self):
    """链表是否为空"""
    return self.__head == None

    def length(self):
    """链表长度"""
    # 定义游标,用来移动遍历节点
    cur = self.__head
    # 定义计数
    count = 0
    while cur != None:
    count += 1
    cur = cur.next
    return count

    def travel(self):
    """遍历整个链表"""
    cur = self.__head
    while cur != None:
    print(cur.elem, end=" ")
    cur = cur.next
    print("")

    def add(self, item):
    """链表头部添加元素,头插法"""
    node = Node(item)
    node.next = self.__head
    self.__head = node
    node.next.prev = node

    def append1(self, item):
    """链表尾部添加元素,尾插法"""
    node = Node(item)
    # if self.__head == None:
    if self.is_empty():
    self.__head = node
    return
    else:
    cur = self.__head
    while cur.next != None:
    cur = cur.next
    cur.next = node
    node.prev = cur

    def insert(self, pos, item):
    """指定位置添加元素
    pos 从0开始
    """
    if pos < 0:
    # 输入值小于0,默认为头插法
    return self.add(item)
    elif pos > self.length() - 1:
    # 输入值大于列表长度,默认为尾插法
    self.append1(item)
    else:
    node = Node(item)
    cur = self.__head
    count = 0
    while count < pos:
    count += 1
    cur = cur.next
    # 当循环退出时,cur指向pos的位置
    node.next = cur
    node.prev = cur.prev
    cur.prev.next = node
    cur.prev = node

    def remove(self, item):
    """删除节点,只删除第一个找到的数据"""
    cur = self.__head
    while cur != None:
    if cur.elem == item:
    # 先判断此节点是否是头节点
    # if pre == None:
    if cur == self.__head:
    self.__head = cur.next
    if cur.next:
    # 判断链表是否只有一个节点
    cur.next.prev = None
    else:
    cur.prev.next = cur.next
    if cur.next:
    # 判断是否为尾节点
    cur.next.prev = cur.prev
    break
    else:
    cur = cur.next
    return

    def search(self, item):
    """查找节点是否存在"""
    cur = self.__head
    while cur != None:
    if cur.elem == item:
    return True
    else:
    cur = cur.next
    return False
  • 相关阅读:
    面试:div水平垂直居中方案--img自适应
    面试:call、apply、bind原理以及自己手写简易模式
    面试之:判断js类型的方式总结
    git的项目完整操作
    vue3.x版本新建项目相关知识和注意事项
    面试常问平时项目中【Date】的常用操作方法总结
    面试常问平时项目中【Math】的常用操作方法总结
    面试常问平时项目中数组【Array】的常用操作方法总结
    面试常问平时用的对象【Object】的创建方式和常用的对象方法总结
    优化无限列表性能vue-virtual-scroll-list【测试90w条数据】
  • 原文地址:https://www.cnblogs.com/wendaobiancheng/p/9075250.html
Copyright © 2011-2022 走看看