链表-python

一、 单链表

1. 创建单链表

class Node:
    """节点"""
    def __init__(self, item):
        self.item = item  # 值
        self.next = None  # 指针,指向下一个节点

class LinkList:
    """单链表"""
    def __init__(self):
        self._head = None
# 1. 创建单链表
List = LinkList()
# 2. 创建头节点
one = Node(1)
List._head = one
# 3. 创建节点,需要将头节点的next指向two
two = Node(2)
one.next = two
# 创建节点的同时,需要将next指向创建的这个节点
three = Node(3)
two.next = three

# 4. 打印输出
print(List._head.item)  # 1
print(List._head.next.item)  # 2
print(List._head.next.next.item)  # 3

2. 基本操作

2.1 头插法

class Node:
    """节点"""
    def __init__(self, item):
        self.item = item  # 值
        self.next = None  # 指针,指向下一个节点
        
class LinkList:
    """单链表"""
    def __init__(self):
        self._head = None

    def is_empty(self):  # 判断链表是否为空
        return self._head is None

    def add(self, item):  # 头插法,在链表头部插入节点
        # 1. 创建新节点
        node = Node(item)
        # 2. 如果链表为空则head指向新节点node,否则将node插入
        if self.is_empty():
            self._head = node
        else:
            node.next = self._head  # 新结点 指向 头结点
            self._head = node       # 头结点此时应为新结点
# 1. 创建单链表
List = LinkList()
# 2. 增加数据
List.add(1)
List.add(2)
List.add(3)
List.add(4)
List.add(5)
# 3. 打印数据
print(List._head.item)  # 5
print(List._head.next.item)  # 4
print(List._head.next.next.item)  # 3
print(List._head.next.next.next.item)  # 2
print(List._head.next.next.next.next.item)  # 1

为什么打印的是逆序的?因为是头部插入,每次都会将数据插在最前面。

2.2 尾插法

class Node:
    """节点"""
    def __init__(self, item):
        self.item = item  # 值
        self.next = None  # 指针,指向下一个节点


class LinkList:
    """单链表"""
    def __init__(self):
        self._head = None

    def is_empty(self):  # 判断链表是否为空
        return self._head is None

    def append(self, item):  # 尾插法:在链表尾部插入元素
        # 1. 创建新节点
        node = Node(item)
        # 2. 如果链表为空则head指向新节点node,否则将node插入
        if self.is_empty():
            self._head = node
        else:
            # 3. 找到最后一个节点
            current = self._head
            while current.next is not None:
                current = current.next
            # 4. 让最后一个节点指向新节点node
            current.next = node
# 1. 创建单链表
List = LinkList()
# 2. 尾插法
List.append(1)
List.append(2)
List.append(3)
# 3. 打印数据
print(List._head.item)  # 1
print(List._head.next.item)  # 2
print(List._head.next.next.item)  # 3

2.3 遍历链表

class Node:
    """节点"""
    def __init__(self, item):
        self.item = item  # 值
        self.next = None  # 指针,指向下一个节点


class LinkList:
    """单链表"""
    def __init__(self):
        self._head = None
    def printall(self):  # 遍历链表
        # 1. 获取head指针
        current = self._head
        # 2. 循环遍历
        # while current != None:
        while current is not None:
            print(current.item, end=' ')
            # 3. 移到下一个节点
            current = current.next
        print("  ")
# 1. 创建单链表
List = LinkList()
# 2. 创建节点,并加入链表
a = Node(1)
List._head = a
b = Node(2)
a.next = b
c = Node(3)
b.next = c
# 3. 遍历链表
List.printall()

2.4 删除元素

class Node:
    """节点"""
    def __init__(self, item):
        self.item = item  # 值
        self.next = None  # 指针,指向下一个节点

class LinkList:
    """单链表"""
    def __init__(self):
        self._head = None

    def is_empty(self):  # 判断链表是否为空
        return self._head is None

    def remove(self, item):  # 删除元素
        current = self._head
        if self.is_empty():  # 如果为空,则return
            return
        elif current.item == item:  # 如果删除的是第一个元素,头节点直接指向头节点的下一个节点
            self._head = current.next
        else:
            # 通过循环找到要删除的节点
            pre = None
            while current is not None and current.item != item:
                pre = current
                current = current.next
            # 要删除节点的前一个节点  指向  删除节点的后一个节点
            pre.next = current.next
# 1. 创建单链表
List = LinkList()
# 2. 创建节点,并加入链表
a = Node(1)
List._head = a
b = Node(2)
a.next = b
c = Node(3)
b.next = c
# 3. 删除元素
List.remove(2)
# 4. 打印数据
print(List._head.item) # 1
print(List._head.next.item) # 3

2.5 链表长度

class Node:
    """节点"""
    def __init__(self, item):
        self.item = item  # 值
        self.next = None  # 指针,指向下一个节点

class LinkList:
    """单链表"""
    def __init__(self):
        self._head = None

    def length(self):  # 链表长度
        # 1. 初始指针指向head
        current = self._head
        count = 0
        # 2. 指针指向None 表示到达尾部
        while current is not None:
            count += 1
            # 3. 指针下移
            current = current.next
        return count
# 1. 创建单链表
List = LinkList()
# 2. 创建节点,并加入链表
a = Node(1)
List._head = a
b = Node(2)
a.next = b
c = Node(3)
b.next = c
# 3. 链表的长度
print(List.length()) # 3

2.6 插入元素

class Node:
    """节点"""
    def __init__(self, item):
        self.item = item  # 值
        self.next = None  # 指针,指向下一个节点


class LinkList:
    """单链表"""
    def __init__(self):
        self._head = None
        
    def is_empty(self):  # 判断链表是否为空
        return self._head is None

    def length(self):  # 链表长度
        # 1. 初始指针指向head
        current = self._head
        count = 0
        # 2. 指针指向None 表示到达尾部
        while current is not None:
            count += 1
            # 3. 指针下移
            current = current.next
        return count
        
    def add(self, item):  # 头插法,在链表头部插入节点
        # 1. 创建新节点
        node = Node(item)
        # 2. 如果链表为空则head指向新节点node,否则将node插入
        if self.is_empty():
            self._head = node
        else:
            node.next = self._head  # 新结点 指向 头结点
            self._head = node  # 头结点 此时应为 新结点

    def append(self, item):  # 尾插法:在链表尾部插入元素
        # 1. 创建新节点
        node = Node(item)
        # 2. 如果链表为空则head指向新节点node,否则将node插入
        if self.is_empty():
            self._head = node
        else:
            # 找到最后一个节点
            current = self._head
            while current.next is not None:
                current = current.next
            # 让最后一个节点指向新节点node
            current.next = node
    def insert(self, item, pos):  # 增加:在某个位置插入元素
        if pos <= 0:  # 插入的位置小于0,则在头部前插入
            self.add(item)
        elif pos > (self.length() - 1):  # 插入的位置大于当前链表长度,则在尾部后插入
            self.append(item)
        else:
            # 创建节点
            node = Node(item)
            # 移到需要插入元素的位置
            pre = self._head
            for i in range(pos - 1):
                pre = pre.next
            # 新节点的下一个节点  指向  pre的下一个节点
            node.next = pre.next
            # pre的下一个节点   指向  node
            pre.next = node
# 1. 创建单链表
List = LinkList()
# 2. 插入数据
List.insert(1,0)
List.insert(2,1)
List.insert(0,0)
# 3. 遍历
List.printall()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1024节

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值